-2

i have an array of arrays like this one

array(4) {
  [0] => array(2) {
    ["option"] => string(5) "64310"
    ["choice"] => string(6) "221577"
  }
  [1] => array(2) {
    ["option"] => string(5) "64310"
    ["choice"] => string(6) "221578"
  }
  [2] => array(2) {
    ["option"] => string(5) "64305"
    ["choice"] => string(6) "221538"
  }

}

i want to obtain a result like this one

array(2) {
  [0] => array(2) {
    ["option"] => string(5) "64310"
    ["choices"] => array(2){
       ["choice"] => string(6) "221577"
       ["choice"] => string(6) "221578"
    }
  }
}

how can i proceed, thank you in advance

  • 2
    A simple foreach will do the job. What have you attempted so far? – El_Vanja Mar 27 '20 at 14:19
  • `array(2) { [0] => array(2) { ["option"] => string(5) "64310" ["choices"] => array(2){ ["choice"] => string(6) "221577" ["choice"] => string(6) "221578" } } }` – 치노장인 Mar 27 '20 at 14:21
  • You just pasted your desired result again in the comments. Please edit the question to add the code you've written that tries to achieve this. – El_Vanja Mar 27 '20 at 14:22
  • There's a small "edit" link at the bottom of your question which you can use to add some more information to it. Comments aren't suitable for multiline code. – El_Vanja Mar 27 '20 at 14:26
  • `$result=$array[0]; $options = []; foreach ($array as $value){ if(isset($result[$value["option"]]) && !in_array($result[$value["option"]], $options) ){ array_push($options, $result[$value["option"]]); } }` – 치노장인 Mar 27 '20 at 14:28
  • thanks for your reply in advance :) – 치노장인 Mar 27 '20 at 14:29
  • what you have attempted and why it did not work? Read more about how to ask a good question : https://stackoverflow.com/help/how-to-ask – Ramesh Pareek Mar 27 '20 at 14:31
  • Do not put code in comments. **Edit** your **question**, as already requested. https://stackoverflow.com/posts/60887786/edit – ADyson Mar 27 '20 at 14:45

1 Answers1

1

Something like this will help you achieve the desired result;

<?php

    $data = [
        [
            'option' => '64310',
            'choice' => '221577'
        ],
        [
            'option' => '64310',
            'choice' => '221578'
        ],
        [
            'option' => '64305',
            'choice' => '221538'
        ]
    ];

    $res = [];
    foreach($data as $d) {

        // Check if we've already got this option
        // Note the '&' --> Check link below
        foreach($res as &$r) {
            if (isset($r['option']) && $r['option'] === $d['option']) {

                // Add to 'choices'
                $r['choices'][] = $d['choice'];

                // Skip the rest of both foreach statements
                continue 2;
            }
        }

        // Not found, create
        $res[] = [
            'option' => $d['option'],
            'choices' => [ $d['choice'] ],
        ];
    };

    print_r($res);

& --> PHP "&" operator

Array
(
    [0] => Array
        (
            [option] => 64310
            [choices] => Array
                (
                    [0] => 221577
                    [1] => 221578
                )
        )
    [1] => Array
        (
            [option] => 64305
            [choices] => Array
                (
                    [0] => 221538
                )
        )
)

Try online!

0stone0
  • 34,288
  • 4
  • 39
  • 64
  • @치노장인 Glad it worked! Did you follow the `&` link? It's important material haha! Also, please [accept the answer](https://stackoverflow.com/help/someone-answers) that you believe is the best solution to your problem so other SO users can find the solution ;) – 0stone0 Mar 27 '20 at 14:58