Good day. I get with the help of mysqli_fetch_assoc an array of this format

array(4) { array(5) {["cat"]=> "guest" , ["name"]=> "name_one" , ["y"]=> "2016", ["q"]=> "2", ["st"]=> "1"} array(5) {["cat"]=> "guest" , ["name"]=> "name_one" , ["y"]=> "2017", ["q"]=> "3", ["st"]=> "2"} array(5) {["cat"]=> "guest" , ["name"]=> "name_two" , ["y"]=> "2017", ["q"]=> "1", ["st"]=> "1"} array(5) {["cat"]=> "guest" , ["name"]=> "name_two" , ["y"]=> "2017", ["q"]=> "2", ["st"]=> "2"} } 

How to convert it to such a tree in PHP

  {["cat"]=> "guest"{ ["name"]=> "name_one"{ ["y"]=> "2016"{ ["q"]=> "2", ["st"]=>"1" } ["y"]=> "2017"{ ["q"]=> "3", ["st"]=>"2" } } ["name"]=> "name_two"{ ["y"]=> "2016"{ ["q"]=> "2", ["st"]=>"1" } ["y"]=> "2017"{ ["q"]=> "3", ["st"]=>"2" } } } } 

    3 answers 3

    As far as I know the context of your task, you better not save the keys in the structure ( name , y ...):

    Feeddle

     $rows = [ ['cat' => 'guest', 'name' => 'name_one', 'y' => '2016', 'q' => '2', 'st' => 1], ['cat' => 'guest', 'name' => 'name_one', 'y' => '2017', 'q' => '3', 'st' => 2], ['cat' => 'guest', 'name' => 'name_two', 'y' => '2017', 'q' => '1', 'st' => 1], ['cat' => 'guest', 'name' => 'name_two', 'y' => '2017', 'q' => '2', 'st' => 2], ]; $data = []; foreach ($rows as $row) { $link = &$data; foreach (['cat', 'name', 'y'] as $field) { if (!isset($link[$row[$field]])) $link[$row[$field]] = []; $link = &$link[$row[$field]]; } $link[$row['q']] = $row['st']; unset($link); } print_r($data); 

    Full answer to the related question.


    If you need to keep the keys, you will have to enter an extra level to separate the values ​​of the current level from the list of descendants:

    Feeddle

     $data = []; foreach ($rows as $row) { $link = &$data; foreach (['cat', 'name', 'y'] as $field) { if (!isset($link[$field])) $link[$field] = []; if (!isset($link[$field][$row[$field]])) $link[$field][$row[$field]] = []; $link = &$link[$field][$row[$field]]; } $link[] = ['q' => $row['q'], 'st' => $row['st']]; unset($link); } echo json_encode($data); 

    Result:

     { "cat":{ "guest":{ "name":{ "name_one":{ "y":{ "2016":[{"q":"2","st":1}], "2017":[{"q":"3","st":2}] } }, "name_two":{ "y":{ "2017":[{"q":"1","st":1},{"q":"2","st":2}]}}}}}} 

      The array should not be obtained using mysqli, but using PDO.

      Then you won't have to rebuild anything, PDO will do everything for us.

       $stmt = $pdo->query("SELECT name, y, q, st FROM table"); $data = $stmt->fetchAll(PDO:FETCH_GROUP); 

      will return to us an array already grouped by name .

      • one
        FETCH_GROUP will return a tree? Quote: Как преобразовать его в такое дерево - user236014

      If I understand you correctly, you can try this method that separates the keys:

       <?php $assoc = array( array( "cat" => "guest1", "name" => "name_one1", "y" => "2000", "q" => "my1", "st" => 1 ), array( "cat" => "guest2", "name" => "name_one2", "y" => "2001", "q" => "my2", "st" => 2 ), ); $three = array(); for ($i = 0; $i < count($assoc); $i++) { foreach ($assoc[$i] as $key => $value) { $three[$key][] = $value; } } print_r($three); ?> 
      • Thank you for your answer, but in this case you get a rebuilt "array of arrays" of this format {guest, guest, guest, guest} {name_one, name_one, name_two, name_two} {2016, 2017, 2017, 2017} {2, 3, 1 , 2} - Kirill