Home >Backend Development >PHP Tutorial >Why does this happen when foreach takes the value of a two-dimensional array?

Why does this happen when foreach takes the value of a two-dimensional array?

WBOY
WBOYOriginal
2016-12-01 01:27:56958browse

Why is this result 136? This 136 is the first character of the id title name field. Isn’t $v2 itself a one-dimensional array? $v2 can print all data. Why is $v2['id'] not 1 but this is the case? why is that?

<code>$bao=M('bao');
$res=$bao->select();
foreach($res as $v){
   foreach($v as $v2){
        echo $v2['id'];
   }
}</code>

Why does this happen when foreach takes the value of a two-dimensional array?

Reply content:

Why is this result 136? This 136 is the first character of the id title name field. Isn’t $v2 itself a one-dimensional array? $v2 can print all data. Why is $v2['id'] not 1 but this is the case? why is that?

<code>$bao=M('bao');
$res=$bao->select();
foreach($res as $v){
   foreach($v as $v2){
        echo $v2['id'];
   }
}</code>

Why does this happen when foreach takes the value of a two-dimensional array?

I don’t understand why you need to loop twice. You can get the ID in the first loop, and then traverse each column in the second loop. This will cause problems

The loop inside corresponds to each field, so $v2['id'] does not exist. If the error prompt is turned to the maximum, a notice will be reported

Because the data produced by Selsect is a two-dimensional array, and the data produced by find is a one-dimensional array.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn