-2

$array_one

Array (
    [2018] => Array ()

    [2019] => Array
        (
            [5] => 2966
        )

    [2020] => Array
        (
            [0] => 2930
            [1] => 2919
        )
)

2nd array is :

$costs

Array
(
    [2018] => Array
        (
            [2789] => Array
                (
                    [jh] => 0
                    [presta] => 0
                    [log] => 0
                )

            [2874] => Array
                (
                    [jh] => 0.5
                    [presta] => 1
                    [log] => 0
                )

            [3786] => Array
                (
                    [jh] => 7
                    [presta] => 0
                    [log] => 0
                )

            [315] => Array
                (
                    [jh] => 0
                    [presta] => 0
                    [log] => 0
                )

            [325] => Array
                (
                    [jh] => 0
                    [presta] => 0
                    [log] => 0
                )

            [3793] => Array
                (
                    [jh] => 0.5
                    [presta] => 1.2
                    [log] => 0
                )

            [3796] => Array
                (
                    [jh] => 22
                    [presta] => 27.4
                    [log] => 0
                )

            [3798] => Array
                (
                    [jh] => 0
                    [presta] => 0
                    [log] => 0
                )

            [3800] => Array
                (
                    [jh] => 17
                    [presta] => 0
                    [log] => 0
                )

            [3832] => Array
                (
                    [jh] => 2
                    [presta] => 9
                    [log] => 0
                )

        )

    [2019] => Array
        (
            [2930] => Array
                (
                    [jh] => 1
                    [presta] => 0
                    [log] => 0
                )

            [3786] => Array
                (
                    [jh] => 4
                    [presta] => 0
                    [log] => 0
                )

            [315] => Array
                (
                    [jh] => 0
                    [presta] => 0
                    [log] => 0
                )

            [3796] => Array
                (
                    [jh] => 14
                    [presta] => 44
                    [log] => 0
                )

            [3834] => Array
                (
                    [jh] => 0
                    [presta] => 0
                    [log] => 0
                )

            [2966] => Array
                (
                    [jh] => 0
                    [presta] => 0
                    [log] => 0
                )

        )

    [2020] => Array
        (
            [2930] => Array
                (
                    [jh] => 0
                    [presta] => 0
                    [log] => 0
                )

            [2919] => Array
                (
                    [jh] => 0
                    [presta] => 0
                    [log] => 0
                )

        )

)

I want to only keep those records in my 2nd array which are in 1st array and I want to unset remaining records from 2nd array

How to keep only those records in 2nds array which match the key values in 1st array?

1
  • 1
    Just use a couple of foreach loops and check the indexes match, if not use unset() Commented Aug 12, 2019 at 9:21

2 Answers 2

1

This is a looping version of the code in this answer:

$output = array();
foreach ($array_one as $key => $keys) {
    if (count($keys))
        $output[$key] = array_intersect_key($costs[$key], array_flip($keys));
}
print_r($output);

Output:

Array
(
    [2019] => Array
        (
            [2966] => Array
                (
                    [jh] => 0
                    [presta] => 0
                    [log] => 0
                )
        )
    [2020] => Array
        (
            [2930] => Array
                (
                    [jh] => 0
                    [presta] => 0
                    [log] => 0
                )    
            [2919] => Array
                (
                    [jh] => 0
                    [presta] => 0
                    [log] => 0
                )    
        )    
)

Demo on 3v4l.org

Sign up to request clarification or add additional context in comments.

Comments

0

You can use array_intersect_key,

foreach($costs as $k => &$val) // & to save value to value itself
{
    if(!empty($array_one[$k])){
        $val = array_intersect_key($val, $array_one[$k]);
    }
}

This will reduce no of iterations,

$result=[];
foreach ($array_one as $k => $val) {
    if (!empty($val))
        $result[$k] = array_intersect_key($costs[$k], array_flip($val));
}
print_r($result);

Ultimately, it will only keep only those index's data which is matching with array_one

Comments

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.