0

我已经能够搜索一个多维数组,比如

Array
(
    [0] => Array
        (
            [size] => 39
            [colour] => N/A
            [quantity] => 5
        )

    [1] => Array
        (
            [size] => 35
            [colour] => N/A
            [quantity] => 5
        )

    [2] => Array
        (
            [size] => 40
            [colour] => N/A
            [quantity] => 5
        )

    [3] => Array
        (
            [size] => 30
            [colour] => N/A
            [quantity] => 5
        )

)

并返回匹配的匹配数组,如下所示

Array
(
    [size] => 35
    [colour] => N/A
    [quantity] => 5
)

使用这个片段

$result = array();
foreach ($multi_array as $arr) {
  if ($arr['size'] == $one_dimen_arr['size'] && $arr['colour'] == $one_dimen_arr['colour']) {
    $result = $arr;
    break;
  }
}

是否可以从主多维数组中删除匹配项并替换为该位置的更新数组?

4

2 回答 2

1

像这样试试

$result = array();
foreach ($multi_array as $key => $arr) {
  if (
     $arr['size'] == $one_dimen_arr['size'] && 
     $arr['colour'] == $one_dimen_arr['colour']
  ) {
    $result = $arr;
    unset($multi_array[$key])  //to remove the matching array
    $multi_array[$key] = $yourUpdatedArr; //to update the values
    break;
  }
}
于 2012-09-30T12:43:26.077 回答
0

尝试这个,

    $result = array();
    foreach ($multi_array as $key=>$arr) {
      if ($arr['size'] == $one_dimen_arr['size'] && $arr['colour'] == $one_dimen_arr['colour']) {
        $i = $key;
        $result = $arr;
        break;
      }

    }
if(isset($i)){$multi_array[$i] = $updatedArray;}
于 2012-09-30T12:42:24.283 回答