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 doing a GET php function on a URL and I get back the resulting response in JSON:

{
    "results": [{
        "text": "The 2014 BICSI Canadian Conference and Exhibition in Vancouver,    British Columbia, Canada is 61 days away on April 27 - 30.",
        "createdAt": "2014-02-  24T19:54:08.707Z",
        "updatedAt": "2014-02-24T19:54:08.707Z",
        "objectId": "ZZrZ9OgyRG"
    }, {
        "text": "Only 33 more days fro the 2014 BICSI Canadian Conference and Exhibition in Vancouver, Canada!",
        "createdAt": "2014-03-24T13:23:56.240Z",
        "updatedAt": "2014-03-24T13:23:56.240Z",
        "objectId": "ZqxJRiHoJo"
    }]
}

I am able to do php json_decode and display the results below on a web page like below

text | The 2014 BICSI Canadian Conference and Exhibition in Vancouver, British Columbia, Canada is 61 days away on April 27 - 30.

createdAt | 2014-02-24T19:54:08.707Z

updatedAt | 2014-02-24T19:54:08.707Z

objectId | ZZrZ9OgyRG

text | Only 33 more days fro the 2014 BICSI Canadian Conference and Exhibition in Vancouver, Canada!

createdAt | 2014-03-24T13:23:56.240Z

updatedAt | 2014-03-24T13:23:56.240Z

objectId | ZqxJRiHoJo

The php code I used to display the results above on the web page is:

$returned_content = get_data('https://api.parse.com/1/classes/Alerts');

$data = json_decode($returned_content, true);

foreach ($data as $array1 => $arrayn) {
    foreach ($arrayn as $k => $v) {
        foreach ($v as $t => $s) {
            echo"<p> $t | $s ";
        }
    }
}

If I just wanted to display the 'text' key/value info only on the web page, how should I modify the php. Meaning all I want to see displayed on the web page is:

text | The 2014 BICSI Canadian Conference and Exhibition in Vancouver, British Columbia, Canada is 61 days away on April 27 – 30.

text | Only 33 more days fro the 2014 BICSI Canadian Conference and Exhibition in Vancouver, Canada!
share|improve this question
1  
Please format your question properly –  Sahil Mittal Mar 25 at 13:26
    
Thought I did... sorry about that. –  bachma0507 Mar 25 at 15:43
add comment

5 Answers

up vote 1 down vote accepted
$data = json_decode($json);
foreach ($data->results as $item) {
    echo '<br>text | '.$item->text;
}

If you don't add the second json_decode() paremeter, you can just use your JSON with the StdClass.

share|improve this answer
add comment

Wrap it inside a simple if condition.

foreach ($data as $array1 => $arrayn) {
    foreach($arrayn as $k => $v) {
        foreach ($v as $t => $s) {
            if($t == 'text') {
                echo"<p> $t | $s ";
            }
        }
   }
}
share|improve this answer
add comment

do like this

$returned_content = get_data('https://api.parse.com/1/classes/Alerts');

$data = json_decode($returned_content, true);

foreach ($data as $array1 => $arrayn) {
foreach($arrayn as $k => $v){
foreach ($v as $t => $s)
{
if($t == "text")
echo"<p> $t | $s ";
}
   }

}
share|improve this answer
add comment

After this line :

$data = json_decode($returned_content, true);

You have an associative array, so you can change your loop to :

foreach ($data as $array1 => $arrayn) {
    foreach($arrayn as $k => $v) {
        foreach ($v as $t => $s) {
            if('text' === $t) {
                echo"<p> $t | $s ";
            }
        }
   }
}
share|improve this answer
add comment

Make it simple and with a check if the key exists, just in case the JSON does not contain the key "text" it will return notice.

$data = json_decode($str,true);
foreach($data["results"] as $key=>$val){
    if(array_key_exists("text",$val)){
        echo 'text | '.$val["text"];
        echo '<br />';
    }
}

Here $str is your json string.

share|improve this answer
add comment

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.