Y
Y
yberpoc2021-08-09 08:41:36
PHP
yberpoc, 2021-08-09 08:41:36

How to sort one big array into categories in the same PHP array?

Through the API, I receive a large array with products and categories at the same time. Categories come on the same level as products. Each category has a key ['id'], and each product has a key ['parent_id']. These keys match. I need to somehow nest all products in categories that have ['parent_id'] the same as ['id'] so that there is no mess when this array is displayed.
I explained very stupidly, but otherwise I do not know how.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2021-08-09
@yberpoc

Something like this, for example
<?php
$data = [
    [
        'id' => 1,
        'parent_id' => 100,
        'name' => 'Стулья'
    ], [
        'id' => 2,
        'parent_id' => 100,
        'name' => 'Столы'
    ], [
        'id' => 5,
        'parent_id' => 1,
        'name' => 'Стул Сакура',
        'price' => 5691,
        'img' => 'lorempixel.com/900/900/?q=5',
        'props' => []
    ], [
        'id' => 9,
        'parent_id' => 2,
        'name' => 'Стол Византия',
        'price' => 5268,
        'img' => 'lorempixel.com/900/900/?q=9',
        'props' => []
    ]
];

function build_tree($data, $id) {
    $result = [];
    foreach ($data as $el) {
        if ($el['parent_id'] == $id) {
            $newEl = $el;
            $childs = build_tree($data, $el['id']);
            if (count($childs) !== 0) {
                $newEl['childs'] = $childs;
            }
            $result[] = $newEl;
        }
    }
    return $result;
}

$tree = build_tree($data, 100);
print_r($tree);

/*
Array (
  [0] => Array (
    [id] => 1
    [parent_id] => 100
    [name] => Стулья
    [childs] => Array (
      [0] => Array (
        [id] => 5
        [parent_id] => 1
        [name] => Стул Сакура
        [price] => 5691
        [img] => lorempixel.com/900/900/?q=5
        [props] => Array ()
      )
    )
  )
  [1] => Array (
    [id] => 2
    [parent_id] => 100
    [name] => Столы
    [childs] => Array (
      [0] => Array (
        [id] => 9
        [parent_id] => 2
        [name] => Стол Византия
        [price] => 5268
        [img] => lorempixel.com/900/900/?q=9
        [props] => Array ()
      )
    )
  )
)
*/

A
Andrey Fedorov, 2021-08-09
@aliencash

loop through all the elements of the array. if the array element has the parent_id key - this is a product, add it to the two-dimensional result array, where the parent_id value will be the first key. If parent_id is not set - it's a category, add the element to the result array.
given the new response format presented, here is the code:

$answer = array(
            array("id" => 1, "parent_id" => 100, "name" => "Стулья"),
            array("id" => 2, "parent_id" => 100, "name" => "Столы"),
            array("id" => 5, "parent_id" => 1, "name" => "Стул Сакура", "price" => 5691, "img" => "lorempixel.com/900/900/?q=5", "props" => array()),
            array("id" => 9, "parent_id" => 2, "name" => "Стол Византия", "price" => 5268, "img" => "lorempixel.com/900/900/?q=9","props" => array())
    );
$result = array();
foreach($answer as $item) {
    if(isset($item['price'])) $result[$item['parent_id']][] = $item;
        else $result[$item['id']] = $item;
}
var_dump($result);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question