Multidimensional arrays in php

I am new to php (learning from 1 week). I am studying arrays. in doing so, I found an api that produces results in the form of a multidimensional array. and I cannot repeat the values ​​of the array.

Response example

Array ( [query] => Array ( [count] => 1 [created] => 2010-07-16T08:35:38Z [lang] => en-US [results] => Array ( [item] => Array ( [rel] => rel:Person [resource] => http://twitter.com/twitter [meta] => Array ( [0] => Array ( [property] => foaf:name [content] => Twitter ) ) ) ) ) ) 

I can repeat some values ​​... for example

 echo $array["query"]['count']."<br />"; echo $array["query"]["results"]["item"]["resource"]; 

but when I want to use [meta] => Array

I can not use: (

 echo $array["query"]["results"]["item"]["resource"]["meta']["0"["content"]; 

please guide me

+4
source share
8 answers

You should use your debugging skills to solve this problem.

  • First, print_r() your $ array, which you did.
  • Then print_r($array['query'])
  • Then print_r($array['query']['results'])
  • etc. etc.

When you get to print_r($array["query"]["results"]["item"]["resource"]) , you will see that the result is not an array, it is a scalar, so you need a different index.

Good luck

+5
source

Perhaps this is because you are not closing the array ["0"]

 echo $array["query"]["results"]["item"]["resource"]["meta"][0]["content"] 
+3
source

From the paste:

 [query] => Array ( [results] => Array ( [item] => Array ( [resource] => "http://twitter.com/twitter" [meta] => Array ( [0] => Array ( [content] => "Twitter" (...) 

$array["query"]["results"]["item"]["resource"] not an array, this is a string; you probably want the meta array to be inside $array["query"]["results"]["item"] (same level as resource ). This should work:

 echo $array["query"]["results"]["item"]["meta"]["0"]["content"]; 

In addition, you made two typos:

  • ["meta'] - you open the meta with a double quote " and try to close the single quotation mark ' - they must be the same - for example, ['meta']
  • ["0" - you did not close the bracket - for example, ['0']
+2
source

You forgot] in ["0"]

Also leave "around 0" since you are calling thins elemnt by index, not by name:

 $array["query"]["results"]["item"]["resource"]["meta"][0]["content"] 

And for meta, you used two different types: “Once” and once “. Closing” should be of the same type as the opener.

+1
source
 echo $array["query"][count]."<br />"; echo $array["query"]["results"]["item"]["resource"]; 

count must be "count" , otherwise PHP considers count to be a constant.


 echo $array["query"]["results"]["item"]["resource"]["meta']["0"["content"]; 

"meta' should be "meta" , you should use the same type of quote.

["0" must be ["0"] or [0] - you have to close the bracket.

+1
source

Morgen32 is true. You did not close the opening [

In addition, you can do the same as the API you are using by typing

 echo "<pre>".print_r($array, true)."</pre>"; 

below the place of your code where you are creating the array.

0
source

Sorry for the length of this post - this is more like a mini tutorial, but hopefully this should give you some good concepts to help you solve this problem. There are several approaches that I would recommend.

Tip # 1 , when you use print_r, try using it like this:

 print_r ($array[query], 1); 

This will allow you to add a “return” by setting the return flag to true. The advantage of this is that you can embed it like this:

 <pre> <?php echo (print_r($array[query], 1)); ?> </pre> 

This will print a “pre-formatted” array into your HTML code, which will save all spaces and line breaks. See http://php.net/manual/en/function.print-r.php for details. I will not go into details on how to do this, but there are a few tutorials here that will help you get started: http://www.java2s.com/Code/Php/Data-Structure/LoopingThroughaMultidimensionalArray.htm (also http: / /php.net/manual/en/control-structures.foreach.php )

Here is a simple example using the object code above:

Tip number 2 . Often I find that when I work with the API and "arrays" derived from the database results, the type is actually erroneous. For example, you often get something that looks like an array, but it's actually a stdObject. Even if it is not, I would advise you to try this function (from http://php.net/manual/en/function.var-dump.php ):

 <?php $a = array(1, 2, array("a", "b", "c")); var_dump($a); ?> 

Objects and arrays perform similar actions, but you can avoid notifications and possible headaches using this approach, since var_dump also displays the type and length of the object. You may try:

 <?php $book = new stdClass; $book->title = "Harry Potter and the Prisoner of Azkaban"; $book->author = "JK Rowling"; $book->publisher = "Arthur A. Levine Books"; $book->amazon_link = "http://rads.stackoverflow.com/amzn/click/0439136369"; ?> <pre> <?php ob_start(); var_dump($book); $a = ob_get_clean(); $b = print_r($book,1); echo($a."\n\n".$b); ?> </pre> 

It also adds output buffering, which can affect performance, but I saved several hours of debugging from deviations (you will also want to use something like Zend-debug or Xdebug). There is a bit of buffering PHP output with var_dump: How can I get the var_dump result for a string?

0
source

do not insert your numeric index (0) in quotation marks:

 echo $array["query"]["results"]["item"]["resource"]["meta"][0]["content"]; 

edit: iirc there is a difference between numeric and string indices. however, the real problem is that you did not close the single bracket and had mixed single / double quotes around the meta.

it should have reported a syntax error too ...

-1
source

Source: https://habr.com/ru/post/1315905/


All Articles