我有一个数组,我需要根据需要安排这些数组。 我可以按照floor_id和时间戳重新排列它们吗?
我的数组格式:
Array (
[0] => stdClass Object
(
[id] => 100638
[gate_name] => Gate 148
[floor_id] => 4
[timestamp] => 2019-02-25 00:00:05
)
[1] => stdClass Object
(
[id] => 100639
[gate_name] => Gate 148
[floor_id] => 5
[timestamp] => 2019-02-25 00:00:10
)
[2] => stdClass Object
(
[id] => 100640
[gate_name] => Gate 148
[floor_id] => 6
[timestamp] => 2019-02-25 00:00:20
)
[3] => stdClass Object
(
[id] => 100644
[gate_name] => Gate 148
[floor_id] => 6
[timestamp] => 2019-02-25 00:00:30
)
[4] => stdClass Object
(
[id] => 100643
[gate_name] => Gate 148
[floor_id] => 4
[timestamp] => 2019-02-25 00:00:40
)
需要输出:
Array
(
[0] => Array
(
[floors] => 4
[data] => Array
(
[1] => Array
(
[id] => 100638
[gate_name] => Gate 148
[floor_id] => 4
[timestamp] => 2019-02-25 00:00:05
)
)
)
[1] => Array
(
[floors] => 5
[data] => Array
(
[2] => Array
(
[id] => 100639
[gate_name] => Gate 148
[floor_id] => 5
[timestamp] => 2019-02-25 00:00:10
)
)
)
[2] => Array
(
[floors] => 6
[data] => Array
(
[3] => Array
(
[id] => 100640
[gate_name] => Gate 148
[floor_id] => 6
[timestamp] => 2019-02-25 00:00:20
)
[4] => Array
(
[id] => 100644
[gate_name] => Gate 148
[floor_id] => 6
[timestamp] => 2019-02-25 00:00:30
)
)
)
[3] => Array
(
[floors] => 4
[data] => Array
(
[5] => Array
(
[id] => 100643
[gate_name] => Gate 148
[floor_id] => 4
[timestamp] => 2019-02-25 00:00:40
)
)
)
)
这些数组背后的逻辑-如果连续序列中出现相同的 floor_id ,则执行数组,如果没有,则在 timestamp 的帮助下拆分这些分组。
答案 0 :(得分:0)
您可以尝试以下代码
$arayData = Array (
array(
'id' => 100638,
'gate_name' => 'Gate 148',
'floor_id' => 4
),
array(
'id' => 100639,
'gate_name' => 'Gate 148',
'floor_id' => 5
),
array(
'id' => 100640,
'gate_name' => 'Gate 148',
'floor_id' => 6
),
array(
'id' => 100644,
'gate_name' => 'Gate 148',
'floor_id' => 6
),
array(
'id' => 100643,
'gate_name' => 'Gate 148',
'floor_id' => 4
));
$uniqueKeys = array_unique(array_column($arayData, 'floor_id'));
$floorDetails = array_column($arayData, 'floor_id');
$dataArrays = [];
foreach($uniqueKeys as $uniqueKey) {
$datas['floor_id'] = $uniqueKey;
foreach (array_keys($floorDetails, $uniqueKey) as $key) {
$datas['data'][] = $arayData[$key];
}
array_push($dataArrays, $datas);
$datas = [];
}
echo json_encode($dataArrays);
答案 1 :(得分:0)
也许这就是您要寻找的东西。
下次,请提供一个JSON序列化数组,这样其他人就不必重写您的数组。
<?php
$arrayData = array(
(object)(array(
"id" => 100638,
"gate_name" => "Gate 148",
"floor_id" => 4
)),
(object)(array(
"id" => 100639,
"gate_name" => " Gate 148",
"floor_id" => 5
)),
(object)(array(
"id" => 100640,
"gate_name" => "Gate 148",
"floor_id" => 6
)),
(object)(array(
"id" => 100644,
"gate_name" => "Gate 148",
"floor_id" => 6
)),
(object)(array(
"id" => 100643,
"gate_name" => "Gate 148",
"floor_id" => 4
))
);
$result = array();
foreach ($arrayData as $key => $data) {
$result[$data->floor_id]["floors"] = $data->floor_id;
$result[$data->floor_id]["data"][] = (array)$data;
// Remove the old data to minimize the memory allocation.
unset($arrayData[$key]);
}
// Clean up.
unset($arrayData, $key, $data);
// Clean up the array index.
$result = array_values($result);
print_r($result);
答案 2 :(得分:0)
主要代码:
$arrObject = array(
(object) (array(
"id" => 100638,
"gate_name" => "Gate 148",
"floor_id" => 4
)),
(object) (array(
"id" => 100639,
"gate_name" => " Gate 148",
"floor_id" => 5
)),
(object) (array(
"id" => 100640,
"gate_name" => "Gate 148",
"floor_id" => 6
)),
(object) (array(
"id" => 100644,
"gate_name" => "Gate 148",
"floor_id" => 6
)),
(object) (array(
"id" => 100643,
"gate_name" => "Gate 148",
"floor_id" => 4
))
);
$onlyFloorId = array();
foreach ($arrObject as $c) {
$onlyFloorId[] = $c->floor_id;
}
$onlyFloorId = array_values(array_unique($onlyFloorId)); //convert unique and indexed
$endResult = array();
foreach ($arrObject as $c) {
foreach ($onlyFloorId as $key => $value) {
if ($c->floor_id == $value) {
$endResult[$key]['floors'] = $c->floor_id;
$endResult[$key]['data'][] = (array)$c;
}
}
}
echo'<pre>';print_r($endResult);die;
输出:
Array
(
[0] => Array
(
[floors] => 4
[data] => Array
(
[0] => Array
(
[id] => 100638
[gate_name] => Gate 148
[floor_id] => 4
)
[1] => Array
(
[id] => 100643
[gate_name] => Gate 148
[floor_id] => 4
)
)
)
[1] => Array
(
[floors] => 5
[data] => Array
(
[0] => Array
(
[id] => 100639
[gate_name] => Gate 148
[floor_id] => 5
)
)
)
[2] => Array
(
[floors] => 6
[data] => Array
(
[0] => Array
(
[id] => 100640
[gate_name] => Gate 148
[floor_id] => 6
)
[1] => Array
(
[id] => 100644
[gate_name] => Gate 148
[floor_id] => 6
)
)
)
)