简体   繁体   中英

Add new column to the end of a multidimensional array using an array of values

I am trying to add values from one array as new column values on another array but only on the subarrays at the end and only as needed.

I have tried to add a new column to $array with functions like array_push() and array_combine() but they don't work as desired.

My input data is:

$array = [
    ["item_id" => "1", "item_name" => "a"],
    ["item_id" => "2", "item_name" => "b"], 
    ["item_id" => "3", "item_name" => "c"], 
    ["item_id" => "4", "item_name" => "d"], 
    ["item_id" => "5", "item_name" => "e"], 
    ["item_id" => "6", "item_name" => "f"]
];

$val = [10, 11, 12];

I am trying to add 'value' => 10 , 'value' => 11 , and 'value => 12 to the last three rows in my multidimensional array respectively.

My desired output is:

$array = [
    ["item_id" => "1", "item_name" => "a"],
    ["item_id" => "2", "item_name" => "b"], 
    ["item_id" => "3", "item_name" => "c"], 
    ["item_id" => "4", "item_name" => "d", "value" => 10], 
    ["item_id" => "5", "item_name" => "e", "value" => 11], 
    ["item_id" => "6", "item_name" => "f", "value" => 12]
];

My current code looks like this:

for ($i = 0; $i < count($array); $i++) {
    $array[$i]['value'] = $val;
}

Unfortunately, this pushes all of the values into all of the rows.

Well, you iterate over the whole array and inject the new property only for those entries where applicable:

<?php
$entries = [
  [
    "item_id" => "1",
    "item_name" => "a"
  ], [
    "item_id" => "2",
    "item_name" => "b"
  ], [
    "item_id" => "3",
    "item_name" => "c"
  ], [
    "item_id" => "4",
    "item_name" => "d"
  ], [
    "item_id" => "5",
    "item_name" => "e"
  ], [
    "item_id" => "6",
    "item_name" => "f"
  ]
];

$values = [10, 11, 12];
$offset = count($entries) - count($values);

array_walk($entries, function(&$entry, $key) use ($values, $offset) {
  if ($key >= $offset) {
    $entry["value"] = $values[$key - $offset];
  }
});

print_r($entries);

The output obviously is:

Array
(
    [0] => Array
        (
            [item_id] => 1
            [item_name] => a
        )
    [1] => Array
        (
            [item_id] => 2
            [item_name] => b
        )
    [2] => Array
        (
            [item_id] => 3
            [item_name] => c
        )
    [3] => Array
        (
            [item_id] => 4
            [item_name] => d
            [value] => 10
        )
    [4] => Array
        (
            [item_id] => 5
            [item_name] => e
            [value] => 11
        )
    [5] => Array
        (
            [item_id] => 6
            [item_name] => f
            [value] => 12
        )
)

You should only iterate the array of new values so that you are not wasting effort traversing the rows of the master array that will go untouched.

It is important to calculate the point where you will begin to push the new column values. Subtract the count of the first array from the count of the second, then add that number to the indexes while you iterate the second array.

Code: ( Demo )

$bump = count($array) - count($val);
foreach ($val as $i => $number) {
    $array[$i + $bump]['value'] = $number;
}
var_export($array);

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