Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i have visited similar question here but not getting what i want in php. Suppose i have 2 arrays. All checking should be case insensitive. say Field0 is same as field0 or fiEld1 is same as Field1.

array1 is array(
"0"=>"field0",
"1"=>"field1",
"2"=>"field2",
"3"=>"field3",
);

array2 is array(
"0"=>"field3",
"1"=>"field2",
"2"=>"field0",
"3"=>"field1",
"4"=>"field6",
"5"=>"field5",
);

Now I want array2 to be sorted based on array1 like the following:

array2 is array(
"0"=>"field0",
"1"=>"field1",
"2"=>"field2",
"3"=>"field3",
"4"=>"field6",
"5"=>"field5",
);

look here array1 has 4 elements so array2 should be sorted based exactly 4 elements of array1 and rest 2 element (index 4,5 of array2) should as it is in array2 before.

share|improve this question

3 Answers 3

up vote 2 down vote accepted

Try array_intersect combined with array_diff:

<?php
$array1 = array(
    "0"=>"field0",
    "1"=>"field1",
    "2"=>"field2",
    "3"=>"field3",
);

$array2 = array(
    "0"=>"field3",
    "1"=>"field2",
    "2"=>"field0",
    "3"=>"field1",
    "4"=>"field6",
    "5"=>"field5",
);


$array3 = array_merge(
    array_intersect($array1, $array2), 
    array_diff($array2, $array1)
);

var_dump($array3);

Update

For a case insensitive approach, use array_map to guarantee all entries in both arrays are lower-cased:

$array3 = array_map('strtolower', $array1);
$array4 = array_map('strtolower', $array2);

$array5 = array_merge(
    array_intersect($array3, $array4), 
    array_diff($array4, $array3)
);

var_dump($array5);
share|improve this answer
    
if i change the case of the value of array1 or array2 then sort will be changed. could you make it case insensitive? –  Samiul Feb 3 '14 at 16:48
    
Do you also want to preserve the original case of the arrays? –  watcher Feb 3 '14 at 16:58
    
yes the case of array2 should be intact –  Samiul Feb 3 '14 at 17:01
    
See my update for a case-insensitive approach –  watcher Feb 3 '14 at 17:37

Assuming that array 2 is always bigger than array 1:

$array2 = array_merge($array1, array_diff($array2, $array1));
share|improve this answer

It's really easy :

$array = $array1 + $array2
share|improve this answer
    
This will not work, as this only computes the union between the two arrays. See php.net/manual/en/language.operators.array.php –  watcher Feb 3 '14 at 16:32
    
I tested it, it works. –  jgroenen Feb 3 '14 at 16:32
    
Sorry, but :$array1 = array( "0"=>"field0", "1"=>"field1", "2"=>"field2", "3"=>"field3", ); $array2 = array( "0"=>"field3", "1"=>"field2", "2"=>"field0", "3"=>"field1", "4"=>"field6", "5"=>"field5", ); var_dump($array1 + $array2); [code] array(6) { [0] => string(6) "field0" [1] => string(6) "field1" [2] => string(6) "field2" [3] => string(6) "field3" [4] => string(6) "field6" [5] => string(6) "field5" } [/code] –  hannibal-dss Feb 3 '14 at 16:33
    
It may work in this one case, but is relying on a lot of assumptions about the input data. perhaps the onus should be on the OP to provide a bit more insight, but in general this will not work. See the first example in the man page I linked to. –  watcher Feb 3 '14 at 16:36
    
I agree, works only for this exact input. –  jgroenen Feb 3 '14 at 16:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.