0

Hello i have an multidimensional array each of the array contains key of "id_category"

I am trying to store value of key "id_category" while loop through the multidimensional array.

Here is the array & php compiler

https://paiza.io/projects/H8FwXLAZc438-XA-hnmNHQ?language=php

$save_id_category = [];
foreach($array as $ct){
    $save_id_category[] = $ct['id_category'];
}
print_r($save_id_category); // this gets me only one record.

I can loop through all array & save data but that would be not good idea.

Please let me know if we have a better approach for this. Thanks

  • You probably want [`array_column`](https://www.php.net/manual/en/function.array-column) – Nick Aug 10 '20 at 08:18
  • I had used = print_r(array_column($children, 'id_category')); but gives only one record –  Aug 10 '20 at 08:19
  • Please do check the array data here - https://paiza.io/projects/H8FwXLAZc438-XA-hnmNHQ?language=php –  Aug 10 '20 at 08:20
  • Please use `var_export` instead of `print_r` for your sample data, then it can be used directly in PHP. – Nick Aug 10 '20 at 08:24
  • Updated array, Thanks –  Aug 10 '20 at 08:26

1 Answers1

0

You can use a recursive function to find all the id_category values, first getting all the values at the current level using array_column, then iterating over the children of each array element to add their categories to the list:

function get_categories($array) {
    $categories = array_column($array, 'id_category');
    foreach ($array as $ct) {
        $categories = array_merge($categories, get_categories($ct['children'] ?? array()));
    }
    return $categories;
}

print_r(get_categories($arrayone));

Output (for your sample data):

Array
(
    [0] => 12
    [1] => 13
    [2] => 15
    [3] => 16
    [4] => 48
    [5] => 68
    [6] => 58
    [7] => 282
    [8] => 17
    [9] => 18
    [10] => 85
    [11] => 52
    [12] => 53
    [13] => 86
    [14] => 54
    [15] => 55
    [16] => 56
    [17] => 217
    [18] => 275
    [19] => 69
    [20] => 70
    [21] => 71
    [22] => 278
    [23] => 59
    [24] => 60
    [25] => 61
    [26] => 283
)
Nick
  • 138,499
  • 22
  • 57
  • 95
  • I am generating the "arrayone" from here $arrayone = Category::getNestedCategories(19, 1); Is their any way we can loop through the arrayone function and change argument i.e "19" ? –  Aug 10 '20 at 08:42
  • @newphpdev I'm not sure what you mean? perhaps something like `$categories = [1, 5, 12]; foreach ($categories as $category) { $arrayone = Category::getNestedCategories($category, 1); }`? – Nick Aug 10 '20 at 08:47
  • I am trying to form a single array by merging two array keys into one –  Aug 10 '20 at 08:49
  • This - Array ( [0] => Array ( [id] => 5 ) [1] => Array ( [id] => 8 ) ) –  Aug 10 '20 at 08:50
  • To This - Array ( [0] => 5 ) ( [1] => 8 ) –  Aug 10 '20 at 08:51
  • `array_column($array, 'id')` will do what you want – Nick Aug 10 '20 at 08:54
  • Hello, please take a look at this https://paiza.io/projects/G-nyByVA-rg5kwS1bcHJ0g?language=php –  Aug 10 '20 at 08:58
  • @newphpdev lots of ideas on how to flatten an array [here](https://stackoverflow.com/questions/526556/how-to-flatten-a-multi-dimensional-array-to-simple-one-in-php) – Nick Aug 10 '20 at 09:03