Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create an associative array from a foreach loop?

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

I want to create an array from my for each loop:

$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, '');

My result is:

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

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

)

What I actually expect is:

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

    )

https://eval.in/571065

like image 877
peace_love Avatar asked Apr 01 '26 09:04

peace_love


2 Answers

i improved your code:

 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: if you find all keys, not only those which points array, change code so:

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

like image 120
splash58 Avatar answered Apr 03 '26 23:04

splash58


You can use your same function with addition ref attribute, and append array into that attribute.

$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);

Demo: https://eval.in/571224

like image 43
kamal pal Avatar answered Apr 03 '26 21:04

kamal pal