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

WBOY
Release: 2023-03-02 14:08:02
Original
936 people have browsed it

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!