2016-05-13 116 views
3
$array = ["farm"=> 
       [ 
       "horse"=> 
       [ 
        "rabbit"=> 
        [ 
         "fred1"=> "fred1", 
         "fred2"=> "fred2", 
         "fred3"=> "fred3", 
         "fred4"=> "fred4" 
        ], 
       "raccoon"=> 
        ["frida"=> "frida"] 
        ] 
       ] 
    ]; 

我想從創建一個數組,我每個循環:如何從foreach循環創建關聯數組?

$keySearch = "o"; 

    function createList($array, $keySearch, $path) { 
     foreach ($array as $key => $item) { 
      $basePath = $path === null ? $key : $path. "/" . $key; 
       if(is_array($item)){ 
        if (stripos($key, $keySearch) !== false){ 
        $a['key'] = $key ; 
        $b['basePath'] = $basePath; 
        $result[] = array_merge_recursive ($a, $b);    
        } 
       createList($item, $keySearch, $basePath); 
       }  
     } 
    print_r($result); 
    } 
    createList($array, $keySearch, ''); 

我的結果是:

Array 
(
    [0] => Array 
     (
      [key] => horse 
      [basePath] => farm/horse 
     ) 

) 
Array 
(
    [0] => Array 
     (
      [key] => raccoon 
      [basePath] => farm/horse/raccoon 
     ) 

) 

什麼其實我想到的是:

Array 
    (
     [0] => Array 
      (
       [key] => horse 
       [basePath] => farm/horse 
      ) 
     [1] => Array 
      (
       [key] => raccoon 
       [basePath] => farm/horse/raccoon 
      ) 

    ) 

https://eval.in/571065

+0

什麼是您的預期輸出之間的差異? – splash58

+0

@ splash58我的輸出是兩個數組,但我只需要一個數組 – Jarla

+0

什麼是$ keySearch? – splash58

回答

1

我改進代碼:

function createList($array, $keySearch, $path=null) { 
    $result = []; 
    foreach ($array as $key => $item) { 
     $basePath = $path === null ? $key : $path. "/" . $key; 
      if(is_array($item)){ 
       if (stripos($key, $keySearch) !== false) { 
       $result[] = ['key' => $key, 'basePath' => $basePath];    
       } 
      $result = array_merge($result, createList($item, $keySearch, $basePath)); 
      }  
    } 
return $result; 
} 

$keySearch = 'o'; 
$res = createList($array, $keySearch); 
print_r($res); 

demo

UPD:如果你發現所有的鑰匙,不僅是那些指向數組,改變代碼,以便:

function createList($array, $keySearch, $path=null) { 
       $result = []; 
    foreach ($array as $key => $item) { 
     $basePath = $path === null ? $key : $path. "/" . $key; 
     if (stripos($key, $keySearch) !== false) 
      $result[] = ['key' => $key, 'basePath' => $basePath];    
     if(is_array($item)) 
      $result = array_merge($result, createList($item, $keySearch, $basePath)); 
    } 
return $result; 
} 

$keySearch = 'fr'; 
$res = createList($array, $keySearch); 
print_r($res); 

demo

+0

這工作得很好! – Jarla

+0

你是什麼意思由'點數組'?例如 – Jarla

+1

,如果在數組中有key =>值,它將不會被第一個代碼選中,並且將被第2個。但是$ key => [array]將在兩種情況下 – splash58

1

遞歸算法求解:

<?php 

     $array = ["farm"=> 
       [ 
        "horse"=> 
        [ 
         "rabbit"=> 
          [ 
           "fred1"=> "fred1", 
           "fred2"=> "fred2", 
           "fred3"=> "fred3", 
           "fred4"=> "fred4" 
          ], 
         "raccoon"=> 
          ["john"=> "john"] 
        ] 
       ] 
     ]; 


      $jl  = array(); 
      $root = ""; 

      function walkJarLarsData($ar, $search, $base="base-path", $pKey=""){ 
       global $jl, $root; 
       if(!stristr($root, $base)){ 
        $root .= $base; 
       } 

       foreach($ar as $key=>$val){ 
        $pKey  = $pKey?"{$pKey}":""; 
        if (preg_match("#" . preg_quote($search) . "#", $key)) { 
         $jl[] = array(
          "key"  => $key, 
          "basePath" => $root . "/{$pKey}/{$key}", 
         ); 
        } 
        if(is_array($val)){ 
         walkJarLarsData($val, $search, $base, $key);  
        } 
       } 


       return $jl; 
      } 

      var_dump(walkJarLarsData($array, "o")); 
1

您可以使用與添加ref屬性相同的函數,並將數組追加到該屬性中。

$array = ["farm"=> 
       [ 
       "horse"=> 
       [ 
        "rabbit"=> 
        [ 
         "fred1"=> "fred1", 
         "fred2"=> "fred2", 
         "fred3"=> "fred3", 
         "fred4"=> "fred4" 
        ], 
       "raccoon"=> 
        ["frida"=> "frida"] 
        ] 
       ] 
    ]; 

function createList($array, $keySearch, $path, &$out) { 
    foreach ($array as $key => $item) { 
     $basePath = $path === null ? $key : $path. "/" . $key; 
     if(is_array($item)){ 
      if (stripos($key, $keySearch) !== false){ 
       $a['key'] = $key ; 
       $b['basePath'] = $basePath; 
       $out[] = array_merge_recursive ($a, $b);    
      } 
      createList($item, $keySearch, $basePath, $out); 
     }  
    } 
} 

$keySearch = "o"; 
createList($array, $keySearch, '', $result); 
print_r($result); 

演示:https://eval.in/571224

1

無疑,這是解決方案,您尋找:

<?php 

    $arBase  = array(); 
    $kern  = ""; 

    function arrayRecurse($ar, $search, $mainPath="base-path", $cue=""){ 
     global $arBase, $kern; 
     $kern = !(stristr($kern, $mainPath))? $kern.= $mainPath : $kern; 

     foreach($ar as $key=>$val){ 
      $cue  = $cue?"{$cue}":""; 
      if (preg_match("#" . preg_quote($search) . "#", $key)) { 
       $arBase[] = array(
        "key"  => $key, 
        "basePath" => $kern . "/{$cue}/{$key}", 
       ); 
      } 
      if(is_array($val)){ 
       arrayRecurse($val, $search, $mainPath, $key); 
      } 
     } 

     return $arBase; 
    } 

    var_dump(arrayRecurse($array, "fr"));