您好我正在尝试以最佳方式组合这两个不同格式的数组来创建一个新数组,但我总是用最后一组覆盖值。
数组1:这是一个动态数组,但格式相同
Array
(
[0] => stdClass Object
(
[label] => Name
[fieldtype] => text
[required] => 1
[description] => Sku Name
[sku_id] => skucid1
)
[1] => stdClass Object
(
[label] => Id
[fieldtype] => text
[required] =>
[description] => Sku Id
[sku_id] => skucid2
)
[2] => stdClass Object
(
[label] => Unit Price
[fieldtype] => decimal
[required] =>
[description] => Sku Unit Price
[sku_id] => skucid3
)
[3] => stdClass Object
(
[label] => Tax
[fieldtype] => decimal
[required] =>
[description] => Sku Tax
[sku_id] => skucid4
)
[4] => stdClass Object
(
[label] => Quantity
[fieldtype] => number
[required] => 1
[description] => Sku Quantity
[sku_id] => skucid5
)
[5] => stdClass Object
(
[label] => Total
[fieldtype] => decimal
[required] =>
[description] => Sku Total
[sku_id] => skucid6
)
)
数组2:它是动态数组,但格式相同
Array
(
[0] => Array
(
[0] => J1
[1] => SKUJ1
[2] => 1000
[3] => 100
[4] => 10
[5] => 11000
)
[1] => Array
(
[0] => J2
[1] => SKUJ2
[2] => 2000
[3] => 100
[4] => 10
[5] => 21000
)
)
我正在尝试的输出数组是创建第二个数组的相同数字对象数组,每个对象中的额外键名为“value”,该键的值是第二个数组中对应的键值对。
以上两个数组的示例JSON输出:
[
[
{
"label": "Name",
"fieldtype": "text",
"required": true,
"description": "Sku Name",
"sku_id": "skucid1",
"value": "J1"
},
{
"label": "Id",
"fieldtype": "text",
"required": false,
"description": "Sku Id",
"sku_id": "skucid2",
"value": "SKUJ1"
},
{
"label": "Unit Price",
"fieldtype": "decimal",
"required": false,
"description": "Sku Unit Price",
"sku_id": "skucid3",
"value": "1000"
},
{
"label": "Tax",
"fieldtype": "decimal",
"required": false,
"description": "Sku Tax",
"sku_id": "skucid4",
"value": "100"
},
{
"label": "Quantity",
"fieldtype": "number",
"required": true,
"description": "Sku Quantity",
"sku_id": "skucid5",
"value": "10"
},
{
"label": "Total",
"fieldtype": "decimal",
"required": false,
"description": "Sku Total",
"sku_id": "skucid6",
"value": "11000"
}
],
[
{
"label": "Name",
"fieldtype": "text",
"required": true,
"description": "Sku Name",
"sku_id": "skucid1",
"value": "J2"
},
{
"label": "Id",
"fieldtype": "text",
"required": false,
"description": "Sku Id",
"sku_id": "skucid2",
"value": "SKUJ2"
},
{
"label": "Unit Price",
"fieldtype": "decimal",
"required": false,
"description": "Sku Unit Price",
"sku_id": "skucid3",
"value": "2000"
},
{
"label": "Tax",
"fieldtype": "decimal",
"required": false,
"description": "Sku Tax",
"sku_id": "skucid4",
"value": "100"
},
{
"label": "Quantity",
"fieldtype": "number",
"required": true,
"description": "Sku Quantity",
"sku_id": "skucid5",
"value": "10"
},
{
"label": "Total",
"fieldtype": "decimal",
"required": false,
"description": "Sku Total",
"sku_id": "skucid6",
"value": "21000"
}
]
]
我目前得到的输出:
[
[
{
"label": "Name",
"fieldtype": "text",
"required": true,
"description": "Sku Name",
"sku_id": "skucid1",
"value": "J2"
},
{
"label": "Id",
"fieldtype": "text",
"required": false,
"description": "Sku Id",
"sku_id": "skucid2",
"value": "SKUJ2"
},
{
"label": "Unit Price",
"fieldtype": "decimal",
"required": false,
"description": "Sku Unit Price",
"sku_id": "skucid3",
"value": "2000"
},
{
"label": "Tax",
"fieldtype": "decimal",
"required": false,
"description": "Sku Tax",
"sku_id": "skucid4",
"value": "100"
},
{
"label": "Quantity",
"fieldtype": "number",
"required": true,
"description": "Sku Quantity",
"sku_id": "skucid5",
"value": "10"
},
{
"label": "Total",
"fieldtype": "decimal",
"required": false,
"description": "Sku Total",
"sku_id": "skucid6",
"value": "21000"
}
],
[
{
"label": "Name",
"fieldtype": "text",
"required": true,
"description": "Sku Name",
"sku_id": "skucid1",
"value": "J2"
},
{
"label": "Id",
"fieldtype": "text",
"required": false,
"description": "Sku Id",
"sku_id": "skucid2",
"value": "SKUJ2"
},
{
"label": "Unit Price",
"fieldtype": "decimal",
"required": false,
"description": "Sku Unit Price",
"sku_id": "skucid3",
"value": "2000"
},
{
"label": "Tax",
"fieldtype": "decimal",
"required": false,
"description": "Sku Tax",
"sku_id": "skucid4",
"value": "100"
},
{
"label": "Quantity",
"fieldtype": "number",
"required": true,
"description": "Sku Quantity",
"sku_id": "skucid5",
"value": "10"
},
{
"label": "Total",
"fieldtype": "decimal",
"required": false,
"description": "Sku Total",
"sku_id": "skucid6",
"value": "21000"
}
]
]
答案 0 :(得分:0)
取第3个变量来保持o / p。循环遍历第二个数组并迭代每个第二个维度项。跟踪第一个数组中第二级值(0,1,2)的键/索引并分配给新数组。为此,我建议你在下面的代码: -
$array1 = "Your Input Array";
$array2 = "Your Input Array";
$array3 = array()
foreach($array2 as $arr2Key => $arr2Value):
$array3[$arr2Key] = array();
foreach($arr2Value as $arr2Innerkey => $arr2InnerValue):
if (array_key_exists($arr2Innerkey, $array1)):
$array1Obj = array();
$array1Obj = (array) $array1[$arr2Innerkey];
$array1Obj['value'] = $arr2InnerValue;
$array3[$arr2Key][] = $arr2InnerValue;
endif;
endforeach;
endforeach;
echo json_encode($array3);