0

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
)

1 Answer 1

1

Use array_intersect():

$result = array_intersect($array["brandname"][0], $array["productsubcategory"][1]);
print_r($result);
Sign up to request clarification or add additional context in comments.

1 Comment

As I have mentioned that keys are not static in this case its brandname and productsubcategory is coming but there might be a case where the keys will be size, processor etc... and event the number of keys are not fixed

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.