1

I have been trying to create a distinct array of country code that will take all date which is for only that country and add inside that array using a multi dimensional array in PHP but I am unable to archive.

Input Array

Array
(
    [0] => Array
        (
            [0] => 2019-01-01
            [1] => lt
            [2] => passport
            [3] => 30122719
            [4] => 2019-03-01
            [5] => 357717289
        )

    [1] => Array
        (
            [0] => 2019-01-01
            [1] => pl
            [2] => identity_card
            [3] => 9879386836
            [4] => 2018-11-01
            [5] => 643023760
        )

    [2] => Array
        (
            [0] => 2019-01-02
            [1] => lt
            [2] => passport
            [3] => 46530663
            [4] => 2019-03-01
            [5] => 357717289
        )

    [3] => Array
        (
            [0] => 2019-01-02
            [1] => pl
            [2] => identity_card
            [3] => 4531480055
            [4] => 2017-10-21
            [5] => 324444899
        )

    [4] => Array
        (
            [0] => 2019-01-03
            [1] => lt
            [2] => passport
            [3] => 54163812
            [4] => 2019-03-01
            [5] => 357717289
        )

    [5] => Array
        (
            [0] => 2019-01-03
            [1] => fr
            [2] => drivers_license
            [3] => 95180604
            [4] => 2018-07-02
            [5] => 942959784
        )
)

Expected output

Array
(
    [lt] => Array
        (
            [0] => 2019-03-01
            [1] => 2019-03-01
            [2] => 2019-03-01
        )

    [pl] => Array
        (
            [0] => 2018-11-01
            [1] => 2017-10-21
        )

    [fr] => Array
        (
            [0] => 2018-07-02
        )

)

So far I have tried the following

$dist = array();
foreach ($inputArray as $key => $value) {
     foreach ($value as $index => $v) {
          $dist[$value[1]] = $value[4];
     }
}

An the code returns the following

Array
(
    [lt] => 2019-03-01
    [pl] => 2016-08-01
    [fr] => 2019-04-01
    [de] => 2009-01-01
    [uk] => 2015-09-07
    [es] => 2015-09-08
    [it] => 2019-02-16
)

1 Answer 1

2

You are very close - you just miss the [] operator which append element to array.

See this:

$dist = array();
$cc = array('fr', 'pl', 'lt');
foreach ($inputArray as $key => $val) {
     if (in_array($val[1], $cc) { //get only the county code you want
          $dist[$val[1]][] = $val[4]; // notice the change in this line
     }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @dWinder for your answer, however I also achieved this result but didn't understand how to apply condition to keep for only specific country code date and achieve my final expected output
Added if condition. Notice your example input has only those 3 country code

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.