>  기사  >  백엔드 개발  >  遍历数组,根据mark值的不同,使数据显示在不同的模块中

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

WBOY
WBOY원래의
2016-06-06 20:32:01937검색

<code>//有这样一个数组$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')));
</code>
<code>//在这里我遍历了三次,然后放在这三个div中,很麻烦,怎么遍历一次就可以将数据分类放在这几个div中呢?
         if($jobArr[$index]){
            foreach($jobArr[1] as $post){
                $jobList2 .= 
                    <a class="post post2">{$post['name']}</a>
                    <span class="salary">{$post['desc']}</span>
                
html;
        }
            foreach($jobArr[2] as $post){
                $jobList3 .= 
                <a class="post post2">{$post['name']}</a>
                <span class="salary">{$post['desc']}</span>
            
html;
            }
            foreach($jobArr[3] as $post){
                $jobList4 .= 
                <a class="post post2">{$post['name']}</a>
                <span class="salary">{$post['desc']}</span>
            
html;
            }
        }
</code>
<code> <div1>{ $jobList2} 
 <div2>{ $jobList3}
 <div3>{ $jobList4}
</div3></div2></div1></code>

回复内容:

<code>//有这样一个数组$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')));
</code>
<code>//在这里我遍历了三次,然后放在这三个div中,很麻烦,怎么遍历一次就可以将数据分类放在这几个div中呢?
         if($jobArr[$index]){
            foreach($jobArr[1] as $post){
                $jobList2 .= 
                    <a class="post post2">{$post['name']}</a>
                    <span class="salary">{$post['desc']}</span>
                
html;
        }
            foreach($jobArr[2] as $post){
                $jobList3 .= 
                <a class="post post2">{$post['name']}</a>
                <span class="salary">{$post['desc']}</span>
            
html;
            }
            foreach($jobArr[3] as $post){
                $jobList4 .= 
                <a class="post post2">{$post['name']}</a>
                <span class="salary">{$post['desc']}</span>
            
html;
            }
        }
</code>
<code> <div1>{ $jobList2} 
 <div2>{ $jobList3}
 <div3>{ $jobList4}
</div3></div2></div1></code>

是要这个效果吗?

<code>php</code><code>$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'].="<li>
<a class="post post2">{$val['name']}</a><span class="salary">{$val['salary']}</span>
</li>";   
    }
    $div.="<div>".$html[$val['mark']]['html']."</div>";
}
echo $div;
</code>
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.