所以我的表格帖子的结果如下:
$data = [
'id_1' => [
'0' => 1,
'1' => 2
],
'id_2' => [
'0' => 3,
'1' => 4
],
'id_3' => [
'0' => 5,
'1' => 6
]
];
我想要实现的是将这个数组分成两个不同的数组:
$item_1 = [
'id_1' => 1,
'id_2' => 3,
'id_3' => 5
]
$item_2 = [
'id_1' => 2,
'id_2' => 4,
'id_3' => 6
]
我已经尝试使用所有正确的数组方法,例如array_chunk,array_merge with loops但是我似乎无法理解如何实现这一点。我见过很多类似的帖子,其中第一个键没有像我的数组那样的名称(id_1,id_2,id_3)。但在我的情况下,键的名称是至关重要的,因为它们需要被设置为各个数组中键的名称。
答案 0 :(得分:0)
没有循环的解决方案,只是为了好玩:
C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin\ld.exe skipping incompatible C:/SDL2-2.0.4/i686-w64-mingw32/lib/libSDL2main.a when searching for -lSDL2main
C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin\ld.exe skipping incompatible C:/SDL2-2.0.4/i686-w64-mingw32/lib\libSDL2main.a when searching for -lSDL2main
C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin\ld.exe skipping incompatible C:/dev_lib/SDL2-2.0.4/i686-w64-mingw32/lib/libSDL2main.a when searching for -lSDL2main
C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin\ld.exe skipping incompatible C:/dev_lib/SDL2-2.0.4/i686-w64-mingw32/lib\libSDL2main.a when searching for -lSDL2main
C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin\ld.exe skipping incompatible C:/dev_lib/SDL2-2.0.4/i686-w64-mingw32/lib/libSDL2main.a when searching for -lSDL2main
C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin\ld.exe skipping incompatible C:/dev_lib/SDL2-2.0.4/i686-w64-mingw32/lib\libSDL2main.a when searching for -lSDL2main
C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin\ld.exe skipping incompatible C:/SDL2-2.0.4/i686-w64-mingw32/lib/libSDL2main.a when searching for -lSDL2main
C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin\ld.exe skipping incompatible C:/dev_lib/SDL2-2.0.4/i686-w64-mingw32/lib/libSDL2main.a when searching for -lSDL2main
C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin\ld.exe skipping incompatible C:/dev_lib/SDL2-2.0.4/i686-w64-mingw32/lib/libSDL2main.a when searching for -lSDL2main
C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin\ld.exe cannot find -lSDL2main
C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin\ld.exe skipping incompatible C:/SDL2-2.0.4/i686-w64-mingw32/lib/libSDL2.dll.a when searching for -lSDL2
C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin\ld.exe skipping incompatible C:/SDL2-2.0.4/i686-w64-mingw32/lib/libSDL2.a when searching for -lSDL2
C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin\ld.exe skipping incompatible C:/SDL2-2.0.4/i686-w64-mingw32/lib\libSDL2.a when searching for -lSDL2
C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin\ld.exe skipping incompatible C:/dev_lib/SDL2-2.0.4/i686-w64-mingw32/lib/libSDL2.dll.a when searching for -lSDL2
C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin\ld.exe skipping incompatible C:/dev_lib/SDL2-2.0.4/i686-w64-mingw32/lib/libSDL2.a when searching for -lSDL2
C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin\ld.exe skipping incompatible C:/dev_lib/SDL2-2.0.4/i686-w64-mingw32/lib\libSDL2.a when searching for -lSDL2
C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin\ld.exe skipping incompatible C:/dev_lib/SDL2-2.0.4/i686-w64-mingw32/lib/libSDL2.dll.a when searching for -lSDL2
C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin\ld.exe skipping incompatible C:/dev_lib/SDL2-2.0.4/i686-w64-mingw32/lib/libSDL2.a when searching for -lSDL2
C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin\ld.exe skipping incompatible C:/dev_lib/SDL2-2.0.4/i686-w64-mingw32/lib\libSDL2.a when searching for -lSDL2
C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin\ld.exe skipping incompatible C:/SDL2-2.0.4/i686-w64-mingw32/lib/libSDL2.dll.a when searching for -lSDL2
C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin\ld.exe skipping incompatible C:/SDL2-2.0.4/i686-w64-mingw32/lib/libSDL2.a when searching for -lSDL2
C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin\ld.exe skipping incompatible C:/dev_lib/SDL2-2.0.4/i686-w64-mingw32/lib/libSDL2.dll.a when searching for -lSDL2
C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin\ld.exe skipping incompatible C:/dev_lib/SDL2-2.0.4/i686-w64-mingw32/lib/libSDL2.a when searching for -lSDL2
C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin\ld.exe skipping incompatible C:/dev_lib/SDL2-2.0.4/i686-w64-mingw32/lib/libSDL2.dll.a when searching for -lSDL2
C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin\ld.exe skipping incompatible C:/dev_lib/SDL2-2.0.4/i686-w64-mingw32/lib/libSDL2.a when searching for -lSDL2
C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin\ld.exe cannot find -lSDL2
C:\Users\dubcusb\Desktop\dubcusb\CEPLUSPLUS\Empty Projy\collect2.exe [Error] ld returned 1 exit status
25 C:\Users\dubcusb\Desktop\dubcusb\CEPLUSPLUS\Empty Projy\Makefile.win recipe for target '"Empty' failed
答案 1 :(得分:0)
只需要一个普通的foreach
循环。
$item_1 = [];
$item_2 = [];
foreach ($data as $k => $v){
$item_1[$k] = $v[0];
$item_2[$k] = $v[1];
}
希望这有帮助。
答案 2 :(得分:0)
比这短得多很难找到:
$item1 = array_map('reset', $data);
$item2 = array_map('end', $data);
array_map
期望回调函数作为其第一个参数。在第一行中,这是reset
,因此将在reset
的每个元素上调用$data
,从而有效地获取子数组的第一个元素值。 array_map
将这些结果组合在一个新数组中,保留原始密钥。
第二行的功能相同,但使用函数end
,它有效地抓取了子数组的最后一个元素的值。
reset
和end
移动内部数组指针的事实无关紧要。这里唯一重要的是它们还返回它们将指针放在元素的值。
答案 3 :(得分:-2)
假设每个'id_1'上只有2组键值对,以下内容将按您的意愿执行。
$item_1 = array();
$item_2 = array();
foreach ($data as $item) {
$item_1 = $item[0];
$item_2 = $item[1];
}