PHP Match by common key of two multidimentional array and merge the two array with both array key [duplicate]












0















This question already has an answer here:




  • PHP merge two arrays on the same key AND value

    5 answers




Good evening, I have a little bit problem. I have two array. like



$firstArr = Array(
[0] => Array(
[customer_id] => 11,
[home_delivery] => no,
),
[1] => Array(
[customer_id] => 12,
[home_delivery] => no,
),
[2] => Array(
[customer_id] => 13,
[home_delivery] => no,
),
);

$secondArr = Array(
[0] => Array(
[customer_id] => 11,
[test] => no,
[is_active] => yes,
),
[1] => Array(
[customer_id] => 22,
[test] => no,
[is_active] => yes,
),
);


Now i want to get the result like first array's customer_id match with the second array customer_id. Id two array's customer id is same the the value of second array add with first array otherwise the value will be null. Hope guys you got my point what i want. The output which i want is like the below.



$getResult = Array(
[0] => Array(
[customer_id] => 11,
[home_delivery] => no,
[test] => no,
[is_active] => yes,
),
[1] => Array(
[customer_id] => 12,
[home_delivery] => no,
[test] => '',
[is_active] => '',
),
[2] => Array(
[customer_id] => 13,
[home_delivery] => no,
[test] => '',
[is_active] => '',
),
);


I have tried by this code, but it doesnt work. Please help me.



$mergedArray = array();
foreach ($firstArr as $index1 => $value1) {
foreach ($secondArr as $index2 => $value2) {
if ($array1[$index1]['customer_id'] == $array2[$index2]['customer_id']) {
$mergedArray = array_merge($firstArr[$index1], $secondArr[$index2]);
}
}
}
echo "<pre>"; print_r($mergedArray); echo "</pre>";









share|improve this question













marked as duplicate by Community Nov 13 at 2:43


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • You can read this post where talk about almost your question stackoverflow.com/questions/37342992/…
    – Ferdinando
    Nov 12 at 18:17












  • Did you omit id=22 from the result array by accident, or do you only want items from the second array that are in the first array.
    – ArtisticPhoenix
    Nov 12 at 19:23












  • If these arrays happen to come from your database you'd be better off using a join instead of two separate queries.
    – Don't Panic
    Nov 12 at 19:50
















0















This question already has an answer here:




  • PHP merge two arrays on the same key AND value

    5 answers




Good evening, I have a little bit problem. I have two array. like



$firstArr = Array(
[0] => Array(
[customer_id] => 11,
[home_delivery] => no,
),
[1] => Array(
[customer_id] => 12,
[home_delivery] => no,
),
[2] => Array(
[customer_id] => 13,
[home_delivery] => no,
),
);

$secondArr = Array(
[0] => Array(
[customer_id] => 11,
[test] => no,
[is_active] => yes,
),
[1] => Array(
[customer_id] => 22,
[test] => no,
[is_active] => yes,
),
);


Now i want to get the result like first array's customer_id match with the second array customer_id. Id two array's customer id is same the the value of second array add with first array otherwise the value will be null. Hope guys you got my point what i want. The output which i want is like the below.



$getResult = Array(
[0] => Array(
[customer_id] => 11,
[home_delivery] => no,
[test] => no,
[is_active] => yes,
),
[1] => Array(
[customer_id] => 12,
[home_delivery] => no,
[test] => '',
[is_active] => '',
),
[2] => Array(
[customer_id] => 13,
[home_delivery] => no,
[test] => '',
[is_active] => '',
),
);


I have tried by this code, but it doesnt work. Please help me.



$mergedArray = array();
foreach ($firstArr as $index1 => $value1) {
foreach ($secondArr as $index2 => $value2) {
if ($array1[$index1]['customer_id'] == $array2[$index2]['customer_id']) {
$mergedArray = array_merge($firstArr[$index1], $secondArr[$index2]);
}
}
}
echo "<pre>"; print_r($mergedArray); echo "</pre>";









share|improve this question













marked as duplicate by Community Nov 13 at 2:43


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • You can read this post where talk about almost your question stackoverflow.com/questions/37342992/…
    – Ferdinando
    Nov 12 at 18:17












  • Did you omit id=22 from the result array by accident, or do you only want items from the second array that are in the first array.
    – ArtisticPhoenix
    Nov 12 at 19:23












  • If these arrays happen to come from your database you'd be better off using a join instead of two separate queries.
    – Don't Panic
    Nov 12 at 19:50














0












0








0








