简体   繁体   English

具有相同键或值的数组不返回

[英]array is not returning if has the same key or value

$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. 但我希望它返回所有键和值,如果value不为空。

I am actually getting the data from form. 我实际上是从表单获取数据。 and my actual code is foreach ($_post as $key=>$value ) 而我的实际代码是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 ) 

The problem is that array keys must be unique, but you're setting the same key child1 with three different values ( 167 , 167 and 165 ) so you're just overwriting the same entry each time. 问题是,数组的键必须是唯一的,但你设定相同的密钥child1有三个不同的值( 167167165 ),所以你只是每次都覆盖相同的条目。

What you could do is: 您可以做的是:

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

  }
}

which will generate a multi-dimensional array 这将生成一个多维数组

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

The key are uniques if you print $arrays on the begin you will find the answer: 如果在开始时打印$ arrays,则密钥是唯一的,您将找到答案:

echo "<pre>";
print_r($arrays);
echo "</pre>";

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
        )

)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM