我希望有一个遵循这种模式的数组:
[1] => Array
(
[es] => 11
[pt] => 22
[br] => 333
)
[2] => Array
(
[es] => 1221
[pt] => 23442
[br] => 344433
)
每个项目(包括密钥)都来自查询。所以,我尝试以下列方式包含项目:
array[1]= array('es'=>'11');
array[1]+= array('pt'=>'22');
如果我手动硬编码数据,这可以正常工作。但是,它在迭代mysql数据时失败:
foreach($elements as $value) {
$available = $value['purchased'] - $value['used'];
$credits[$value['type']]= array($value['country']=>$availableCredit);
}
我收到以下错误:
致命错误:不支持的操作数类型
P.S。 $value['type']
仅包含“1”或“2”。
答案 0 :(得分:2)
<?php
$elements = [
[
'type' => 'car',
'country' => 'br',
'purchased' => 47,
'used' => 23
],
[
'type' => 'car',
'country' => 'es',
'purchased' => 50,
'used' => 10
],
[
'type' => 'boat',
'country' => 'es',
'purchased' => 100,
'used' => 99
],
];
foreach($elements as $value) {
$difference = $value['purchased'] - $value['used'];
$credits[$value['type']][$value['country']] = $difference;
}
var_export($credits);
输出:
array (
'car' =>
array (
'br' => 24,
'es' => 40,
),
'boat' =>
array (
'es' => 1,
),
)