1

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']
     ]
   ];

3 Answers 3

3

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
                )

        )

)
Sign up to request clarification or add additional context in comments.

Comments

0

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
            )

    )

)

3 Comments

Do or do not. There is no "try". A good answer will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO.
@JayBlanchard Sorry for that! I have updated my answer.
Your update doesn't explain what you did. Imagine you are having to teach a new programmer what to do. What would you tell him/her?
0

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

4 Comments

Do or do not. There is no "try". A good answer will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO.
How it wrong? can you please explain it more. Thanks
Look precisely to the expected array OP wants, deeply sharply. You will get it, or else, compare with my output and check whether you get your flaws. But if you will make answer like mine, your answer will have no value ;)
@RahulMeshram Yes you were right. I have updated my answer now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.