PHP - efficient way of adding multidimensional array values to a specific key

Andy

I have a multi-dimensional array which contains some ID's based on filters a user has chosen to "find" or "exclude" from a search. Each set of filters is grouped by a key (65 in the example below):

$cache_data = ['filters' => [
        65 => [
            'find' => [
                167
            ],
            'exclude' => [
                169,
                171
            ]
        ]
    ]
];

I want to add some more ID's to the find array whilst retaining any that are already there: 167 in this case. The values in the exclude array need to remain untouched. Assume I want to add the following 4 values to find:

$to_be_added = [241, 242, 285, 286];

I need to target the filters based on their group ID (65 in this case) and merge in my new values using array_merge():

$existing_filters = ($cache_data['filters'][65]);
$merged = array_merge($existing_filters['find'], $to_be_added);

I then rewrite $cache_data['filters'][65] by using $merged with the find key, and keep the values that were already there in exclude:

$cache_data['filters'][65] = [ 
        'find' => $merged,
        'exclude' => $existing_filters['exclude']
    ];

The output for this, print_r($cache_data['filters'][65]); is exactly as I want:

Array
(
    [find] => Array
        (
            [0] => 167
            [1] => 241
            [2] => 242
            [3] => 285
            [4] => 286
        )

    [exclude] => Array
        (
            [0] => 169
            [1] => 171
        )

)

However I'm wondering if there is an easier or more efficient way to achieve the same thing?

Using PHP 7.2.10

u_mulder

Oneliner:

$cache_data['filters'][65]['find'] = array_merge(
    $cache_data['filters'][65]['find'], 
    $to_be_added
);

Using

$cache_data['filters'][65]['find'] += $to_be_added;

is not safe because in this case key value 241 which is under key 0 will be ignored, as $cache_data['filters'][65]['find'] already has key 0 with value 167.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

PHP Multidimensional Array Extract Specific Values Based Upon Key

Remove duplicate values of specific key in multidimensional array in PHP but remove the last?

Adding values to a multidimensional associative array in php

PHP - Get all values with specific array key from multidimensional array with unknown depth

PHP explode string key into multidimensional array with values

Group multidimensional array by key and sum values in PHP

Compare multidimensional array values by key in php

Add array values with key to multidimensional array without adding a new 'layer'

Faster way of calulating values in multidimensional array in php?

PHP: Most Efficient way in initializing large amount of multidimensional array?

PHP Adding array to multidimensional array at key inside foreach

PHP get specific value of a specific key in a multidimensional array

Adding multiple values to same key in array php

Get specific values from randomized multidimensional array with url key

Adding values to multidimensional array PHP without overwritting last added value

Merge array depending key values in a multidimensional array in PHP

Copying specific keys and values of a PHP multidimensional array into another array

adding end values in a multidimensional array

Grouping and adding values multidimensional array

adding values to multidimensional aray in PHP

PHP Multidimensional Array Searching (Find key by specific value)

PHP, getting $value out of a specific $key in associative multidimensional array

Check for specific key values in array - PHP

PHP count values in multidimensional-array where key and value equals

PHP: Addition of values in multidimensional array where key equal to and equal to

Build multidimensional array from string naming key values in php

Grouping values by Key, Multidimensional Array inside a Foreach Loop - PHP

How to get sum of values in specific index of multidimensional array using php?

PHP - Count number of values in a specific index of multidimensional array