我在数组中有30个JSON对象。我想遍历每个对象并创建一个只有3个对象的新数组,每个对象包含10个对象的数据。
我的问题是我不确定如何在foreach循环中创建新对象
左侧的键和右侧的值
1 1
2 2
3 3
. .
. .
. .
1 11
1 12
2 13
. .
. .
. .
1 21
2 22
3 23
. .
. .
这里,sid告诉所有对象需要分组到一个对象中,根据这个例子,将10个对象分成1个。
所以目前我有:
[
{
"key1" : "value1",
"key2" : "value2",
"sid" : "1"
},
{
"key1" : "value3",
"key2" : "value4",
"sid" : "1"
},
.
. // Total 10 objects with sid = 1
.
,
{
"key1" : "value5",
"key2" : "value6",
"sid" : "2"
},
{
"key1" : "value7",
"key2" : "value8",
"sid" : "2"
},
.
. // Total 10 objects with sid = 2
.
,
{
"key1" : "value9",
"key2" : "value10",
"sid" : "3"
},
{
"key1" : "value11",
"key2" : "value12",
"sid" : "3"
},
.
. // Total 10 objects with sid = 3
]
我想将其替换为:
[
{
"value2" : "value1",
"value4" : "value3", //all data in this object belongs to sid = 1
"valueN" : "valueM" //Other respective 8 objects
},
{
"value6" : "value5",
"value8" : "value7", //all data in this object belongs to sid = 2
"valueN" : "valueM" //Other respective 8 objects
},
{
"value10" : "value9",
"value12" : "value11", //all data in this object belongs to sid = 3
"valueN" : "valueM" //Other respective 8 objects
}
]
我直接循环遍历数组中的每个对象,并将它们添加到名为finalResult的新数组中,方法是将key设置为key2的值,将值设置为key1值
for ($i = 0; $i < $count; $i++)
{
$finalResult[$arrObj[$i]['key2']] = $arrObj[$i]['key1'];
}
这只会创建一个包含所有数据的对象。
我想检查一下sid == 11,12,13,...等等然后创建新对象并开始在新创建的对象中添加所有数据,直到遇到下一个sid。
这就是我得到的:
{
"1": {
"value2" : "value1",
"value4" : "value3",
"valueN" : "valueM" //Other respective 8 objects
},
"2": {
"value6" : "value5",
"value8" : "value7",
"valueN" : "valueM" //Other respective 8 objects
},
"3": {
"value10" : "value9",
"value12" : "value11",
"valueN" : "valueM" //Other respective 8 objects
}
}
我在期待:
[
{
"value2" : "value1",
"value4" : "value3",
"valueN" : "valueM" //Other respective 8 objects
},
{
"value6" : "value5",
"value8" : "value7",
"valueN" : "valueM" //Other respective 8 objects
},
{
"value10" : "value9",
"value12" : "value11",
"valueN" : "valueM" //Other respective 8 objects
}
]
答案 0 :(得分:1)
如果我理解正确,你需要做这样的事情:
// Solution One
function removeProperty(obj, prop) {
var bool;
var keys = Object.keys(obj);
for (var i = 0; i < keys.length; i++) {
if (keys[i] === prop) {
delete obj[prop];
bool = true;
}
}
return Boolean(bool);
}
//Solution two
function removeProperty(obj, prop) {
var bool;
if (obj.hasOwnProperty(prop)) {
bool = true;
delete obj[prop];
}
return Boolean(bool);
}
编辑:完整示例。
for ($i = 0; $i < $count; $i++)
{
$sid = $arrObj[$i]['sid'];
if(!isset($finalResult[$sid])) $finalResult[$sid] = array();
$finalResult[$sid][$arrObj[$i]['key2']] = $arrObj[$i]['key1'];
}
var_dump(json_encode(array_values($finalResult)));
输出:
$json = '[
{
"key1" : "value1",
"key2" : "value2",
"sid" : "1"
},
{
"key1" : "value3",
"key2" : "value4",
"sid" : "1"
},
{
"key1" : "value5",
"key2" : "value6",
"sid" : "2"
},
{
"key1" : "value7",
"key2" : "value8",
"sid" : "2"
}]';
$arrObj = json_decode($json, true);
$count = count($arrObj);
$finalResult = [];
for ($i = 0; $i < $count; $i++)
{
$sid = $arrObj[$i]['sid'];
if(!isset($finalResult[$sid])) $finalResult[$sid] = array();
$finalResult[$sid][$arrObj[$i]['key2']] = $arrObj[$i]['key1'];
}
var_dump(json_encode(array_values($finalResult)));
答案 1 :(得分:0)
您可以使用sid
和key2
作为结果数组中的两个键级别来更改为所需的结构:
foreach ($data as $item) {
$result[$item->sid][$item->key2] = $item->key1;
}
$result = array_values($result);
注意事项:
如果同一key2
的{{1}}个值相同,则相应的sid
值将在key1
中被后续覆盖值。
$result
时, array_values($result)
是将结果编码为对象数组所必需的。如果没有它,它将是一个具有每个json_encode
的属性的对象。