xitas
xitas

Reputation: 1164

creating a multidimensional array in php through loop

I am trying to create a array with three dimensions. first let me explain how my data is coming. my current data

Array
(
    [0] => stdClass Object
        ([1] => A [2] => a [3] => *)
    [1] => stdClass Object
        ([1] => A [2] => a [3] => $)
    [2] => stdClass Object
        ([1] => B [2] => a [3] => %)
    [3] => stdClass Object
        ([1] => B [2] => b [3] => @)
    [4] => stdClass Object
        ([1] => B[2] => c[3] => x)

What i want:

   Array
    (
      [0] => A(
                [0] => a(
                          [0] => *
                          [1] => $
                        )
               )

       [1] => B
            (
                [0] => a(
                           [0] => %
                        )
                [1] => b(
                           [0] => $
                        )
                [2] => c(
                           [0] => x
                        )
            )

So first i have to remove duplicate heading make array of them and then add more array values SO here i have done so far and don't know where to go

Code:

        $query = $this->db->get();
        $raw_data = $query->result(); 
        //$array = json_decode(json_encode($data), true);
        $data = array();
        echo "<pre>";
        foreach ($raw_data as $key => $value) {
                if (in_array($value->DEPT, $data) != 1) {
                        $data[] = $value->DEPT;       
                }
        }
        //for here no idea what to do
        foreach ($raw_data as $key => $value) {
                $d_key = array_search($value->DEPT, $data[$d_key]);
                if (in_array($value->CAT, $data) != 1) {
                        $data[$d_key] = [$value->CAT]; 
                }
        }
        print_r($data);
        echo "</pre>";

I have remove duplication new i want to add sub heading in main heading array

Upvotes: 1

Views: 36

Answers (1)

Ethan
Ethan

Reputation: 4375

Try this:

$raw_data = [
    ['A', 'a', '*'],
    ['A', 'a', '$'],
    ['B', 'a', '%'],
    ['B', 'b', '@'],
    ['B', 'c', 'x'],
];

$data = [];
foreach ($raw_data as $row) {
    if (!array_key_exists($row[0], $data)) $data[$row[0]] = [];
    if (!array_key_exists($row[1], $data[$row[0]])) $data[$row[0]][$row[1]] = [];
    $data[$row[0]][$row[1]][] = $row[2];
}
print_r($data);

Outputs:

Array
(
    [A] => Array
        (
            [a] => Array
                (
                    [0] => *
                    [1] => $
                )

        )

    [B] => Array
        (
            [a] => Array
                (
                    [0] => %
                )

            [b] => Array
                (
                    [0] => @
                )

            [c] => Array
                (
                    [0] => x
                )

        )

)

It basically just adds a new child array for the heading and subheading if it doesn't already exist, and then sets the subsubheading to the value of the path of the heading and subheading

Upvotes: 1

Related Questions