Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to parse articles from mysql and encode the data in json with php.

Currently publishing articles I use:

<?php if ($success): ?>
    <?php foreach($article->get_items() as $item): ?>

    <?php echo $item->get_content(); ?>


    <?php endforeach; ?>
<?php endif; ?>

and I am trying to encode this into json.

I have tried this:

<?php if ($success): ?>
    <?php foreach($feed->get_items() as $item): ?>

    <?php
    $data = array(
        'id'    =>    "1",
        'result'       =>    array(
                            'title'    =>   'This is the title',
                            'publish'   =>  'John Doe',
                            'content'    =>  $item->get_content()
                     )
    );

    echo json_encode($data);
?>
<?php endforeach; ?>
<?php endif; ?>

Also, I'm not sure how I would use foreach() so that I could parse and encode all the content.

UPDATE:

The content parsed from $item->get_content() has HTML elements such as , etc. so they should be encoded into json or strings ?

UPDATE 2:

The problem is that currently I end up with this:

[
{"id":"1","result":{"title":"This is the title","publish":"John Doe","content":"content 1"}},
{"id":"1","result":{"title":"This is the title","publish":"John Doe","content":"content 1"}},
{"id":"1","result":{"title":"This is the title","publish":"John Doe","content":"content 1"}},
{"id":"1","result":{"title":"This is the title","publish":"John Doe","content":"content 1"}},
{"id":"1","result":{"title":"This is the title","publish":"John Doe","content":"content 1"}}
]

because I am not using foreach() properly and I want to end up with this:

[
{"id":"1","result": {"title":"This is the title","publish":"John Doe","content":"content 1"},
            {"title":"This is the title","publish":"John Doe","content":"content 1"},
            {"title":"This is the title","publish":"John Doe","content":"content 1"},
            {"title":"This is the title","publish":"John Doe","content":"content 1"},
            {"title":"This is the title","publish":"John Doe","content":"content 1"}
]

and also the content sometimes it contains html elements which destroys the json encoding so I would imagine I have to encode it into json or string?

share|improve this question
1  
Take out the echo in content's value and you should be fine. –  Interrobang Mar 28 '13 at 22:42
    
Don't break out of <?php unless you have to. Here, you definitely do not want to. –  Eric Mar 28 '13 at 22:43
    
@Interrobang Sorry it was just a wrong paste. Updated. –  Johny Bryne Mar 28 '13 at 22:45
    
@Eric Sorry it was just a wrong paste. Updated. –  Johny Bryne Mar 28 '13 at 22:46
    
"parse articles" -- What? –  fab Mar 28 '13 at 22:49

3 Answers 3

up vote 4 down vote accepted

Create an array that holds all the data and then encode it to json

if ($success){
    $result = array();
    foreach($feed->get_items() as $item){
        $data = array(
             'id'    =>    "1",
             'result'       =>    array(
                   'title'    =>   'This is the title',
                   'publish'  =>  'John Doe',
                   'content'  =>  $item->get_content()
             )
         );
         array_push($result,$data);
    }                
    echo json_encode($result);
}
share|improve this answer
2  
Don't wrap every line in a <?php ... ?>! –  Eric Mar 28 '13 at 22:49
    
@Eric I was just using his code... Now it's fixed and nicer. –  Ander2 Mar 28 '13 at 22:54
<?php
foreach($article->get_items() as $item){
    $result[] = array(
            'title'   => 'This is the title',
            'publish' => 'John Doe',
            'content' => $item->get_content()
        );
    );
}

echo json_encode(array('id'=>'1', 'result'=>$result));
?>

Actually I'm not sure I understand what you need so maybe it wouldn't help.

share|improve this answer
    
Beat me to it - +1 –  Eric Mar 28 '13 at 22:48
    
Sorry can you please see the updated question? –  Johny Bryne Mar 28 '13 at 22:48
    
Drop the echo –  Eric Mar 28 '13 at 22:48
    
@eric I want to have only I result array and within that the 'foreach()' statement. Im just not sure how to do it. –  Johny Bryne Mar 28 '13 at 22:53
1  
echo $item->get_content() should be $item->get_content() here –  Eric Mar 28 '13 at 22:56

When outputting a string that contains HTML code, you'll want to use htmlspecialchars:

echo htmlspecialchars(json_encode($data));

For example:

$data = array('id' => '<hello>');

echo htmlspecialchars(json_encode($data));

// Outputs: {"id":"<hello>"}
share|improve this answer
    
Great! That gets the job done. Do you also know the solution for the foreach() after result array has been created? –  Johny Bryne Mar 28 '13 at 23:06
    
See Ander2's answer. Looks about right. –  MichaelRushton Mar 28 '13 at 23:06
    
Bingo. Thanks alot –  Johny Bryne Mar 28 '13 at 23:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.