简体   繁体   中英

Pad an array with incrementing numbers

Is it possible to pad an array with incrementing numbers? For example

$myArr = ["red", "green", "blue"];
$type = "colour";

I want to somehow merge these and add a sort order so I end up with the following

Array
(
    [red] => Array
        (
            [type] => "colour"
            [sort] => 1
        )
    [green] => Array
        (
            [type] => "colour"
            [sort] => 2
        )
    [blue] => Array
        (
            [type] => "colour"
            [sort] => 3
        )
)

So far I have only managed:

$additional_data = array_pad([], count($myArr), ['type_id' => $type_id]);
$data = array_combine($myArr, $additional_data);

which is yielding:

Array
(
    [red] => Array
        (
            [type] => "colour"
        )
    [green] => Array
        (
            [type] => "colour"
        )
    [blue] => Array
        (
            [type] => "colour"
        )
)

I know I can do it by iterating through colours, but wondered if it could be done without a loop.

Thanks

I done this, hope can help

$myArr = ["red", "green", "blue"];
$type = "colour";

$x = array();
for($i=0;$i<count($myArr);$i++){
$x[$myArr[$i]]=array();
$x[$myArr[$i]]["type"]=$type;
$x[$myArr[$i]]["sort"]=$i;
}
var_dump($x);

The version which you posted to solve this problem always has 1 as the sort_order.

$myArr = ["red", "green", "blue"];
$type_id = "colour";
$sort = 0;

$additional_data = array_pad([], count($myArr), ['type_id' => $type_id, 'sort_order' => ++$sort]);
$data = array_combine($myArr, $additional_data);
print_r($data);

outputs...

Array
(
    [red] => Array
        (
            [type_id] => colour
            [sort_order] => 1
        )

    [green] => Array
        (
            [type_id] => colour
            [sort_order] => 1
        )

    [blue] => Array
        (
            [type_id] => colour
            [sort_order] => 1
        )

)

You could then process the result with array_walk to correct the values...

$myArr = ["red", "green", "blue"];
$type_id = "colour";
$sort = 0;

$additional_data = array_pad([], count($myArr), ['type_id' => $type_id, 'sort_order' => ++$sort]);
$data = array_combine($myArr, $additional_data);
$sort = 1;
array_walk($data, function (&$item, $key) use(&$sort) {
    $item['sort_order'] = $sort++;
});

print_r($data);

Which corrects it to.

Array
(
    [red] => Array
        (
            [type_id] => colour
            [sort_order] => 1
        )

    [green] => Array
        (
            [type_id] => colour
            [sort_order] => 2
        )

    [blue] => Array
        (
            [type_id] => colour
            [sort_order] => 3
        )

)

I figured it out. Pretty simple really.

$myArr = ["red", "green", "blue"];
$type = "colour";
$sort = 0;

$additional_data = array_pad([], count($myArr), ['type_id' => $type_id, 'sort_order' => ++$sort]);
$data = array_combine($myArr, $additional_data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM