我是新工作的PHP,我使用foreach循环来遍历解码对象的数组。我想为每次迭代输入一个新数组的值。这是代码的一部分:
val df = Seq(
(1, 1, 2, 4), (1, 1, 2, 4), (1, 1, 3, 5)
).toDF("col1", "col2", "col3", "col4")
df.melt(
Seq("col1", "col2"), Seq("col3", "col4"), "col_name", "col_value"
).groupBy("col1", "col2", "col_name", "col_value").count.show
// +----+----+--------+---------+-----+
// |col1|col2|col_name|col_value|count|
// +----+----+--------+---------+-----+
// | 1| 1| col3| 3| 1|
// | 1| 1| col4| 5| 1|
// | 1| 1| col4| 4| 2|
// | 1| 1| col3| 2| 2|
// +----+----+--------+---------+-----+
目前我正在生成一维数组,我需要的是获得这种类型的数组:
//example of array before decoding it [{"id":1,"quantity":12, other values...},{"id":2,"quantity":13,other values...}]
$data = json_decode($request->data);
$newArray = array();
foreach ($data as $key => $value){
$newArray[$key] = $value->{'id'} //[1,2,3....]
}
其他'将是我从循环[
1 => ['quantity' => $other],
2 => ['quantity' => $other]
]
得到的另一个值。我怎样才能生成这个?
答案 0 :(得分:6)
我不是100%自信,我是否真的得到了你的问题......但这可能是你想要的:
foreach ($data as $key => $value) {
$newArray[$value->id] = ['quantity' => $value->quantity];
}
print_r($newArray);
在json_decode
之后您拥有stdClass
类型的对象,因此您可以访问上面显示的属性。
答案 1 :(得分:4)
我不确定为什么在一个元素数组中需要quantity
个键。如果没有这个,你可以简单地使用https://docs.angularjs.org/api/ng/directive/select函数:
$items = json_decode($json);
// $items = json_decode($json, true); // For PHP < 7.0
$result = array_column($items, 'quantity', 'id');
但如果您确定需要quantity
个密钥,则可以使用array_column
映射结果:
$result = array_map(function ($quantity) {
return ['quantity' => $quantity];
}, $result);
这是array_map
。
答案 2 :(得分:2)
使用循环,你会做这样的事情:
$data = '[{"id":1,"quantity":12},{"id":2,"quantity":13}]';
$decoded = json_decode($data, $as_array = true); // decode as array, not object
$accumulator = [];
foreach($decoded as $index=>$values) {
$accumulator[$values['id']] = ['quantity' => $values['quantity']];
}
print_r($accumulator);
使用array_reduce
:
$data = '[{"id":1,"quantity":12},{"id":2,"quantity":13}]';
$decoded = json_decode($data, $as_array = true); // decode as array, not object
$reduced = array_reduce($decoded, function($carry, $entry){
$carry[$entry['id']] = ['quantity' => $entry['quantity']];
return $carry;
}, $initial_value = []);
print_r($reduced);
两种方法都会生成相同的输出:
Array
(
[1] => Array
(
[quantity] => 12
)
[2] => Array
(
[quantity] => 13
)
)
答案 3 :(得分:2)
我认为你期待这样,
$request = new stdClass();
$request->data = '[{"id":1,"quantity":12,"name":"item 1","code":"ITM-111"},{"id":2,"quantity":13,"name":"item 2","code":"ITM-222"}]';
$data = json_decode($request->data);
$newArray = array();
print_r($data);
示例:的
Array
(
[0] => stdClass Object
(
[id] => 1
[quantity] => 12
[name] => item 1
[code] => ITM-111
)
[1] => stdClass Object
(
[id] => 2
[quantity] => 13
[name] => item 2
[code] => ITM-222
)
)
$other = 'name'; // required key
foreach($data as $value){
$newArray[$value->id] = [
'quantity' => $value->quantity,
'name' => $value->{$other}
];
}
print_r($newArray);
输出:
Array
(
[1] => Array
(
[quantity] => 12
[name] => item 1
)
[2] => Array
(
[quantity] => 13
[name] => item 2
)
)
答案 4 :(得分:1)
您可以使用简单的foreach循环
来完成$json='[{"id":1,"quantity":12, "ov":1,"ov2":2},{"id":2,"quantity":13,"ov":"other values"}]';
echo "<pre/>";
$a=json_decode($json,TRUE);
foreach($a as $val){
$arr=$val;
unset($arr['id']);
$new_arr[$val['id']]=$arr;
}
print_r($new_arr);
您的问题不明确,如果您正在尝试将数量放在密钥上,请尝试以下内容
$json='[{"id":1,"quantity":12, "ov":1,"ov2":2},{"id":2,"quantity":13,"ov":"other values"}]';
echo "<pre/>";
$a=json_decode($json,TRUE);
foreach($a as $val){
$arr=$val;
unset($arr['id']);
$temp=$arr;
unset($temp['quantity']);
$new_arr[$val['id']]=[$arr['quantity']=>$temp];
}
print_r($new_arr);
Array
(
[1] => Array
(
[12] => Array
(
[ov] => 1
[ov2] => 2
)
)
[2] => Array
(
[13] => Array
(
[ov] => other values
)
)
)
答案 5 :(得分:1)
以下是如您所述生成数组的方法。我添加了other
属性的示例值以帮助说明。
<?php
header('Content-Type: text/plain');
$request_data = '[{"id":1,"quantity":12,"other":"Other 1"},{"id":2,"quantity":13,"other":"Other 2"}]';
$data = json_decode($request_data);
$newArray = array();
foreach ($data as $key => $value) {
$newArray[$value->id] = array(
'quantity' => $value->other
);
}
print_r($newArray);
// Returns:
/*
Array
(
[1] => Array
(
[quantity] => Other 1
)
[2] => Array
(
[quantity] => Other 2
)
)
*/
答案 6 :(得分:-3)
您可以使用以下内容附加数组:
foreach($file_data as $value) {
list($value1, $value2, $value3) = explode('|', $value);
$item = array(
'value1' => $value1,
'value2' => $value2,
'value3' => $value3
);
$file_data_array[] = $item;
}
有关更多信息,请查看以下内容: Creating/modifying with square bracket syntax