$test = array
(
"0" => array
(
"isLibrary" => 1,
"isActive" => 1,
"title" => "Mytitle1234",
"nps" => 750000001456,
"system" => "Remote",
"system_path" =>"./remote/system/test"
),
"1" => array
(
"isLibrary" => 1,
"isActive" => 1,
"title" => "Mytitle1234",
"nps" => 750000001456,
"system" => "Local",
"system_path" =>"./local/system/test"
)
)
这是我的阵列。 PHP中是否有任何预定义的函数,以便我可以得到如下结果。
$test = array
(
"0" => array
(
"isLibrary" => 1,
"isActive" => 1,
"title" => "Mytitle1234",
"nps" => 750000001456,
"locations" => array (
"Remote" => array (
"system" => "Remote",
"system_path" =>"./remote/system/test"
),
"local" => array (
"system" => "Local",
"system_path" =>"./local/system/test"
)
)
)
我想组合数组,并希望根据系统值制作子数组。
答案 0 :(得分:1)
您可以使用array_diff分离所需的部分,然后从这些部分构建新数组。
$remote = array_diff($test[0], $test[1]);
$local = array_diff($test[1], $test[0]);
//what is not "remote", meaning the start of the array
$new[] = array_diff($test[0], $remote);
// Add remote and local
$new[0]["locations"] =["remote" => $remote, "local" => $local];
Var_dump($new);
编辑:忘记0索引,添加[]和[0]。
如果您不确定远程或本地是否在[0]中,您可以使用此代码。
$temp = array_diff($test[0], $test[1]);
$locations["locations"][$temp["system"]] = $temp;
$temp = array_diff($test[1], $test[0]);
$locations["locations"][$temp["system"]] = $temp;
$new[] = array_diff($test[0], $locations["locations"][$test[0]["system"]]);
$new[0] =array_merge($new[0], $locations);
Var_dump($new);
它保存了临时数组中的差异,然后根据位置数组中的内容构建位置数组。
答案 1 :(得分:-1)
预定义的功能不会为您创建自定义数组,但这里有如何构建它。
<?php
$old =
[
[
"isLibrary" => 1,
"isActive" => 1,
"title" => "Mytitle1234",
"nps" => 750000001456,
"system" => "Remote",
"system_path" =>"./remote/system/test"
],
[
"isLibrary" => 1,
"isActive" => 1,
"title" => "Mytitle1234",
"nps" => 750000001456,
"system" => "Local",
"system_path" =>"./local/system/test"
],
];
$new = [
"isLibrary" => $old[0]['isLibrary'],
"isActive" => $old[0]['isActive'],
"title" => $old[0]['title'],
"nps" => $old[0]['nps'],
"locations" => [
"remote" => [
"system" => $old[0]['system'],
"system_path" => $old[0]['system_path'],
],
"local" => [
"system" => $old[1]['system'],
"system_path" => $old[1]['system_path'],
],
],
];
var_dump($new);
哪个输出:
array(5) {
["isLibrary"]=> int(1)
["isActive"]=> int(1)
["title"]=> string(11) "Mytitle1234"
["nps"]=> int(750000001456)
["locations"]=> array(2) {
["remote"]=> array(2) {
["system"]=> string(6) "Remote"
["system_path"]=> string(20) "./remote/system/test"
} ["local"]=> array(2) {
["system"]=> string(5) "Local"
["system_path"]=> string(19) "./local/system/test"
}
}
}
答案 2 :(得分:-1)
$res = [];
foreach ($test as $info) {
$key = $info["isLibrary"] . $info["isActive"] . $info["title"] . $info["nps"];
if (isset($res[$key])) {
$res[$key]['locations'][$info['system']] = [
"system" => $info['system'],
"system_path" => $info["system_path"],
];
} else {
$res[$key] = [
"isLibrary" => $info["isLibrary"],
"isActive" => $info["isActive"],
"title" => $info["title"],
"nps" => $info["nps"],
"locations" => [
$info['system'] => [
"system" => $info['system'],
"system_path" => $info["system_path"],
],
],
];
}
}
$res = array_values($res);
print_r($res);