我有两个不同的数组,第一个是types_array,第二个是result_array。我想按照类型_array中的类型从result_array收集数据并放入新数组。 它仅与types_array中的索引的第一条记录匹配,而与其他记录不匹配。在获得结果时,我将直接添加数组,以便您可以更好地理解。
类型数组
Array
(
[0] => class
[1] => late_fine
[2] => exam_fees
)
结果数组
Array
(
[0] => stdClass Object
(
[unit_name] => class 1
[price] => 10
[type] => class
)
[1] => stdClass Object
(
[unit_name] => class 1
[price] => 10
[type] => class
)
[2] => stdClass Object
(
[unit_name] => late fine
[price] => 50
[type] => late_fine
)
[3] => stdClass Object
(
[unit_name] => late fine
[price] => 40
[type] => late_fine
)
)
我得到的结果
Array
(
[class] => Array
(
[name] => class 1
[0] => 10
[1] => 10
[2] => 10
)
)
我期望的结果
Array
(
[class] => Array
(
[name] => class 1
['price'] => 20
)
[late_fine] => Array
(
[name] => late fine
['price'] => 100
)
)
我的代码
$i=0;
$x=0;
$types_array = array_values(student_payment_types());
while($x < count($types_array)){
while($i < count($result_array)){
if( $types_array[$x] == $result_array[$i]->type ){
$data_array[$types_array[$x]]['name'] = $result_array[$i]->unit_name;
$data_array[$types_array[$x]][] += (int)$result_array[$i]->price;
}
$i++;
}
unset($types_array[$x]);
array_values($types);
$x++;
}
答案 0 :(得分:0)
哦,我想我明白了,我犯了一个愚蠢的错误,当我退出内循环时,需要将$ i设置为零。