P
P
Pavel Kizernis2016-12-06 19:41:21
PHP
Pavel Kizernis, 2016-12-06 19:41:21

How to wrap array elements in an additional key (index)?

There is an array:

Array
(
    [82] => Array
        (
            /*значения*/
        )
    [83] => Array
        (
            /*значения*/
        )
    [84] => Array
        (
            /*значения*/
        )
)

How to make it like this:
Array
(
    [0] => Array
        (
            [82] => Array
                 (
                    /*значения*/
                 )
        )
    [1] => Array
        (
            [83] => Array
                 (
                    /*значения*/
                 )
        )
    [2] => Array
        (
            [84] => Array
                 (
                    /*значения*/
                 )
        )
)

?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
W
WQP, 2016-12-06
@pashakiz

<?php

$startArray = array(
  50 => 'lorem',
  70 => 'ipsum',
  60 => 'dolor',
);

$endArray = array();

foreach($startArray as $key => $item) {
    $endArray[] = array($key => $item);
}

print_r($endArray);

L
lxfr, 2016-12-06
@lxfr

Loop through the array and create a new array with elements from the old one, as an option.

S
Sergey Semenko, 2016-12-06
@abler98

$map = function ($key, $value) { return [$key => $value]; };
$array = array_map($map, array_keys($array), array_values($array));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question