0

Using an application's API, I'm able to retrieve data as a JSON and put it in an array.

$json = file_get_contents($url);
$obj = json_decode($json);
print_r($obj);

When I print the array, I see:

stdClass Object(
    [metadataList] = > stdClass Object(
        [metadata] = > Array(
                [0] = > stdClass Object([metadataName] = > category [metadataValue] = > RECIPES)
                [1] = > stdClass Object([metadataName] = > title [metadataValue] = > Easy Sugar Cookies)
            )
        )
)

I need to set a variable equal to the title metadata value ("Easy Sugar Cookies"), but I'm having some trouble.

Here's where I am so far, but I'm not having any luck figuring out how to specify the specific metadataValue key, since there is more than one in the array.

$title = array_search('description',($obj->{'metadataList'}->{'metadata'}));
3
  • if i'm not mistaken , array_search won't help you because that string is in an Object. Commented Feb 28, 2012 at 14:56
  • Do you mean get the title value? Commented Feb 28, 2012 at 14:57
  • So you jsut want that value? .. Or do you need all values that might be a title? $obj->metadataList->metadata[1]->metadataValue (direct) or (expensive) foreach($obj->metadataList->metadata as $meta) { if($meta->metadatavalue == ... } Commented Feb 28, 2012 at 15:02

4 Answers 4

1

I think this is you have want...

$obj = json_decode($json, true);
$value = '';
  foreach($obj['metadataList']['metaData'] as $metadata) {
  if($metadata['metadataName'] === 'someName') {
    $value = $metadata['metadataValue'];
    break;
  }
   continue ;

}
Sign up to request clarification or add additional context in comments.

1 Comment

The continue statement is superfluous.
0

$obj->{'metadataList'}->{'metadata'} is an array of objects, not of strings. Hence array_search(string, array) won't work (apart from the fact, that there is no string description in this array/object structure).

I suggest you parse the data as array instead (object works fine as well though) and simply iterate over the array:

$obj = json_decode($json, true);
$value = '';
foreach($obj['metadataList']['metaData'] as $metadata) {
    if($metadata['metadataName'] === 'someName') {
        $value = $metadata['metadataValue'];
        break;
    }
}

Comments

0
foreach(($obj->{'metadataList'}->{'metadata'}) as $sub_object)
{
 if(($sub_object->{'metadataName'}) == "title")
  return ($sub_object->{'metadataName'}->{'metadataValue'});
}

Comments

0

Off the top of my head

$originalValue = "";
foreach($obj->metadataList->metadata as $index=>$metadataInfo){
  if($metadataInfo->metadataName =="title"){
    $originalValue = $obj->metadataList->metadata[$index]->metadataValue;
    $obj->metadataList->metadata[$index]->metadataValue = "hi big boy";
  }
}

1 Comment

If you are in control of the output of the object then make the metadata an Associative array then you can reference it as $title = $obj->metadataList->metadata["title"];

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.