2011-11-07 53 views
0

如果我沒有在我的孩子中找到我的針,但由於某種原因,我無法讓此工作,無論我嘗試了多少種不同的方式它,有人可能指向我在正確的方向嗎?這是在一個底層深度約爲4的多維數組中拋出的。下面是代碼和數組的一部分可能的樣子。在這種情況下,只有Array [3]應該保留,Array的[1-2]應該被移除。PHP如果未在數組中找到未設置的父項

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);返回錯誤;它將返回其二級陣列針出現英寸

// 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>'; 

回答

0

爲什麼不把你確實需要進入一個新的數組,並取消了整個舊陣列中的數據?