我正在尝试在加入两个sql表后检索记录。结果数组如下:
Array
(
[0] => Array
(
[optionId] => 150
[price] => 2.00
[name] => 1 Ice
[languageId] => 4
)
[1] => Array
(
[optionId] => 150
[price] => 2.00
[name] => Ek Burph
[languageId] => 1
)
[2] => Array
(
[optionId] => 151
[price] => 5.00
[name] => 2 Ice
[languageId] => 4
)
[3] => Array
(
[optionId] => 151
[price] => 5.00
[name] => Do Burph
[languageId] => 1
)
)
我想将此数组转换为以下格式,以便在我的应用中轻松解析。
Array
(
[0] => Array
(
[optionId] => 150
[price] => 2.00
[languages]=>array(
[0]=>array(
[name] => 1 Ice
[languageId] => 4
)
[1]=>array(
[name] => Ek Burph
[languageId] => 1
)
)
)
[1] => Array
(
[optionId] => 151
[price] => 5.00
[languages]=>array(
[0]=>array(
[name] => 2 Ice
[languageId] => 4
)
[1]=>array(
[name] => Do Burph
[languageId] => 1
)
)
)
)
即。我需要在PHP中对属于同一optionId的项进行分组,并且无法弄清楚如何。请帮忙。
答案 0 :(得分:1)
自己做完了:
$array2 = array();
$f = 0;
if($array1){
foreach($array1 as $option){
$i = 0;
foreach($array2 as $option2){
if($option2['optionId'] == $option['optionId']){
$array2[$i]['language'][] = array('name'=>$option['name'],'languageId'=>$option['languageId']) ;
$f = 1;
}
$i++;
}
if(!$f){
$array2[$i]['optionId'] = $option['optionId'];
$array2[$i]['price'] = $option['price'];
$array2[$i]['language'][] = array('name'=>$option['name'],'languageId'=>$option['languageId']);
}
$f = 0;
}
}