dtxf759200 2015-03-29 22:46
浏览 28
已采纳

如果具有相同的键或值,则不返回数组

$arrays = array (
    'child1_167'=>'1st', 
    'child1_167'=>'2nd', 
    'child1_165'=>'2nd', 
    'child2_165'=>'1st', 
    'child3_164'=>'2nd', 
    'child1_164'=>'' 
);

$classes = array();
foreach ($arrays as $key=>$value) {

  if($value != '') {
      $exp= explode('_', $key);
      $classes[$exp[0]] = $exp[1];

  }

}
 print_r($classes);

currently it is returning like this:

Array ( [child1] => 165 [child2] => 165 [child3] => 164 ) 

but i want it to return all the keys and value if value is not blank.

I am actually getting the data from form. and my actual code is foreach ($_post as $key=>$value )

<td>
  <select name="child1_<?php echo child_id(); ?>">
   <option></option>
   <option>1st</option>
   <option>2nd</option>
  </select>
</td>
<td>
  <select name="child2_<?php echo child_id(); ?>">
   <option></option>
   <option>1st</option>
   <option>2nd</option>
  </select>
</td>
<td>
  <select name="child3_<?php echo child_id(); ?>">
   <option></option>
   <option>1st</option>
   <option>2nd</option>
  </select>
</td>

and here is the form post data.

Array ( [child1_167] => 1st [child2_167] => 2nd [child3_167] => [child1_165] => [child2_165] => 1st [child3_165] => 2nd [child1_164] => 2nd [child2_164] => [child3_164] => 1st ) Array ( [167] => 2nd [165] => 2nd [164] => 1st ) 
  • 写回答

3条回答 默认 最新

  • douju2599 2015-03-29 22:59
    关注

    Working with the array you provided in the comments her as an example that has all the values, storing them as a two-dimensional associative array:

     $arrays =  Array ( 
            'child1_167' => '1st' ,
            'child2_167' => '2nd' ,
            'child3_167' => '',
            'child1_165' => '',
            'child2_165' => '1st' ,
            'child3_165' => '2nd' ,
            'child1_164' => '2nd' ,
            'child2_164' => '',
            'child3_164' => '1st' ,
            ) ;
    
    
    $classes = array();
    foreach ($arrays as $key=>$value) {
    
      if($value != '') {
    
          $exp= explode('_', $key);
          $child_number=$exp[0];
          $child_id=$exp[1];
          $child_order=$value;
          $classes[$child_number][$child_order] = $exp[1];
    
      }
    
    }
     print_r($classes);
    

    And this is the output:

    Array
    (
        [child1] => Array
            (
                [1st] => 167
                [2nd] => 164
            )
    
        [child2] => Array
            (
                [2nd] => 167
                [1st] => 165
            )
    
        [child3] => Array
            (
                [2nd] => 165
                [1st] => 164
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