Home > database >  PHP get arrays from one and add to the property of another
PHP get arrays from one and add to the property of another

Time:11-15

I have two arrays. One is taxonomies:

Array
(
    [genres] => Array
        (
            [label] => Genres
            [value] => genres
        )

    [movie_tags] => Array
        (
            [label] => Movie Tags
            [value] => movie_tags
        )

    [portfolio_category] => Array
        (
            [label] => Portfolio Categories
            [value] => portfolio_category
        )

)

and postTypes:

Array
(
    [movies] => Array
        (
            [0] => genres
            [1] => movie_tags
        )

    [portfolio] => Array
        (
            [0] => portfolio_category
        )

)

Is it possible to combine the two arrays to get something like this?

Array
(
    [movies] => Array
        (
            [0] => Array ( [label] => Genres [value] => genres )
            [1] => Array ( [label] => Movie Tags [value] => movie_tags )
        )

    [portfolio] => Array
        (
            [0] => Array ( [label] => Portfolio Categories [value] => portfolio_category )
        )

)

I'm trying to add the label and value from taxonomies array into postTypes array but I'm not sure how to match the keys and combine the arrays. Is this possible with a foreach loop?

CodePudding user response:

Since the values in $postTypes match the keys in $taxonomies it seems easiest to loop over the $postTypes array, and assuming these will only have one level of nesting, loop over the inner arrays.

If we loop over the inner arrays, the values of those will be equivalent to the keys in $taxonomies, that means that if we access $taxonomies[$value] we should get the array we want in our final result.

Thus, we could replace the values in place when looping over $postTypes as follows:

// Assigning the inner array by reference to update it in place.
foreach ($postTypes as &$innerArray) {
    foreach ($innerArray as $index => $value) {
        // Under the assumption that the values in postTypes
        // will always match the keys of taxonomies. Otherwise
        // you could add an additional check and logic.
        $innerArray[$index] = $taxonomies[$value];
    }
}

unset($innerArray);

echo '<pre>', print_r($postTypes, true), '</pre>';

The result we get:

(
    [movies] => Array
        (
            [0] => Array
                (
                    [label] => Genres
                    [value] => genres
                )

            [1] => Array
                (
                    [label] => Movie Tags
                    [value] => movie_tags
                )

        )

    [portfolio] => Array
        (
            [0] => Array
                (
                    [label] => Portfolio Categories
                    [value] => portfolio_category
                )

        )

)

CodePudding user response:

You can achieve this using array_map function twice for external and internal arrays:

$res = array_map( // map external array
    function($el) use($taxonomies) {
        return array_map( // map internal array
            function($el) use ($taxonomies) {
                return $taxonomies[$el];
            },
            $el
        );
    },
    $postTypes
);

PHP array_map online

CodePudding user response:

Using array_intersect_key() will prevent any warnings from missing elements in your lookup array.

Code: (Demo)

$result = [];
foreach ($postTypes as $group => $taxKeys) {
    $result[$group] = array_values(
        array_intersect_key(
            $taxonomies,
            array_flip($taxKeys)
        )
    );
}
var_export($result);
  • Related