当前位置:Gxlcms > PHP教程 > 遍历数组,根据mark值的不同,使数据显示在不同的模块中

遍历数组,根据mark值的不同,使数据显示在不同的模块中

时间:2021-07-01 10:21:17 帮助过:12人阅读

//有这样一个数组$jobArr
$jobArr=array(
  1 => 
    array(
      0 => 
        array(
          'name' =>  '产品经理1',
          'desc' =>  '广州' ,
          'mark' =>  'group:1' ),
      1 => 
        array 
         ( 'name' =>  '产品经理1', 
          'desc' =>  '广州' ,
          'mark' =>  'group:1'),
      2 => 
        array 
         ( 'name' =>  '产品经理1',
          'desc' =>  '广州' ,
          'mark' =>  'group:1') ),
  2 => 
    array (
      0 => 
        array (
          'name' =>  '产品经理2' ,
          'desc' =>  '广州' ,
          'mark' =>  'group:2'),
      1 => 
        array (
          'name' =>  '产品经理2' ,
          'desc' =>  '广州' ,
          'mark' =>  'group:2')),
  3 => 
    array (
      0 => 
        array (
          'name' =>  '产品经理3' ,
          'desc' =>  '广州' ,
          'mark' =>  'group:3' ),
      1 => 
        array (
          'name' =>  '产品经理3' ,
          'desc' =>  '广州' ,
          'mark' =>  'group:3')));
//在这里我遍历了三次,然后放在这三个div中,很麻烦,怎么遍历一次就可以将数据分类放在这几个div中呢?
         if($jobArr[$index]){
            foreach($jobArr[1] as $post){
                $jobList2 .= <<
                    {$post['name']}
                    {$post['desc']}
                
html;
        }
            foreach($jobArr[2] as $post){
                $jobList3 .= <<
                {$post['name']}
                {$post['desc']}
            
html;
            }
            foreach($jobArr[3] as $post){
                $jobList4 .= <<
                {$post['name']}
                {$post['desc']}
            
html;
            }
        }
 { $jobList2} 
 { $jobList3}
 { $jobList4}

回复内容:

//有这样一个数组$jobArr
$jobArr=array(
  1 => 
    array(
      0 => 
        array(
          'name' =>  '产品经理1',
          'desc' =>  '广州' ,
          'mark' =>  'group:1' ),
      1 => 
        array 
         ( 'name' =>  '产品经理1', 
          'desc' =>  '广州' ,
          'mark' =>  'group:1'),
      2 => 
        array 
         ( 'name' =>  '产品经理1',
          'desc' =>  '广州' ,
          'mark' =>  'group:1') ),
  2 => 
    array (
      0 => 
        array (
          'name' =>  '产品经理2' ,
          'desc' =>  '广州' ,
          'mark' =>  'group:2'),
      1 => 
        array (
          'name' =>  '产品经理2' ,
          'desc' =>  '广州' ,
          'mark' =>  'group:2')),
  3 => 
    array (
      0 => 
        array (
          'name' =>  '产品经理3' ,
          'desc' =>  '广州' ,
          'mark' =>  'group:3' ),
      1 => 
        array (
          'name' =>  '产品经理3' ,
          'desc' =>  '广州' ,
          'mark' =>  'group:3')));
//在这里我遍历了三次,然后放在这三个div中,很麻烦,怎么遍历一次就可以将数据分类放在这几个div中呢?
         if($jobArr[$index]){
            foreach($jobArr[1] as $post){
                $jobList2 .= <<
                    {$post['name']}
                    {$post['desc']}
                
html;
        }
            foreach($jobArr[2] as $post){
                $jobList3 .= <<
                {$post['name']}
                {$post['desc']}
            
html;
            }
            foreach($jobArr[3] as $post){
                $jobList4 .= <<
                {$post['name']}
                {$post['desc']}
            
html;
            }
        }
 { $jobList2} 
 { $jobList3}
 { $jobList4}

是要这个效果吗?

php$jobArr=array(
  1 => 
    array(
      0 => 
        array(
          'name' =>  '产品经理1',
          'desc' =>  '广州' ,
          'mark' =>  'group:1' ),
      1 => 
        array 
         ( 'name' =>  '产品经理1', 
          'desc' =>  '广州' ,
          'mark' =>  'group:1'),
      2 => 
        array 
         ( 'name' =>  '产品经理1',
          'desc' =>  '广州' ,
          'mark' =>  'group:1') ),
  2 => 
    array (
      0 => 
        array (
          'name' =>  '产品经理2' ,
          'desc' =>  '广州' ,
          'mark' =>  'group:2'),
      1 => 
        array (
          'name' =>  '产品经理2' ,
          'desc' =>  '广州' ,
          'mark' =>  'group:2')),
  3 => 
    array (
      0 => 
        array (
          'name' =>  '产品经理3' ,
          'desc' =>  '广州' ,
          'mark' =>  'group:3' ),
      1 => 
        array (
          'name' =>  '产品经理3' ,
          'desc' =>  '广州' ,
          'mark' =>  'group:3')));

foreach($jobArr as $k=>$v){
    $html=array();
    foreach($v as $key=>$val){
        $html[$val['mark']]['html'].="
  • {$val['name']}{$val['salary']}
  • "; } $div.="".$html[$val['mark']]['html'].""; } echo $div;

    人气教程排行