dhanashri dhanashri - 6 months ago 8
PHP Question

How to merge two arrays in php

i am stuck at one point I have two arrays one contains read data and anther contains write as shown below

read array

Array
(
[0] => master/city
[1] => master/national_holiday
[2] => master/operator_comments
[3] => master/sensors
)


write array

Array
(
[0] => master/city
[1] => master/national_holiday
)


I want to combine these two arrays. And i want as below

Array
(
['master/city'] => 'read/write'
['master/national_holiday'] => 'read/write'
['master/operator_comments'] => 'read'
['master/sensors'] => 'read'
)


is it possible like this?

Ali Ali
Answer

You can do this:

Here is DEMO

$final = [];
array_walk($read,function($v,$k) use (&$final,&$write){
    if(array_search($v,$write) > -1){
        $final[$v] = 'read/write';
    }
    else{
        $final[$v] = 'read';
    }
});
print_r($final);

Output

Array
(
    [master/city] => read/write
    [master/national_holiday] => read/write
    [master/operator_comments] => read
    [master/sensors] => read
)
Comments