1

Using PHP and MySQL, I have generated an array called $response.

A var_dump of $response can be seen here.

array(2) {
  ["OperationRequest"]=>
  array(4) {
    ["HTTPHeaders"]=>
    array(1) {
      [0]=>
      array(1) {
        ["@attributes"]=>
        array(2) {
          ["Name"]=>
          string(9) "UserAgent"
          ["Value"]=>
          string(14) "ApaiIO [2.1.0]"
        }
      }
    }
    ["RequestId"]=>
    string(36) "f53f381e-efb3-4fef-8e39-4f732b4b463e"
    ["Arguments"]=>
    array(1) {
      ["Argument"]=>
      array(11) {
        [0]=>
        array(1) {
          ["@attributes"]=>
          array(2) {
            ["Name"]=>
            string(14) "AWSAccessKeyId"
            ["Value"]=>
            string(20) "KEY"
          }
        }
        [1]=>
        array(1) {
          ["@attributes"]=>
          array(2) {
            ["Name"]=>
            string(12) "AssociateTag"
            ["Value"]=>
            string(11) "TAG"
          }
        }
        [2]=>
        array(1) {
          ["@attributes"]=>
          array(2) {
            ["Name"]=>
            string(6) "IdType"
            ["Value"]=>
            string(4) "ISBN"
          }
        }
        [3]=>
        array(1) {
          ["@attributes"]=>
          array(2) {
            ["Name"]=>
            string(6) "ItemId"
            ["Value"]=>
            string(38) "0751538310,9780141382067,9781305341141"
          }
        }
        [4]=>
        array(1) {
          ["@attributes"]=>
          array(2) {
            ["Name"]=>
            string(9) "Operation"
            ["Value"]=>
            string(10) "ItemLookup"
          }
        }.......so on

A json_encode of the array can be seen here (as requested in a comment).

I'd like to select the Title from these two items. From what I can see this is located at;

Items > Item > ItemAttributes > Author

So, using a foreach loop I have tried the following;

foreach ($response as $item) {
    echo $item['Items']['Item']['ItemAttributes']['Title']; // line 2
}

However this returns the following error;

Message: Undefined index: Items. Line Number: 2

Where am I going wrong and what must I change in my code in order to achieve the desired result?

Also, any advice on how to 'read' multidimensional arrays would be greatly appreciated.

Thanks

6
  • youn need to do like this:- echo $item['OperationRequest']['Items']['Item']['ItemAttributes']['Title']; Commented May 11, 2017 at 9:02
  • 1
    @johnny_s Can you share the json of the above array? Commented May 11, 2017 at 9:06
  • @johnny_s echo json_encode($response); Commented May 11, 2017 at 9:09
  • @johnny_s echo json_encode($json, JSON_PRETTY_PRINT); Commented May 11, 2017 at 9:09
  • @johnny_s, your array keys must exist. What I see is that you use array keys like 'Items', 'item', and that they don't exist in the actual array (var_dumped). That's why you get this error,... Commented May 11, 2017 at 9:22

3 Answers 3

2

Try this one, it will help you out. You were are iterating on the wrong key that's why you were not getting desired output.

Try this code snippet herefrom json provide by OP in question

foreach($array["Items"]["Item"] as $key => $value)
{
    print_r($value["ItemAttributes"]["Title"]);
    echo PHP_EOL;
}

Output:

Panic Panic Captain Flinn and the Pirate Dinosaurs: Missing Treasure! (Captain Flinn)

For getting unique titles:

foreach(json_decode($json,true)["Items"]["Item"] as $key => $value)
{
    $result[]=$value["ItemAttributes"]["Title"];
    echo PHP_EOL;
}
print_r(array_unique($result));
Sign up to request clarification or add additional context in comments.

Comments

1

@Also, any advice on how to 'read' multidimensional arrays would be greatly appreciated.

Post your encoded json string to

http://json.parser.online.fr

"+" and "-" button at the right panel should help you read it easily.

//Check Items is not empty
if( !isset($response["Items"]["Item"]) || empty($response["Items"]["Item"]) )
    throw New Exception("Empty Item");

foreach($response["Items"]["Item"] as $item){
    $title = $item['ItemAttributes']['Title']
}

1 Comment

Thanks for the explanation and link to json.parser.online.fr @Parfait - both very helpful :)
0

You should debug as:

foreach ($response as $key => $item) {
    if(isset($item['Items'])){ //Check Items index defined
        echo $item['Items']['Item']['ItemAttributes']['Title'];
    }else{
        var_dump($key);
    }
}

2 Comments

What is the purpose of that exactly? That produces string(16) "OperationRequest" string(5) "Items". Thanks
Because your array defined by keys, You have to input exactly keys as path for access child array. If array with key not exist, php will warning is: "Undefined index" With "OperationRequest" and "Items" key, It hasn't child array $item['Items'] => error You can add this script for debug each key: var_dump($key); die;

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.