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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化