@AlpineMilk

Как сгрупировать одно поле многомерного массива?

Есть массив вида:
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"
  ]
]


Нужно получить следующий массив:
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"
  ]
]


Но у меня получается откровенно какая-то дич.
Вот мой код:
$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'];
                }
            }
        }


Прошу помощи в решении данной проблемы
  • Вопрос задан
  • 84 просмотра
Решения вопроса 1
Rsa97
@Rsa97
Для правильного вопроса надо знать половину ответа
$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"
  }
}
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы