-1

I want to show children category inside parent category

I'm using $categories to get arrays like this:

[5] => Array
(
    [id] => 5
    [parent_id] => 4
)
 [4] => Array
(
    [id] => 5
    [parent_id] => 0
)
 [3] => Array
(
    [id] => 3
    [parent_id] => 1
)
 [1] => Array
(
    [id] => 1
    [parent_id] => 0
)

And i'm using this code to check if category have a children

if (array_search($category['id'], array_column($categories, 'parent_id'))) {
    echo "This category has children";
}

get arrays from database

$categories = load_categories(array('db_table' => 'pm_categories'));

enter image description here

So can help me to get id of array where find parent_id

Boyzen
  • 49
  • 1
  • 7
  • I don't see what the problem is. What are you expecting? What is the problem? What is the question? – Andreas Sep 04 '18 at 03:27
  • it is not clear to me, show us how `$categories` array looks, or clarifies if the array shown is `$categories` – Mario Sep 04 '18 at 03:28
  • par example i'm in category where have id 4, and i'm search if find children with same id of current category and find it, good, now my question is how get ID of array where find parent_id – Boyzen Sep 04 '18 at 03:32
  • @user615274 i'm put $categories arrays in my question, i'm get all arrays with $print_r($categories); – Boyzen Sep 04 '18 at 03:34
  • Please show us the array declaration, for example `$categories = [['parent...' => '...', ...]]`, some example data would be fine – Mario Sep 04 '18 at 03:42
  • arrays get from database, i'm update my question – Boyzen Sep 04 '18 at 03:46

3 Answers3

2

If I understand you correctly you are trying to search a multidimensional array and get a subset of that array returned. This other question covered that issue nicely. How to search by key=>value in a multidimensional array in PHP

In your case just check if the "parent_id" has a value instead of equaling a value and your resulting array will have all child categories.

Hope this helps!

TitaniaAnn
  • 21
  • 4
1

array_search() returns the index of the element that was found. You can save that in a variable, then use it to index the array.

$index = array_search($category['id'], array_column($categories, 'parent_id'));
if ($index !== false) {
    $categories_indexed = array_values($categories);
    $id = $categories_indexed[$index]['id'];
}

If you need to get all the categories with the parent ID, not just the first, use array_filter()

$matching_categories = array_filter($categories, function ($cat) use ($category) {
    return $cat['parent_id'] == $category['id'];
});
$ids = array_column($matching_categories, 'id');

BTW, you should never use if (array_search(...)). If the element it finds is the first element of the array, it will return index 0. This is considered to be false in an if statement, so it will incorrectly treat this as not found.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • thank you sir for help and tips, your code is work and can get another keys in array but big problem is It works incorrectly, $index give me wrong array id, i don't know why but when i try only echo $index it's give me another id array not array where find valid parent_id value – Boyzen Sep 04 '18 at 04:47
  • The problem is that `$categories` is an associative array, but `array_column()` does use those keys in its result, it returns an indexed array. So the index that's returned by `array_search()` won't be the correct associative key. I've updated the answer to use `array_values` to get an array with the matching indexes. – Barmar Sep 04 '18 at 04:57
  • thank you very mush, now it's return correct value, but i have another problem, it's return only one category, if i have multi sub-categories it's return only one, how i can return all sub-categories found, maybe with foreach but i don't know how exactly, can you help me more sir – Boyzen Sep 04 '18 at 05:03
  • Use `array_filter()`. – Barmar Sep 04 '18 at 05:05
  • thank you but i don't know how, can you give me full syntax please – Boyzen Sep 04 '18 at 05:06
  • I've added it to the answer. But why don't you know how? – Barmar Sep 04 '18 at 05:08
  • i'm still learning php not pro sir, thank you again i'll test your code and will tell you what happened – Boyzen Sep 04 '18 at 05:14
  • i don't know what say, thank you very mush, it's works excellently – Boyzen Sep 04 '18 at 05:19
1

You don't need array_search to check if the current category has childs:

<?php

$category_childs = array_filter($categories, function($_category) use ($category){
    return $_category['parent_id'] == $category['id'];
});

if($category_childs){
    echo 'This category has childs';
}

$category_childs_ids = array_column($category_childs, 'id');
Kerkouch
  • 1,446
  • 10
  • 14