Home > Article > Web Front-end > How to use
How to insert 5b7a15bed8615d1b843806256bebea72 in loop?
$data = array ( 0 => array ( 'id' => '1', 'name' => 'WEB编程', 'parentid' => '0', ), 1 => array ( 'id' => '2', 'name' => 'PHP', 'parentid' => '1', ), 2 => array ( 'id' => '3', 'name' => 'Ajax', 'parentid' => '1', ), 3 => array ( 'id' => '4', 'name' => 'java', 'parentid' => '1', ), 4 => array ( 'id' => '5', 'name' => 'WinForm编程', 'parentid' => '0', ), 5 => array ( 'id' => '6', 'name' => 'VB', 'parentid' => '5', ), 6 => array ( 'id' => '7', 'name' => 'VC', 'parentid' => '5', ), );
With such an array, I want to get the following effect.
The difficulty lies in how to judge whether 5a07473c87748fb1bf73f23d45547ab8 is included in 5b7a15bed8615d1b843806256bebea72 in foreach. After struggling for a long time, I still can’t find a solution. Please give some guidance on how to complete it.
$data = array ( 0 => array ( 'id' => '1', 'name' => 'WEB编程', 'parentid' => '0', ), 1 => array ( 'id' => '2', 'name' => 'PHP', 'parentid' => '1', ), 2 => array ( 'id' => '3', 'name' => 'Ajax', 'parentid' => '1', ), 3 => array ( 'id' => '4', 'name' => 'java', 'parentid' => '1', ), 4 => array ( 'id' => '5', 'name' => 'WinForm编程', 'parentid' => '0', ), 5 => array ( 'id' => '6', 'name' => 'VB', 'parentid' => '5', ), 6 => array ( 'id' => '7', 'name' => 'VC', 'parentid' => '5', ), ); echo '';
Nagging brother, the above operation still lacks the end characterbc2752b74169f27a1fb12a4976198c73
I am just wondering how to add thisbc2752b74169f27a1fb12a4976198c73.
echo "";
Just change it a little bit.
$data = array ( 0 => array ( 'id' => '1', 'name' => 'WEB编程', 'parentid' => '0', ), 1 => array ( 'id' => '2', 'name' => 'PHP', 'parentid' => '1', ), 2 => array ( 'id' => '3', 'name' => 'Ajax', 'parentid' => '1', ), 3 => array ( 'id' => '4', 'name' => 'java', 'parentid' => '1', ), 4 => array ( 'id' => '5', 'name' => 'WinForm编程', 'parentid' => '0', ), 5 => array ( 'id' => '6', 'name' => 'VB', 'parentid' => '5', ), 6 => array ( 'id' => '7', 'name' => 'VC', 'parentid' => '5', ), ); $tempArray = array(); foreach($data as $item){ $tempArray[$item['parentid']][$item['id']] = $item['name']; } echo '';
I just foreach three times for fear of poor efficiency. I don’t know if there is any other method.
If you redo the array...
$data = array ( array ( 'id' => '1', 'name' => 'WEB编程', 'parentid' => '0', 'sub'=> array( array( 'id' => '2', 'name' => 'PHP', 'parentid' => '1', ), array ( 'id' => '3', 'name' => 'Ajax', 'parentid' => '1', ), array ( 'id' => '4', 'name' => 'java', 'parentid' => '1', ) ) ), array ( 'id' => '5', 'name' => 'WinForm编程', 'parentid' => '0', 'sub'=> array( array ( 'id' => '6', 'name' => 'VB', 'parentid' => '5', ), array ( 'id' => '7', 'name' => 'VC', 'parentid' => '5', ) ) ) ); echo '';
Really?
Oh, it is missing bc2752b74169f27a1fb12a4976198c73
But when I tested the code, I didn’t feel the difference between yes and no
If it must be there, just add a switch variable. Why do we need to cycle multiple times?
echo '';
The above is the detailed content of How to use