This question already has an answer here:




  • PHP merge two arrays on the same key AND value

    5 answers




Good evening, I have a little bit problem. I have two array. like



$firstArr = Array(
[0] => Array(
[customer_id] => 11,
[home_delivery] => no,
),
[1] => Array(
[customer_id] => 12,
[home_delivery] => no,
),
[2] => Array(
[customer_id] => 13,
[home_delivery] => no,
),
);

$secondArr = Array(
[0] => Array(
[customer_id] => 11,
[test] => no,
[is_active] => yes,
),
[1] => Array(
[customer_id] => 22,
[test] => no,
[is_active] => yes,
),
);


Now i want to get the result like first array's customer_id match with the second array customer_id. Id two array's customer id is same the the value of second array add with first array otherwise the value will be null. Hope guys you got my point what i want. The output which i want is like the below.



$getResult = Array(
[0] => Array(
[customer_id] => 11,
[home_delivery] => no,
[test] => no,
[is_active] => yes,
),
[1] => Array(
[customer_id] => 12,
[home_delivery] => no,
[test] => '',
[is_active] => '',
),
[2] => Array(
[customer_id] => 13,
[home_delivery] => no,
[test] => '',
[is_active] => '',
),
);


I have tried by this code, but it doesnt work. Please help me.



$mergedArray = array();
foreach ($firstArr as $index1 => $value1) {
foreach ($secondArr as $index2 => $value2) {
if ($array1[$index1]['customer_id'] == $array2[$index2]['customer_id']) {
$mergedArray = array_merge($firstArr[$index1], $secondArr[$index2]);
}
}
}
echo "<pre>"; print_r($mergedArray); echo "</pre>";









share|improve this question














This question already has an answer here:




  • PHP merge two arrays on the same key AND value

    5 answers




Good evening, I have a little bit problem. I have two array. like



$firstArr = Array(
[0] => Array(
[customer_id] => 11,
[home_delivery] => no,
),
[1] => Array(
[customer_id] => 12,
[home_delivery] => no,
),
[2] => Array(
[customer_id] => 13,
[home_delivery] => no,
),
);

$secondArr = Array(
[0] => Array(
[customer_id] => 11,
[test] => no,
[is_active] => yes,
),
[1] => Array(
[customer_id] => 22,
[test] => no,
[is_active] => yes,
),
);


Now i want to get the result like first array's customer_id match with the second array customer_id. Id two array's customer id is same the the value of second array add with first array otherwise the value will be null. Hope guys you got my point what i want. The output which i want is like the below.



$getResult = Array(
[0] => Array(
[customer_id] => 11,
[home_delivery] => no,
[test] => no,
[is_active] => yes,
),
[1] => Array(
[customer_id] => 12,
[home_delivery] => no,
[test] => '',
[is_active] => '',
),
[2] => Array(
[customer_id] => 13,
[home_delivery] => no,
[test] => '',
[is_active] => '',
),
);


I have tried by this code, but it doesnt work. Please help me.



$mergedArray = array();
foreach ($firstArr as $index1 => $value1) {
foreach ($secondArr as $index2 => $value2) {
if ($array1[$index1]['customer_id'] == $array2[$index2]['customer_id']) {
$mergedArray = array_merge($firstArr[$index1], $secondArr[$index2]);
}
}
}
echo "<pre>"; print_r($mergedArray); echo "</pre>";




This question already has an answer here:




  • PHP merge two arrays on the same key AND value

    5 answers








php






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 18:09









S Sopno

63




63




marked as duplicate by Community Nov 13 at 2:43


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Community Nov 13 at 2:43


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • You can read this post where talk about almost your question stackoverflow.com/questions/37342992/…
    – Ferdinando
    Nov 12 at 18:17












  • Did you omit id=22 from the result array by accident, or do you only want items from the second array that are in the first array.
    – ArtisticPhoenix
    Nov 12 at 19:23












  • If these arrays happen to come from your database you'd be better off using a join instead of two separate queries.
    – Don't Panic
    Nov 12 at 19:50


















  • You can read this post where talk about almost your question stackoverflow.com/questions/37342992/…
    – Ferdinando
    Nov 12 at 18:17












  • Did you omit id=22 from the result array by accident, or do you only want items from the second array that are in the first array.
    – ArtisticPhoenix
    Nov 12 at 19:23












  • If these arrays happen to come from your database you'd be better off using a join instead of two separate queries.
    – Don't Panic
    Nov 12 at 19:50
















