PHP traverses the code for a two dimensional array

  • 2020-05-05 10:59:04
  • OfStack

At first, I intended to use foreach for many times, but I found that it did not work, oo failed, and oo could not write
After studying, I decided to use for loop. The demo code is as follows:
 
<?php 
$blog=array( 
array( 
"titledata"=>"titleMM", 
"bodydata"=>"bodyMM" 
), 
array( 
"titledata"=>"titleGG", 
"bodydata"=>"bodyGG" 
) 
); 
// error  
foreach($blog as $b) 
{ 
$b['titledata']="BB"; 
$b['bodydata']="CC"; 
} 
print_r($blog); 
// correct  
for($i=0;$i<count($blog);$i++) 
{ 
$blog[$i]['titledata']="title"; 
$blog[$i]['bodydata']="body"; 
} 
?> 

Related articles: