Hello! There is an array of products:

array(4) { [0]=> array(21) { ["product_id"]=> string(2) "54" ... ["category"]=> string(2) "76" ["parent_category"]=> string(2) "62" } [1]=> ... 

You must first split them across the parent_category field, then among those products that have the same parent_category split across the category field. Those. should end up with something like this:

 - Π ΠΎΠ΄ΠΈΡ‚Π΅Π»ΡŒΡΠΊΠ°Ρ катСгория 1 -- катСгория 1 ~Ρ‚ΠΎΠ²Π°Ρ€ ΠΈΠ· ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΈ 1 ~Ρ‚ΠΎΠ²Π°Ρ€ ΠΈΠ· ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΈ 1 -- катСгория 2 ~Ρ‚ΠΎΠ²Π°Ρ€ ΠΈΠ· ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΈ 2 - Π ΠΎΠ΄ΠΈΡ‚Π΅Π»ΡŒΡΠΊΠ°Ρ катСгория 2 -- катСгория 3 ~Ρ‚ΠΎΠ²Π°Ρ€ ΠΈΠ· ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΈ 3 ~Ρ‚ΠΎΠ²Π°Ρ€ ΠΈΠ· Ρ€ΠΎΠ΄ΠΈΡ‚Π΅Π»ΡŒΡΠΊΠΎΠΉ ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΈ 3 

While it turns out to divide them only by parent categories

    1 answer 1

    If we assume that the source of such

     $data = [ ['id' => 1, 'category' => 1, 'parent_category' => 1], ['id' => 2, 'category' => 1, 'parent_category' => 1], ['id' => 3, 'category' => 2, 'parent_category' => 1], ['id' => 4, 'category' => 2, 'parent_category' => 1], ['id' => 5, 'category' => 3, 'parent_category' => 2], ['id' => 6, 'category' => 3, 'parent_category' => 2], ['id' => 7, 'category' => 3, 'parent_category' => 2], ['id' => 8, 'category' => 3, 'parent_category' => 2], ]; $result = []; 

    You must first separate them by parent_category

    Ok, let's do it, it seems not difficult?

     foreach($data as $d){ $result[ $d['parent_category'] ] [] = $d; } 

    Then among those products whose parent_category matches parent_category divided by the category field

    And repeat the same in the loop?

     foreach($result as &$v){ $tmp = []; foreach($v as $p){ $tmp[$p['category']][] = $p; } $v = $tmp; } 
    • Thanks for the response. But in my case, the data is in the form `array (21) {[" product_id "] => string (2)" 54 "... [" category "] => string (2)" 76 "[" parent_category "] => string (2) "62" `but can one come up with something in this case? - r.mcreal
    • @ r.mcreal and what has changed compared to my data? - teran
    • And what is the end result in which variable is stored?) - r.mcreal
    • $result , obviously? - teran
    • I apologize, did not immediately understand after the withdrawal. It seems everything works, thanks! And do not tell me another such thing, if 'parent_category' == 'category' (ie, when the product is only in the parent category) it will be displayed only in the parent category, do I understand correctly? - r.mcreal