1

I have no idea why this is not working. The count comes back with the correct size for the array.

Here's the mWeb.json file:

    [{"Title": "Some-Name","h1": "Some-h1-words", "Comments": "Some Comment", "Comments2": "Some Comment2"},
{"Title": "Some-Name b","h1": "Some-h1-words b", "Comments": "Some Comment b", "Comments2": "Some Comment2 b"},
{"Title": "Some-Name c","h1": "Some-h1-words c", "Comments": "Some Comment c", "Comments2": "Some Comment2 c"}]

Here's the code:

    <?
$url = 'mWeb.json';
$JSON = file_get_contents($url);
    $someName = "Some-Name";    
$data = json_decode($JSON);
echo count($data);
for($x=0; $x<count($data); $x++){
    if($data[$x]['Title']==$someName){
        echo '[{"Title":"'.$data[$x]["Title"].'","h1":"'.$data[$x]["h1"].'","Comments":"'.$data[$x]["Comments"].'","Comments2":"'.$data[$x]['Comments2'].'"}]';
    }
}
?>

All the echoes is the length of the array - echo count($data);

1 Answer 1

6

If you var_dump($data) you would see that your objects are... objects!

To get an associative array, do this:

$data = json_decode($JSON,true);
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, just because I left off the ",true" - thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.