php - Change keys in each row of a 2d array without losing values

354

I have an array of rows where one (visual) column of data has two similar but different keys. I would like to replace one of the keys so that the column has the same key in all rows.

My input array:

[
    ['Ttitle' => 'lilly', 'Price' => 1.75, 'Number' => 3],
    ['Title' => 'rose',   'Price' => 1.25, 'Number' => 15],
    ['Title' => 'daisy',  'Price' => 0.75, 'Number' => 25],
    ['Title' => 'nettle', 'Price' => 2.75, 'Number' => 33],
    ['Title' => 'orchid', 'Price' => 1.15, 'Number' => 7],
];

My desired result: (notice the key change forlilly)

[
    ['Title' => 'lilly',  'Price' => 1.75, 'Number' => 3],
    ['Title' => 'rose',   'Price' => 1.25, 'Number' => 15],
    ['Title' => 'daisy',  'Price' => 0.75, 'Number' => 25],
    ['Title' => 'nettle', 'Price' => 2.75, 'Number' => 33],
    ['Title' => 'orchid', 'Price' => 1.15, 'Number' => 7],
];

When I attempted to usearray_map(), I manage to change all the keys toTitle, but I also delete allTitle values except for theTtitle key I just changed toTitle.

Code is as follows:

if (!empty($notmatchingarray))
{
    $completearray = array_merge($notmatchingarray, $datastuff2);

    $completearray = array_map(
        function($complete) {
            return array(
                'Title' => $complete['Ttitle'],
                'Price' => $complete['Price'],
                'Number' => $complete['Number'],
            );
        },
        $completearray
    );
    print_r($completearray);
}

Am I doing the wrong thing in using thisarray_map() function? Should I be doing something else?

710

Answer

Solution:

Better ways but I'm headed out. With your existing code you need to setTtitle orTitle whichever is present:

$completearray = array_map(function($complete) {
        $title = isset($complete['Ttitle']) ? $complete['Ttitle'] : $complete['Title'];
        return array(
           'Title' => $title,
           'Price' => $complete['Price'],
           'Number' => $complete['Number'],
       );
}, $completearray);

Ternary operator is a shortcut for a simpleif /else, so:

if(isset($complete['Ttitle'])) {
    $title = $complete['Ttitle']; //if Ttitle is set use it
} else {
    $title = $complete['Title'];  //if not use Title
}
759

Answer

Solution:

Your rows of data are statically positioned, so honestly, you can just use iterated calls ofarray_combine() with your preferred keys.

If your row elements are not predictably positioned, then you can simplify/modernize the earlier posted answers with the null coalescing operator.

Codes: (Demo)

var_export(
    array_map(
        fn($row) => array_combine(['Title', 'Price', 'Number'], $row),
        $array
    )
);

Or:

var_export(
    array_map(
        fn($row) => ['Title' => $row['Ttitle'] ?? $row['Title'], 'Price' => $row['Price'], 'Number' => $row['Number']],
        $array
    )
);

People are also looking for solutions to the problem: Efficiency of using foreach loops to clear a PHP array's values

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.