Sanket Utekar
Sanket Utekar

Reputation: 327

Get common values from a single multidimensional array in php

I have a array called $array which is multidimensional array. It consits of keys like brandname, productsubcategory (Keys are not static they can be anything and even number of keys are not fixed). I want all the common values from the array

Current array:

$array = array
(
    [brandname] => Array
        (
            [0] => Array
                (
                    [0] => sony_brandname
                    [1] => touch screen_type
                    [3] => 2.8_size
                    [4] => gsm + gsm_sim_support
                    [5] => 2_primary_camera
                    [6] => 64 mb_internal_memory
                    [7] => 32_expandable_memory
                    [8] => 1200_standard_battery_type
                    [9] => 3_size
                [10] => 1000_standard_battery_type
                [11] => touch and type_type         
                )

        )

    [productsubcategory] => Array
        (
            [1] => Array
                (
                    [0] => karbonn_brandname
                    [1] => touch and type_type
                    [2] => micromax_brandname
                    [3] => 2.8_size
                    [4] => gsm_sim_support
                    [5] => 3.15_primary_camera
                    [6] => 52 mb_internal_memory
                    [7] => 8_expandable_memory
                    [8] => 1000_standard_battery_type
                    [9] => nokia_brandname
                    [10] => symbian s40_os_clean
                    [11] => 5_primary_camera
                    [12] => 128 mb_ram
                    [13] => 256 mb_internal_memory
                    [14] => 32_expandable_memory
                    [15] => 1110_standard_battery_type
                    [16] => gsm + gsm_sim_support
                    [17] => 2_primary_camera
                    [18] => 32 mb_ram
                    [19] => 10 mb_internal_memory
                    [20] => no_expandable_memory
                    [21] => 1020_standard_battery_type
                    [22] => 680 mhz_processor
                    [23] => 64 mb_ram
                    [24] => 128 mb_internal_memory
                    [25] => 860_standard_battery_type
                    [26] => blackberry_brandname
                    [27] => 2.45_size
                    [28] => 1 ghz_processor
                )

        )

);

Desired array :

$array = array
(
    [0] => sony_brandname
    [1] => touch and type_type
    [2] => 2.8_size
    [8] => 1000_standard_battery_type
    [16] => gsm + gsm_sim_support
    [17] => 2_primary_camera
)

Upvotes: 0

Views: 740

Answers (1)

user2571870
user2571870

Reputation:

Use array_intersect():

$result = array_intersect($array["brandname"][0], $array["productsubcategory"][1]);
print_r($result);

Upvotes: 1

Related Questions