如果在数组中找不到父元素,取消父元素设置


PHP Unset parent if not found in an array

我有一个问题解除我的父母,如果我没有找到我的针在我的孩子,出于某种原因,我只是不能得到这个工作无论有多少不同的方式我尝试它,有人可能指向我在正确的方向?这是在一个层深度为4的多维数组中抛出的。下面是代码和数组切片的示例。在这种情况下,只有Array[3]应该保留,Array的[1-2]应该被删除。

array (
  1 => 
  array (
    197015 => 
    array (
      345415 => 
      array (
        'options' => 
        array (
          'Name on Credential' => '',
          'Ordination Month' => '',
          'Ordination Day' => '',
          'Ordination Year' => '',
          'Badge Choice?' => '',
        ),
        'comments' => 
        array (
          213354 => '',
        ),
        'products_name' => '',
        'products_quantity' => '',
        'delivery_country' => '',
        'customers_name' => '',
        'delivery_name' => '',
        'delivery_street_address' => '',
        'delivery_city' => '',
        'delivery_postcode' => '',
        'delivery_state' => '',
        'customers_telephone' => '',
      ),
    ),
  ),
  2 => 
  array (
    197014 => 
    array (
      345414 => 
      array (
        'options' => 
        array (
          'Name on Credential' => '',
          'Ordination Month' => '',
          'Ordination Day' => '',
          'Ordination Year' => '',
          'Badge Choice?' => '',
        ),
        'comments' => 
        array (
          213353 => '',
        ),
        'products_name' => '',
        'products_quantity' => '',
        'delivery_country' => '',
        'customers_name' => '',
        'delivery_name' => '',
        'delivery_street_address' => '',
        'delivery_city' => '',
        'delivery_postcode' => '',
        'delivery_state' => '',
        'customers_telephone' => '',
      ),
    ),
  ),
  3 => 
  array (
    197013 => 
    array (
      345412 => 
      array (
        'options' => 
        array (
          'Name on Credential' => '',
          'Ordination Month' => '',
          'Ordination Day' => '',
          'Ordination Year' => '',
        ),
        'comments' => 
        array (
          213352 => '',
        ),
        'products_name' => 'Jedi',
        'products_quantity' => '1',
        'delivery_country' => '',
        'customers_name' => '',
        'delivery_name' => '',
        'delivery_street_address' => '',
        'delivery_city' => '',
        'delivery_postcode' => '',
        'delivery_state' => '',
        'customers_telephone' => '',
      ),
      345413 => 
      array (
        'options' => 
        array (
          '' => '',
        ),
        'comments' => 
        array (
          213352 => '',
        ),
        'products_name' => '',
        'products_quantity' => '',
        'delivery_country' => '',
        'customers_name' => '',
        'delivery_name' => '',
        'delivery_street_address' => '',
        'delivery_city' => '',
        'delivery_postcode' => '',
        'delivery_state' => '',
        'customers_telephone' => '',
      ),
    ),
  ),

下面是我尝试使用的最后一个代码示例,如果您替换unset($k);带返回false;它将返回指针出现在哪个次级数组中。

    // array_search with recursive searching, optional partial matches and optional search by key
    function array_find_r($needle, &$haystack, $partial_matches = false, $search_keys = false) {
        if(!is_array($haystack)) return false;
        foreach($haystack as $key=>&$value) {
            $what = ($search_keys) ? $key : $value;
            if($needle===$what) return $key;
            else if($partial_matches && @strpos($what, $needle)!==false) return $key;
            else if(is_array($value) && array_find_r($needle, $value, $partial_matches, $search_keys)!==false) return $key;
        }
        unset($k);
    }
$tty = array();
foreach($datas as &$k) {
    $tty[] = array_find_r('Jedi', &$k, true, false);
}
$tty=array_filter($tty); rsort($tty);

echo '<pre>'; var_export($datas); echo '</pre>';

为什么不把你需要的数据推到一个新的数组中,并取消整个旧数组的设置呢?