You can read this post where talk about almost your question stackoverflow.com/questions/37342992/…
– Ferdinando
Nov 12 at 18:17






You can read this post where talk about almost your question stackoverflow.com/questions/37342992/…
– Ferdinando
Nov 12 at 18:17














Did you omit id=22 from the result array by accident, or do you only want items from the second array that are in the first array.
– ArtisticPhoenix
Nov 12 at 19:23






Did you omit id=22 from the result array by accident, or do you only want items from the second array that are in the first array.
– ArtisticPhoenix
Nov 12 at 19:23














If these arrays happen to come from your database you'd be better off using a join instead of two separate queries.
– Don't Panic
Nov 12 at 19:50




If these arrays happen to come from your database you'd be better off using a join instead of two separate queries.
– Don't Panic
Nov 12 at 19:50












2 Answers
2






active

oldest

votes


















0














You can do this :



<?php 

$results = ;
// Get all unique keys from both arrays
$keys = array_unique(array_merge(array_keys($firstArr[0]), array_keys($secondArr[0])));

// Make array of common customer_ids
foreach (array_merge($firstArr, $secondArr) as $record) {
$results[$record['customer_id']] = isset($results[$record['customer_id']]) ? array_merge($results[$record['customer_id']], $record) : $record;
}
// Fill keys which are not present with blank strings
foreach ($keys as $key) {
foreach ($results as $index => $result) {
if(!array_key_exists($key, $result)){
$results[$index][$key] = '';
}
}
}
print_r($results);





