删除关联数组中的父元素

时间:2019-03-30 23:45:34

标签: php arrays foreach

问题:

我想删除条件中数组的父元素。

输入

$myArray = array (
    0 =>
    array (
      'ulStatic' =>
      array (
        0 =>
        array (
          'day' => '2019-03-30 18:30:00',
          'id' => '7',
          'origin' => 'intentions',
        ),
      ),
      'ulDynamic' =>
      array (
        0 =>
        array (
          'day' => '2019-03-30 18:30:00',
          'id' => '5',
          'origin' => 'intentions',
        ),
      ),
      'ulCreatedDynamic' =>
      array (
      ),
    ),
    1 =>
    array (
      'ulStatic' =>
      array (
        0 =>
        array (
          'day' => '2019-03-31 09:30:00',
          'id' => '8',
          'origin' => 'intentions',
        ),
      ),
      'ulDynamic' =>
      array (
      ),
      'ulCreatedDynamic' =>
      array (
      ),
    ),
    2 =>
    array (
      'ulStatic' =>
      array (
      ),
      'ulDynamic' =>
      array (
      ),
      'ulCreatedDynamic' =>
      array (
        0 =>
        array (
          'day' => '2019-04-02 11:15:00',
          'id' => '275',
          'origin' => 'obseques',
        ),
        1 =>
        array (
          'day' => '2019-04-02 11:15:00',
          'id' => '277',
          'origin' => 'obseques',
        ),
      ),
    ),
  );

在此数组中,我想在名为ulCreatedDynamic的关联数组中删除包含id = 275day = '2019-04-02 11:15:00'的键;

因此,此密钥必须删除:

0 =>
    array (
      'day' => '2019-04-02 11:15:00',
      'id' => '275',
      'origin' => 'obseques',
    ),

尝试:

$id = 275;
$day = "2019-04-02 11:15:00";

foreach($myArray as $array):
    foreach($array as $ul => $v):
        if($ul == "ulCreatedDynamic"):
            foreach($v as $elementKey => $element):
                if($element['day'] == $day && $element['id'] == $id):
                    unset($v[$elementKey]);
                endif;
            endforeach;
        endif;
    endforeach;
endforeach;

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

foreach($some_array as $item)将在任何项目更改后即时复制整个阵列。您需要使用&运算符来处理参考。

foreach($myArray as &$array):
  foreach($array as $ul => &$v):

答案 1 :(得分:0)

我不确定这是否是您想要实现的目标。我敢肯定,有更简单的方法可以解决您的问题。

可能不是在以下行中从主阵列$myArray取消设置:unset($v[$elementKey]);

$myArray = array(
    0 => array(
        'ulStatic' => array(
            0 => array(
                'day' => '2019-03-30 18:30:00',
                'id' => '7',
                'origin' => 'intentions',
            ),
        ),
        'ulDynamic' => array(
            0 => array(
                'day' => '2019-03-30 18:30:00',
                'id' => '5',
                'origin' => 'intentions',
            ),
        ),
        'ulCreatedDynamic' => array(
        ),
    ),
    1 => array(
        'ulStatic' => array(
            0 => array(
                'day' => '2019-03-31 09:30:00',
                'id' => '8',
                'origin' => 'intentions',
            ),
        ),
        'ulDynamic' => array(
        ),
        'ulCreatedDynamic' => array(
        ),
    ),
    2 => array(
        'ulStatic' => array(
        ),
        'ulDynamic' => array(
        ),
        'ulCreatedDynamic' => array(
            0 => array(
                'day' => '2019-04-02 11:15:00',
                'id' => '275',
                'origin' => 'obseques',
            ),
            1 => array(
                'day' => '2019-04-02 11:15:00',
                'id' => '277',
                'origin' => 'obseques',
            ),
        ),
    ),
);

$id = 275;
$day = "2019-04-02 11:15:00";

foreach ($myArray as $k => $array):
    foreach ($array as $ul => $v):
        if ($ul == "ulCreatedDynamic"):
            foreach ($v as $elementKey => $element):
                if ($element['day'] == $day && $element['id'] == $id):
                    // unset($v[$elementKey]);
                    unset($myArray[$k][$ul][$elementKey]);
                endif;
            endforeach;
        endif;
    endforeach;
endforeach;
var_dump($myArray);

$ myArray:

array(3) {
  [0]=>
  array(3) {
    ["ulStatic"]=>
    array(1) {
      [0]=>
      array(3) {
        ["day"]=>
        string(19) "2019-03-30 18:30:00"
        ["id"]=>
        string(1) "7"
        ["origin"]=>
        string(10) "intentions"
      }
    }
    ["ulDynamic"]=>
    array(1) {
      [0]=>
      array(3) {
        ["day"]=>
        string(19) "2019-03-30 18:30:00"
        ["id"]=>
        string(1) "5"
        ["origin"]=>
        string(10) "intentions"
      }
    }
    ["ulCreatedDynamic"]=>
    array(0) {
    }
  }
  [1]=>
  array(3) {
    ["ulStatic"]=>
    array(1) {
      [0]=>
      array(3) {
        ["day"]=>
        string(19) "2019-03-31 09:30:00"
        ["id"]=>
        string(1) "8"
        ["origin"]=>
        string(10) "intentions"
      }
    }
    ["ulDynamic"]=>
    array(0) {
    }
    ["ulCreatedDynamic"]=>
    array(0) {
    }
  }
  [2]=>
  array(3) {
    ["ulStatic"]=>
    array(0) {
    }
    ["ulDynamic"]=>
    array(0) {
    }
    ["ulCreatedDynamic"]=>
    array(1) {
      [1]=>
      array(3) {
        ["day"]=>
        string(19) "2019-04-02 11:15:00"
        ["id"]=>
        string(3) "277"
        ["origin"]=>
        string(8) "obseques"
      }
    }
  }
}