Home  >  Article  >  Web Front-end  >  How to use tag in loop

How to use tag in loop

黄舟
黄舟Original
2017-07-03 09:43:441929browse

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 "$v[name]";

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 tag in loop. For more information, please follow other related articles on the PHP Chinese website!

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