Как l oop до многомерного массива в php? - PullRequest
0 голосов
/ 18 апреля 2020

Добрый день всем, у меня есть многомерный массив, подобный этому, основанный на 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
    )

)

Я пытаюсь l oop к этому многомерному массиву и создайте что-то вроде этого,

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
    )

)

1 Ответ

1 голос
/ 20 апреля 2020

Вот код, который работает:

foreach($data as $value) {
    $new_key = $value["name"];
    $new_value = $value["value"];
    $new_array[$new_key][] = $new_value;
}
...