1

I have a two-dimensional array of this format:

$oldArr = [
    0 => [
        "color" => "red",
        "shape" => "circle",
        "size" => "small",
    ],
    1 => [
        "color" => "green",
        "shape" => "square",
        "size" => "large",
    ],
    2 => [
        "color" => "yellow",
        "shape" => "triangle",
        "size" => "large",
    ],
];

And a one-dimensional array of this format:

$newVals = [
    0 => "large",
    1 => "large",
    2 => "small",
];

I'm attempting to use str_replace() to iterate through each "size" value in $oldArr, and replace it with the value in $newVals that matches its position. Since these arrays will always have the same number of top-level key-value pairs, I'm basically trying to take $newVals and map it onto each $oldArr["size"] value. The end result should be

$newArr = [
    0 => [
        "color" => "red",
        "shape" => "circle",
        "size" => "large",
    ],
    1 => [
        "color" => "green",
        "shape" => "square",
        "size" => "large",
    ],
    2 => [
        "color" => "yellow",
        "shape" => "triangle",
        "size" => "small",
    ],
];

Can anyone recommend the best way to go about this? I've attempted a str_replace in a foreach loop, but it hasn't worked:

foreach($oldArr as $entity):

    str_replace($entity['size'], $newVals, $entity);

endforeach;
1

3 Answers 3

1

You can use array_map() and loop through both arrays at once and instead of using the size value of your original array, you just use the new one, e.g.

$result = array_map(function($old, $new){
    return ["color" => $old["color"], "shape" => $old["shape"], "size" => $new];
}, $oldArr, $newVals);
Sign up to request clarification or add additional context in comments.

Comments

1

You can use this code:

<?php

$oldArr = [
    0 => [
        "color" => "red",
        "shape" => "circle",
        "size" => "small",
    ],
    1 => [
        "color" => "green",
        "shape" => "square",
        "size" => "large",
    ],
    2 => [
        "color" => "yellow",
        "shape" => "triangle",
        "size" => "large",
    ],
];


$newVals = [
    0 => "large",
    1 => "large",
    2 => "small",
];

$newArr = array();
foreach($oldArr as $key => $entity){
    $newEntity = $entity;
    $newEntity['size'] = $newVals[$key];
    $newArr[$key] = $newEntity;
}

var_dump($newArr);

Comments

0

array_walk() modifying the original array: (Demo)

array_walk(
    $oldArr,
    fn(&$row, $i) => $row['size'] = $newVals[$i]
);
var_export($oldArr);

foreach() modifying the original array: (Demo)

foreach ($oldArr as $i => ['size' => &$size]) {
    $size = $newVals[$i];
}
var_export($oldArr);

foreach() to produce a new array: (Demo)

$result = [];
foreach ($oldArr as $i => $row) {
    $row['size'] = $newVals[$i];
    $result[] = $row;
}
var_export($result);

array_map() to synchronously iterate both arrays and avoid declaring a result array: (Demo)

var_export(
    array_map(
        fn($row, $size) => array_replace($row, ['size' => $size]),
        $oldArr,
        $newVals
    )
);

*Because the rows to be joined contain associative/non-numeric keys, array_merge() and array_replace() behave identically. The array union operator (+) can not be used for this task because it retains the left side elements where both payloads contain the same key -- in other words, there would be no change.


array_replace_recursive() after mutating the new values into a 2d array: (Demo)

var_export(
    array_replace_recursive(
        $oldArr,
        array_map(fn($size) => ['size' => $size], $newVals)
    )
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.