0

I wonder how to do this. I have two arrays, I need to assign them into a variable. The example below shows the output. When I print out the output using print_r(), the value that I want to match is "id_car"..

1) variable used for this array is $data.

Array ( [0] => 26 )

2) variable used for this array is $dataKereta.

Array ( [0] => Array ( [0] => 25 [id_car] => 25 [1] => 23 [id_user] => 23 ) 

[1] => Array ( [0] => 26 [id_car] => 26 [1] => 23 [id_user] => 24)

[2] => Array ( [0] => 27 [id_car] => 27 [1] => 23 [id_user] => 25  ) )  

what i want is, find the same value in 1st array with 2nd array,if same assign them into variable..

I tried do like this

foreach ($dataKereta as $dk => $dk1) {
    if($data[$dk] != $dk1['id_car']) {
        $not_same[] = $dk1;
    }
    else {
        $same[] = $dk1;
    }
}

The code I made above has find the match value, if not match assign into the $not_same variable and otherwise.. this code is OK when have the value for 1st variable like this :

Array ( [0] => 25 [1] => 26 ) 

but if i remove the "[0] => 25" value,it start not detect the 26.. i'm sorry for bad english.

4

1 回答 1

0

如果您的数据有多个元素,请尝试此操作

即,$data = (0=>'26',1=>'27');

foreach ($data as $dataval) {
    foreach ($dataKereta as $key=>$dk) {
        if ($dataval == $dk['id_car']) {
            $same[] = $dk['id_car'];
        } 
    }
}

foreach ($dataKereta as $key=>$dk) {
    if (!in_array($dk['id_car'],$same)) {
        $not_same[] = $dk['id_car'];
    }
}

结果

相同:
数组([0] => 26 [1] => 27)
not_same:
数组([0] => 25)

于 2013-10-26T15:10:30.027 回答