Arny
Arny

Reputation: 41

How to convert array from single array to multidimensional array on php

I have an array from excel file like this.

   $array = [
     ['parent 1', '10000', '20000'],
     ['parent 1', '15000', '21000'],
     ['parent 2', '13000', '22000'],
     ['parent 2', '11000', '5000'],
   ];

How to convert array above with php to array like this below

   $array = [
     'parent 1' => [
        ['10000', '20000'],
        ['15000', '21000']
     ],
     'parent 2' => [
        ['13000', '22000'],
        ['11000', '5000']
     ]
   ];

Upvotes: 1

Views: 179

Answers (3)

Rahul
Rahul

Reputation: 18557

You need to loop through array to make first value as key and rest its values.

Version 1

$result = [];
foreach ($arr as $k => $v) {
    $result[$v[0]][] = [ // making first value as key and reset its values
        $v[1], $v[2],
    ];
}
print_r($result);

Demo.

EDIT

Version 2

$result = [];
foreach ($arr as $k => $v) {
    $result[array_shift($v)][] = $v;
}
print_r($result);

Output

Array
(
    [parent 1] => Array
        (
            [0] => Array
                (
                    [0] => 10000
                    [1] => 20000
                )

            [1] => Array
                (
                    [0] => 15000
                    [1] => 21000
                )

        )

    [parent 2] => Array
        (
            [0] => Array
                (
                    [0] => 13000
                    [1] => 22000
                )

            [1] => Array
                (
                    [0] => 11000
                    [1] => 5000
                )

        )

)

Upvotes: 3

Zain Farooq
Zain Farooq

Reputation: 2964

Try this simple and precise solution.

$array = [
   ['parent 1', '10000', '20000'],
   ['parent 1', '15000', '21000'],
   ['parent 2', '13000', '22000'],
   ['parent 2', '11000', '5000'],
 ];

$output = [];
foreach ($array as $key => $value) {
    $output[$value[0]][] = array($value[1],$value[2]);
}
print_r($output);

The parent 1 and parent 2 lie on the 0 index and other entities lie on the 1 and 2 indexes. I have assigned 1 and 2 index values to 0 index in a new array while each iteration

OUTPUT

    Array
(
    [parent 1] => Array
        (
            [0] => Array
                (
                    [0] => 10000
                    [1] => 20000
                )

            [1] => Array
                (
                    [0] => 15000
                    [1] => 21000
                )

        )

    [parent 2] => Array
        (
            [0] => Array
                (
                    [0] => 13000
                    [1] => 22000
                )

            [1] => Array
                (
                    [0] => 11000
                    [1] => 5000
                )

        )

)

Here is the demo

Upvotes: 0

Shashin Bhayani
Shashin Bhayani

Reputation: 1578

Here is a solution:

Demo: https://3v4l.org/XoivL

   $array = [
     ['parent 1', '10000', '20000'],
     ['parent 1', '15000', '21000'],
     ['parent 2', '13000', '22000'],
     ['parent 2', '11000', '5000'],
   ];


    $new_array = array();

    foreach($array as $a) {
       $temp = array_slice($a, 1);

      if(!array_key_exists($a[0], $new_array)) {
          $new_array[$a[0]] = array();
      }

       array_push($new_array[$a[0]], $temp);

    }

    print_r($new_array)

OUTPUT:

Array
(
[parent 1] => Array
    (
        [0] => Array
            (
                [0] => 10000
                [1] => 20000
            )

        [1] => Array
            (
                [0] => 15000
                [1] => 21000
            )

    )

[parent 2] => Array
    (
        [0] => Array
            (
                [0] => 13000
                [1] => 22000
            )

        [1] => Array
            (
                [0] => 11000
                [1] => 5000
            )

    )

)

Upvotes: 0

Related Questions