share|improve this answer





























    0














    This is how I would do it:



    $firstArr = array (
    0 =>
    array (
    'customer_id' => 11,
    'home_delivery' => 'no'
    ),
    1 =>
    array (
    'customer_id' => 12,
    'home_delivery' => 'no'
    ),
    2 =>
    array (
    'customer_id' => 13,
    'home_delivery' => 'no'
    )
    );

    $secondArr = array (
    0 =>
    array (
    'customer_id' => 11,
    'test' => 'no',
    'is_active' => 'yes'
    ),
    1 =>
    array (
    'customer_id' => 22,
    'test' => 'no',
    'is_active' => 'yes'
    )
    );

    $secondKey = array_column($secondArr,'customer_id');

    foreach($firstArr as &$value){
    $idx2 = array_search($value['customer_id'], $secondKey);

    $value = array_merge($value, [
    'test' => false !== $idx2 ? $secondArr[$idx2]['test'] : '',
    'is_active' => false !== $idx2 ? $secondArr[$idx2]['is_active'] : '',
    ]);
    }
    print_r($firstArr);


    Output:



    Array
    (
    [0] => Array
    (
    [customer_id] => 11
    [home_delivery] => no
    [test] => no
    [is_active] => yes
    )

    [1] => Array
    (
    [customer_id] => 12
    [home_delivery] => no
    [test] =>
    [is_active] =>
    )

    [2] => Array
    (
    [customer_id] => 13
    [home_delivery] => no
    [test] =>
    [is_active] =>
    )

    )


    Sandbox



    There are 2 "tricks" I use here, the first, and more important one, is array_column this picks just one column from an array, but the thing is the keys in the resulting array will match the original array. Which we can take advantage of.



    The array we get from array column looks like this:



    array (
    0 => 11,
    1 => 22
    );


    Because the keys match the original array we can use array_search (with the ID) to look up that key, which we can then use in the original array. This gives us an "easier" way to search the second array by flattening it out.



    So for example when we look up $firstArr['customer_id'] = 11 in the above array we get the key 0 (which is not boolean false, see below). Then we can take that index and use it for the original array $secondArr and get the values from the other 2 columns.



    -Note- that array search returns boolean false when it cant find the item, because PHP treats 0 and false the same we have to do a strict type check !== instead of just !=. Otherwise PHP will confuse the 0 index with being false, which is not something we want.



    The second "trick" is use & in the foreach value, this is by reference which allows us to modify the array used in the loop, directly. This is optional as you could just as easily create a new array instead. But I thought I would show it as an option.






    share|improve this answer























    • thankz a lot..Its working..sorry for late reply..:)
      – S Sopno
      Nov 13 at 2:42


















    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You can do this :



    <?php 

    $results = ;
    // Get all unique keys from both arrays
    $keys = array_unique(array_merge(array_keys($firstArr[0]), array_keys($secondArr[0])));

    // Make array of common customer_ids
    foreach (array_merge($firstArr, $secondArr) as $record) {
    $results[$record['customer_id']] = isset($results[$record['customer_id']]) ? array_merge($results[$record['customer_id']], $record) : $record;
    }
    // Fill keys which are not present with blank strings
    foreach ($keys as $key) {
    foreach ($results as $index => $result) {
    if(!array_key_exists($key, $result)){
    $results[$index][$key] = '';
    }
    }
    }
    print_r($results);





    share|improve this answer


























      0














      You can do this :



      <?php 

      $results = ;
      // Get all unique keys from both arrays
      $keys = array_unique(array_merge(array_keys($firstArr[0]), array_keys($secondArr[0])));

      // Make array of common customer_ids
      foreach (array_merge($firstArr, $secondArr) as $record) {
      $results[$record['customer_id']] = isset($results[$record['customer_id']]) ? array_merge($results[$record['customer_id']], $record) : $record;
      }
      // Fill keys which are not present with blank strings
      foreach ($keys as $key) {
      foreach ($results as $index => $result) {
      if(!array_key_exists($key, $result)){
      $results[$index][$key] = '';
      }
      }
      }
      print_r($results);





      share|improve this answer
























        0












        0








        0






        You can do this :



        <?php 

        $results = ;
        // Get all unique keys from both arrays
        $keys = array_unique(array_merge(array_keys($firstArr[0]), array_keys($secondArr[0])));

        // Make array of common customer_ids
        foreach (array_merge($firstArr, $secondArr) as $record) {
        $results[$record['customer_id']] = isset($results[$record['customer_id']]) ? array_merge($results[$record['customer_id']], $record) : $record;
        }
        // Fill keys which are not present with blank strings
        foreach ($keys as $key) {
        foreach ($results as $index => $result) {
        if(!array_key_exists($key, $result)){
        $results[$index][$key] = '';
        }
        }
        }
        print_r($results);





        share|improve this answer












        You can do this :



        <?php 

        $results = ;
        // Get all unique keys from both arrays
        $keys = array_unique(array_merge(array_keys($firstArr[0]), array_keys($secondArr[0])));

        // Make array of common customer_ids
        foreach (array_merge($firstArr, $secondArr) as $record) {
        $results[$record['customer_id']] = isset($results[$record['customer_id']]) ? array_merge($results[$record['customer_id']], $record) : $record;
        }
        // Fill keys which are not present with blank strings
        foreach ($keys as $key) {
        foreach ($results as $index => $result) {
        if(!array_key_exists($key, $result)){
        $results[$index][$key] = '';
        }
        }
        }
        print_r($results);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 18:39









        Mihir Bhende

        2,0141515




        2,0141515

























            0














            This is how I would do it:



            $firstArr = array (
            0 =>
            array (
            'customer_id' => 11,
            'home_delivery' => 'no'
            ),
            1 =>
            array (
            'customer_id' => 12,
            'home_delivery' => 'no'
            ),
            2 =>
            array (
            'customer_id' => 13,
            'home_delivery' => 'no'
            )
            );

            $secondArr = array (
            0 =>
            array (
            'customer_id' => 11,
            'test' => 'no',
            'is_active' => 'yes'
            ),
            1 =>
            array (
            'customer_id' => 22,
            'test' => 'no',
            'is_active' => 'yes'
            )
            );

            $secondKey = array_column($secondArr,'customer_id');

            foreach($firstArr as &$value){
            $idx2 = array_search($value['customer_id'], $secondKey);

            $value = array_merge($value, [
            'test' => false !== $idx2 ? $secondArr[$idx2]['test'] : '',
            'is_active' => false !== $idx2 ? $secondArr[$idx2]['is_active'] : '',
            ]);
            }
            print_r($firstArr);


            Output:



            Array
            (
            [0] => Array
            (
            [customer_id] => 11
            [home_delivery] => no
            [test] => no
            [is_active] => yes
            )

            [1] => Array
            (
            [customer_id] => 12
            [home_delivery] => no
            [test] =>
            [is_active] =>
            )

            [2] => Array
            (
            [customer_id] => 13
            [home_delivery] => no
            [test] =>
            [is_active] =>
            )

            )


            Sandbox



            There are 2 "tricks" I use here, the first, and more important one, is array_column this picks just one column from an array, but the thing is the keys in the resulting array will match the original array. Which we can take advantage of.



            The array we get from array column looks like this:



            array (
            0 => 11,
            1 => 22
            );


            Because the keys match the original array we can use array_search (with the ID) to look up that key, which we can then use in the original array. This gives us an "easier" way to search the second array by flattening it out.



            So for example when we look up $firstArr['customer_id'] = 11 in the above array we get the key 0 (which is not boolean false, see below). Then we can take that index and use it for the original array $secondArr and get the values from the other 2 columns.



            -Note- that array search returns boolean false when it cant find the item, because PHP treats 0 and false the same we have to do a strict type check !== instead of just !=. Otherwise PHP will confuse the 0 index with being false, which is not something we want.



            The second "trick" is use & in the foreach value, this is by reference which allows us to modify the array used in the loop, directly. This is optional as you could just as easily create a new array instead. But I thought I would show it as an option.






            share|improve this answer























            • thankz a lot..Its working..sorry for late reply..:)
              – S Sopno
              Nov 13 at 2:42
















            0














            This is how I would do it:



            $firstArr = array (
            0 =>
            array (
            'customer_id' => 11,
            'home_delivery' => 'no'
            ),
            1 =>
            array (
            'customer_id' => 12,
            'home_delivery' => 'no'
            ),
            2 =>
            array (
            'customer_id' => 13,
            'home_delivery' => 'no'
            )
            );

            $secondArr = array (
            0 =>
            array (
            'customer_id' => 11,
            'test' => 'no',
            'is_active' => 'yes'
            ),
            1 =>
            array (
            'customer_id' => 22,
            'test' => 'no',
            'is_active' => 'yes'
            )
            );

            $secondKey = array_column($secondArr,'customer_id');

            foreach($firstArr as &$value){
            $idx2 = array_search($value['customer_id'], $secondKey);

            $value = array_merge($value, [
            'test' => false !== $idx2 ? $secondArr[$idx2]['test'] : '',
            'is_active' => false !== $idx2 ? $secondArr[$idx2]['is_active'] : '',
            ]);
            }
            print_r($firstArr);


            Output:



            Array
            (
            [0] => Array
            (
            [customer_id] => 11
            [home_delivery] => no
            [test] => no
            [is_active] => yes
            )

            [1] => Array
            (
            [customer_id] => 12
            [home_delivery] => no
            [test] =>
            [is_active] =>
            )

            [2] => Array
            (
            [customer_id] => 13
            [home_delivery] => no
            [test] =>
            [is_active] =>
            )

            )


            Sandbox



            There are 2 "tricks" I use here, the first, and more important one, is array_column this picks just one column from an array, but the thing is the keys in the resulting array will match the original array. Which we can take advantage of.



            The array we get from array column looks like this:



            array (
            0 => 11,
            1 => 22
            );


            Because the keys match the original array we can use array_search (with the ID) to look up that key, which we can then use in the original array. This gives us an "easier" way to search the second array by flattening it out.



            So for example when we look up $firstArr['customer_id'] = 11 in the above array we get the key 0 (which is not boolean false, see below). Then we can take that index and use it for the original array $secondArr and get the values from the other 2 columns.



            -Note- that array search returns boolean false when it cant find the item, because PHP treats 0 and false the same we have to do a strict type check !== instead of just !=. Otherwise PHP will confuse the 0 index with being false, which is not something we want.



            The second "trick" is use & in the foreach value, this is by reference which allows us to modify the array used in the loop, directly. This is optional as you could just as easily create a new array instead. But I thought I would show it as an option.






            share|improve this answer























            • thankz a lot..Its working..sorry for late reply..:)
              – S Sopno
              Nov 13 at 2:42














            0












            0








            0






            This is how I would do it:



            $firstArr = array (
            0 =>
            array (
            'customer_id' => 11,
            'home_delivery' => 'no'
            ),
            1 =>
            array (
            'customer_id' => 12,
            'home_delivery' => 'no'
            ),
            2 =>
            array (
            'customer_id' => 13,
            'home_delivery' => 'no'
            )
            );

            $secondArr = array (
            0 =>
            array (
            'customer_id' => 11,
            'test' => 'no',
            'is_active' => 'yes'
            ),
            1 =>
            array (
            'customer_id' => 22,
            'test' => 'no',
            'is_active' => 'yes'
            )
            );

            $secondKey = array_column($secondArr,'customer_id');

            foreach($firstArr as &$value){
            $idx2 = array_search($value['customer_id'], $secondKey);

            $value = array_merge($value, [
            'test' => false !== $idx2 ? $secondArr[$idx2]['test'] : '',
            'is_active' => false !== $idx2 ? $secondArr[$idx2]['is_active'] : '',
            ]);
            }
            print_r($firstArr);


            Output:



            Array
            (
            [0] => Array
            (
            [customer_id] => 11
            [home_delivery] => no
            [test] => no
            [is_active] => yes
            )

            [1] => Array
            (
            [customer_id] => 12
            [home_delivery] => no
            [test] =>
            [is_active] =>
            )

            [2] => Array
            (
            [customer_id] => 13
            [home_delivery] => no
            [test] =>
            [is_active] =>
            )

            )


            Sandbox



            There are 2 "tricks" I use here, the first, and more important one, is array_column this picks just one column from an array, but the thing is the keys in the resulting array will match the original array. Which we can take advantage of.



            The array we get from array column looks like this:



            array (
            0 => 11,
            1 => 22
            );


            Because the keys match the original array we can use array_search (with the ID) to look up that key, which we can then use in the original array. This gives us an "easier" way to search the second array by flattening it out.



            So for example when we look up $firstArr['customer_id'] = 11 in the above array we get the key 0 (which is not boolean false, see below). Then we can take that index and use it for the original array $secondArr and get the values from the other 2 columns.



            -Note- that array search returns boolean false when it cant find the item, because PHP treats 0 and false the same we have to do a strict type check !== instead of just !=. Otherwise PHP will confuse the 0 index with being false, which is not something we want.



            The second "trick" is use & in the foreach value, this is by reference which allows us to modify the array used in the loop, directly. This is optional as you could just as easily create a new array instead. But I thought I would show it as an option.






            share|improve this answer














            This is how I would do it:



            $firstArr = array (
            0 =>
            array (
            'customer_id' => 11,
            'home_delivery' => 'no'
            ),
            1 =>
            array (
            'customer_id' => 12,
            'home_delivery' => 'no'
            ),
            2 =>
            array (
            'customer_id' => 13,
            'home_delivery' => 'no'
            )
            );

            $secondArr = array (
            0 =>
            array (
            'customer_id' => 11,
            'test' => 'no',
            'is_active' => 'yes'
            ),
            1 =>
            array (
            'customer_id' => 22,
            'test' => 'no',
            'is_active' => 'yes'
            )
            );

            $secondKey = array_column($secondArr,'customer_id');

            foreach($firstArr as &$value){
            $idx2 = array_search($value['customer_id'], $secondKey);

            $value = array_merge($value, [
            'test' => false !== $idx2 ? $secondArr[$idx2]['test'] : '',
            'is_active' => false !== $idx2 ? $secondArr[$idx2]['is_active'] : '',
            ]);
            }
            print_r($firstArr);


            Output:



            Array
            (
            [0] => Array
            (
            [customer_id] => 11
            [home_delivery] => no
            [test] => no
            [is_active] => yes
            )

            [1] => Array
            (
            [customer_id] => 12
            [home_delivery] => no
            [test] =>
            [is_active] =>
            )

            [2] => Array
            (
            [customer_id] => 13
            [home_delivery] => no
            [test] =>
            [is_active] =>
            )

            )


            Sandbox



            There are 2 "tricks" I use here, the first, and more important one, is array_column this picks just one column from an array, but the thing is the keys in the resulting array will match the original array. Which we can take advantage of.



            The array we get from array column looks like this:



            array (
            0 => 11,
            1 => 22
            );


            Because the keys match the original array we can use array_search (with the ID) to look up that key, which we can then use in the original array. This gives us an "easier" way to search the second array by flattening it out.



            So for example when we look up $firstArr['customer_id'] = 11 in the above array we get the key 0 (which is not boolean false, see below). Then we can take that index and use it for the original array $secondArr and get the values from the other 2 columns.



            -Note- that array search returns boolean false when it cant find the item, because PHP treats 0 and false the same we have to do a strict type check !== instead of just !=. Otherwise PHP will confuse the 0 index with being false, which is not something we want.



            The second "trick" is use & in the foreach value, this is by reference which allows us to modify the array used in the loop, directly. This is optional as you could just as easily create a new array instead. But I thought I would show it as an option.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 12 at 19:51

























            answered Nov 12 at 19:35









            ArtisticPhoenix

            15.2k11223




            15.2k11223












            • thankz a lot..Its working..sorry for late reply..:)
              – S Sopno
              Nov 13 at 2:42


















            • thankz a lot..Its working..sorry for late reply..:)
              – S Sopno
              Nov 13 at 2:42
















            thankz a lot..Its working..sorry for late reply..:)
            – S Sopno
            Nov 13 at 2:42




            thankz a lot..Its working..sorry for late reply..:)
            – S Sopno
            Nov 13 at 2:42



            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python