-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

8
  • 2
    A simple foreach will do the job. What have you attempted so far? Commented Mar 27, 2020 at 14:19
  • array(2) { [0] => array(2) { ["option"] => string(5) "64310" ["choices"] => array(2){ ["choice"] => string(6) "221577" ["choice"] => string(6) "221578" } } } Commented Mar 27, 2020 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. Commented Mar 27, 2020 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. Commented Mar 27, 2020 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"]]); } } Commented Mar 27, 2020 at 14:28

1 Answer 1

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!

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

Comments

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.