A
A
AlpineMilk2019-05-09 14:46:32
PHP
AlpineMilk, 2019-05-09 14:46:32

How to group one field of multidimensional array?

There is an array like:

array:4 [▼
  0 => array:7 [▼
    "answer_schema_id" => "1"
    "content" => "odpowiedz 1"
    "locale" => "pl"
    "points" => "3"
    "answerKey" => "1"
    "answerElementOrder" => "1"
    "image" => "5cd143beba821203428009.jpg"
  ]
  1 => array:7 [▼
    "answer_schema_id" => "1"
    "content" => "answer 1"
    "locale" => "en"
    "points" => "3"
    "answerKey" => "1"
    "answerElementOrder" => "1"
    "image" => "5cd143beba821203428009.jpg"
  ]
  2 => array:7 [▼
    "answer_schema_id" => "2"
    "content" => "answer 2"
    "locale" => "en"
    "points" => "2"
    "answerKey" => "2"
    "answerElementOrder" => "2"
    "image" => "5cd143bebcdfc405126844.jpg"
  ]
  3 => array:7 [▼
    "answer_schema_id" => "2"
    "content" => "odpowiedz 2"
    "locale" => "pl"
    "points" => "2"
    "answerKey" => "2"
    "answerElementOrder" => "2"
    "image" => "5cd143bebcdfc405126844.jpg"
  ]
]

You need to get the following array:
array:4 [▼
  0 => array:7 [▼
    "answer_schema_id" => "1"
    "content" => [
        "pl" => 'odpowiedz 1',
        "en" => 'answer 1'    
     ]
    "points" => "3"
    "answerKey" => "1"
    "answerElementOrder" => "1"
    "image" => "5cd143beba821203428009.jpg"
  ],
1 => array:7 [▼
    "answer_schema_id" => "2"
     "content" => [
        "pl" => 'odpowiedz 2',
        "en" => 'answer 2'    
     ]
    "points" => "2"
    "answerKey" => "2"
    "answerElementOrder" => "2"
    "image" => "5cd143bebcdfc405126844.jpg"
  ]
]

But I get frankly some kind of game.
Here is my code:
$new = [];

foreach ($data as $key => $datum){
            if(empty($new)){
                $new[] = $datum;
                unset($new[0]['content']);
            }

            foreach ($new as $keyz => $item){
                if($item['answer_schema_id'] == $datum['answer_schema_id']){
                    $new[$keyz]['content'][$datum['locale']] = $datum['content'];
                }
            }
        }

Please help me to solve this problem

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2019-05-09
@AlpineMilk

$arr = [
  [
    "answer_schema_id" => "1",
    "content" => "odpowiedz 1",
    "locale" => "pl",
    "points" => "3",
    "answerKey" => "1",
    "answerElementOrder" => "1",
    "image" => "5cd143beba821203428009.jpg",
  ], [
    "answer_schema_id" => "1",
    "content" => "answer 1",
    "locale" => "en",
    "points" => "3",
    "answerKey" => "1",
    "answerElementOrder" => "1",
    "image" => "5cd143beba821203428009.jpg",
  ], [
    "answer_schema_id" => "2",
    "content" => "answer 2",
    "locale" => "en",
    "points" => "2",
    "answerKey" => "2",
    "answerElementOrder" => "2",
    "image" => "5cd143bebcdfc405126844.jpg",
  ], [
    "answer_schema_id" => "2",
    "content" => "odpowiedz 2",
    "locale" => "pl",
    "points" => "2",
    "answerKey" => "2",
    "answerElementOrder" => "2",
    "image" => "5cd143bebcdfc405126844.jpg",
  ]
];
$result = [];
$ptr = [];
foreach ($arr as $el) {
  if (!isset($ptr[$el['answer_schema_id']])) {
    $res[] = [
      "answer_schema_id" => $el['answer_schema_id'],
      "content" => [],
      "points" => $el['points'],
      "answerKey" => $el['answerKey'],
      "answerElementOrder" => $el['answerElementOrder'],
      "image" => $el['image']
    ];
    $ptr[$el['answer_schema_id']] = count($res)-1;
  }
  $res[$ptr[$el['answer_schema_id']]]['content'][$el['locale']] = $el['content'];
}
var_dump($res);

array(2) {
  [0] => array(6) {
    ["answer_schema_id"] => string(1) "1"
    ["content"] => array(2) {
      ["pl"] => string(11) "odpowiedz 1"
      ["en"] =>string(8) "answer 1"
    }
    ["points"] => string(1) "3"
    ["answerKey"] => string(1) "1"
    ["answerElementOrder"] => string(1) "1"
    ["image"] => string(26) "5cd143beba821203428009.jpg"
  }
  [1] => array(6) {
    ["answer_schema_id"] => string(1) "2"
    ["content"] => array(2) {
      ["en"] => string(8) "answer 2"
      ["pl"] => string(11) "odpowiedz 2"
    }
    ["points"] => string(1) "2"
    ["answerKey"] => string(1) "2"
    ["answerElementOrder"] => string(1) "2"
    ["image"] => string(26) "5cd143bebcdfc405126844.jpg"
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question