如何循环到php中的多维数组?
How to loop to mulitidimensional array in php?
大家好,我有一个这样的多维数组,基于PHP。
Array(
[0] => Array
(
[name] => id[]
[value] => 1
)
[1] => Array
(
[name] => name[]
[value] => John DoeS
)
[2] => Array
(
[name] => position[]
[value] => Manager
)
[3] => Array
(
[name] => nationality[]
[value] => US
)
[4] => Array
(
[name] => shares[]
[value] => 20
)
[5] => Array
(
[name] => email_address[]
[value] => jd@gmail.com
)
[6] => Array
(
[name] => tel_no[]
[value] => 123456789
)
[7] => Array
(
[name] => fax_no[]
[value] => 12345678
)
[8] => Array
(
[name] => ic_number[]
[value] => 51-698485
)
[9] => Array
(
[name] => id[]
[value] => 2
)
[10] => Array
(
[name] => name[]
[value] => Jane Doe
)
[11] => Array
(
[name] => position[]
[value] => Manager
)
[12] => Array
(
[name] => nationality[]
[value] => US
)
[13] => Array
(
[name] => shares[]
[value] => 20
)
[14] => Array
(
[name] => email_address[]
[value] => jage@gmail.com
)
[15] => Array
(
[name] => tel_no[]
[value] => 12345678
)
[16] => Array
(
[name] => fax_no[]
[value] => 12345678
)
[17] => Array
(
[name] => ic_number[]
[value] => 25-8978477
)
)
我试图循环到这个多维数组并创建类似这样的东西,
Array (
[id] => Array
(
[0] => 1
[1] => 2
)
[name] => Array
(
[0] => John DoeS
[1] => Jane Doe
)
[position] => Array
(
[0] => Manager
[1] => Manager
)
[nationality] => Array
(
[0] => Brunieian
[1] => US
)
[shares] => Array
(
[0] => 20
[1] => 20
)
[email_address] => Array
(
[0] => jd@gmail.com
[1] => jage@gmail.com
)
[tel_no] => Array
(
[0] => 123456789
[1] => 12345678
)
[fax_no] => Array
(
[0] => 12345678
[1] => 12345678
)
[ic_number] => Array
(
[0] => 51-698485
[1] => 25-8978477
)
)
基本上第一个数组是 ajax 提交表单并使用 json_decode() 解码的结果
这是我的测试代码,我无法实现第二个数组。
foreach ($data as $field => $values) {
if (is_array($values)) {
foreach ($values as $key => $value) {
$singleArray[$key][$field] = $value;
}
}
}
输出
Array(
[name] => Array
(
[0] => id[]
[1] => name[]
[2] => position[]
[3] => nationality[]
[4] => shares[]
[5] => email_address[]
[6] => tel_no[]
[7] => fax_no[]
[8] => ic_number[]
[9] => id[]
[10] => name[]
[11] => position[]
[12] => nationality[]
[13] => shares[]
[14] => email_address[]
[15] => tel_no[]
[16] => fax_no[]
[17] => ic_number[]
)
[value] => Array
(
[0] => 1
[1] => John DoeS
[2] => Manager
[3] => Brunieian
[4] => 20
[5] => jd@gmail.com
[6] => 123456789
[7] => 12345678
[8] => 51-698485
[9] => 2
[10] => Jane Doe
[11] => Manager
[12] => US
[13] => 20
[14] => jage@gmail.com
[15] => 12345678
[16] => 12345678
[17] => 25-8978477
)
)
这是有效的代码:
foreach($data as $value) {
$new_key = $value["name"];
$new_value = $value["value"];
$new_array[$new_key][] = $new_value;
}
大家好,我有一个这样的多维数组,基于PHP。
Array(
[0] => Array
(
[name] => id[]
[value] => 1
)
[1] => Array
(
[name] => name[]
[value] => John DoeS
)
[2] => Array
(
[name] => position[]
[value] => Manager
)
[3] => Array
(
[name] => nationality[]
[value] => US
)
[4] => Array
(
[name] => shares[]
[value] => 20
)
[5] => Array
(
[name] => email_address[]
[value] => jd@gmail.com
)
[6] => Array
(
[name] => tel_no[]
[value] => 123456789
)
[7] => Array
(
[name] => fax_no[]
[value] => 12345678
)
[8] => Array
(
[name] => ic_number[]
[value] => 51-698485
)
[9] => Array
(
[name] => id[]
[value] => 2
)
[10] => Array
(
[name] => name[]
[value] => Jane Doe
)
[11] => Array
(
[name] => position[]
[value] => Manager
)
[12] => Array
(
[name] => nationality[]
[value] => US
)
[13] => Array
(
[name] => shares[]
[value] => 20
)
[14] => Array
(
[name] => email_address[]
[value] => jage@gmail.com
)
[15] => Array
(
[name] => tel_no[]
[value] => 12345678
)
[16] => Array
(
[name] => fax_no[]
[value] => 12345678
)
[17] => Array
(
[name] => ic_number[]
[value] => 25-8978477
)
)
我试图循环到这个多维数组并创建类似这样的东西,
Array (
[id] => Array
(
[0] => 1
[1] => 2
)
[name] => Array
(
[0] => John DoeS
[1] => Jane Doe
)
[position] => Array
(
[0] => Manager
[1] => Manager
)
[nationality] => Array
(
[0] => Brunieian
[1] => US
)
[shares] => Array
(
[0] => 20
[1] => 20
)
[email_address] => Array
(
[0] => jd@gmail.com
[1] => jage@gmail.com
)
[tel_no] => Array
(
[0] => 123456789
[1] => 12345678
)
[fax_no] => Array
(
[0] => 12345678
[1] => 12345678
)
[ic_number] => Array
(
[0] => 51-698485
[1] => 25-8978477
)
) 基本上第一个数组是 ajax 提交表单并使用 json_decode() 解码的结果 这是我的测试代码,我无法实现第二个数组。
foreach ($data as $field => $values) {
if (is_array($values)) {
foreach ($values as $key => $value) {
$singleArray[$key][$field] = $value;
}
}
} 输出
Array(
[name] => Array
(
[0] => id[]
[1] => name[]
[2] => position[]
[3] => nationality[]
[4] => shares[]
[5] => email_address[]
[6] => tel_no[]
[7] => fax_no[]
[8] => ic_number[]
[9] => id[]
[10] => name[]
[11] => position[]
[12] => nationality[]
[13] => shares[]
[14] => email_address[]
[15] => tel_no[]
[16] => fax_no[]
[17] => ic_number[]
)
[value] => Array
(
[0] => 1
[1] => John DoeS
[2] => Manager
[3] => Brunieian
[4] => 20
[5] => jd@gmail.com
[6] => 123456789
[7] => 12345678
[8] => 51-698485
[9] => 2
[10] => Jane Doe
[11] => Manager
[12] => US
[13] => 20
[14] => jage@gmail.com
[15] => 12345678
[16] => 12345678
[17] => 25-8978477
)
)
这是有效的代码:
foreach($data as $value) {
$new_key = $value["name"];
$new_value = $value["value"];
$new_array[$new_key][] = $new_value;
}