我需要输出'floorno'和'units'作为键。但在执行array_splice操作后,'floorno'和'units'键被整数值替换。
如何用文本floorno和unit替换整数值键。
以下是我的代码
$floor = "Ground";
$query = "SELECT id,property_no,status FROM property_details WHERE wing ='$A' AND floor ='$floor'";
$execute = $db->query($query);
$ground = $execute->fetchAll(PDO::FETCH_ASSOC);
$block_A = array("floorno"=>$floor,"units"=>$ground);
$floor = "First";
$query = "SELECT id,property_no,status FROM property_details WHERE wing ='$A' AND floor ='$floor'";
$execute = $db->query($query);
$ground = $execute->fetchAll(PDO::FETCH_ASSOC);
$first = array("floorno"=>$floor,"units"=>$ground);
array_splice($block_A,2,0,$first);
以下是我的数组o / p:
{
"0": "Second",
"1": [
{
"id": "396",
"property_no": "SSC.A.02.01",
"status": "0"
},
{
"id": "397",
"property_no": "SSC.A.02.02",
"status": "0"
}
],
"2": "First",
"3": [
{
"id": "388",
"property_no": "SSC.A.01.01",
"status": "0"
},
{
"id": "389",
"property_no": "SSC.A.01.02",
"status": "0"
}
],
"floorno": "Ground",
"units": [
{
"id": "379",
"property_no": "SSC.A.00.01",
"status": "0"
},
{
"id": "381",
"property_no": "SSC.A.00.02",
"status": "0"
}
]
}
答案 0 :(得分:0)
使用assoc
进行拼接的辅助函数 function array_splice_assoc(&$input, $offset, $length, $replacement)
{
$replacement = (array)$replacement;
$key_indices = array_flip(array_keys($input));
if (isset($input[$offset]) && is_string($offset)) {
$offset = $key_indices[$offset];
}
if (isset($input[$length]) && is_string($length)) {
$length = $key_indices[$length] - $offset;
}
$input = array_slice($input, 0, $offset, TRUE)
+ $replacement
+ array_slice($input, $offset + $length, NULL, TRUE);
}