我有这个数组来自帖子请求:
$x = [
"meal" => [
"monday" => [
"breakfast" => [
0 => "type1",
1 => "type1",
],
"afternoonTea" => [
0 => "type2",
],
],
],
"number" => [
"monday" => [
"breakfast" => [
0 => "10",
1 => "9",
],
"afternoonTea" => [
0 => "1",
],
],
],
];
我正在尝试将所有这些合并为一个数组,而不使用foreaches的色调。 也许更聪明的人会知道如何使用迭代器来实现这一目标。
来自meal
和number
的数据始终为1:1
。
我需要将其转换为如下形式:
$x = [
"monday" => [
"breakfast" => [
[
'type' => "type1",
'number' => 10,
],
[
'type' => "type1",
'number' => 9
]
],
"afternoonTea" => [
[
'type' => "type2",
'number' => 1,
],
],
],
],
];
答案 0 :(得分:1)
如果您将3视为吨,那么这不是您的答案-但我看不到一种方法,可以减少低于此数量的循环次数,而无需参加代码高尔夫比赛,这将无济于事
由于您知道ceil
和addEventListener
之间存在1:1的关系,因此您可以通过执行以下操作使用一个索引从另一个索引获取数据:
IF OBJECT_ID('tempdb..#SearchText') IS NOT NULL
DROP TABLE #SearchText
CREATE TABLE #SearchText
(
ProductId INT,
ProductName VARCHAR(500)
)
INSERT INTO #SearchText VALUES
(1,'Water Soap Bottel'),
(2,'Water Milk Bottel'),
(3,'Wooden Box'),
(4,'Water Plastic Bottel'),
(5,'Water Copper Bottel')
其输出将为meal
,其中将包含所需格式的数组。