0

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.

1
  • This isn't a great minimal reproducible example because the first four elements to be sorted as the first four elements. By happenstance, jgroenen's seems correct, but will not be in other circumstances. Commented Sep 7, 2022 at 7:49

3 Answers 3

2

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);
2
  • if i change the case of the value of array1 or array2 then sort will be changed. could you make it case insensitive? Commented Feb 3, 2014 at 16:48
  • Do you also want to preserve the original case of the arrays? Commented Feb 3, 2014 at 16:58
1

Assuming that array 2 is always bigger than array 1:

$array2 = array_merge($array1, array_diff($array2, $array1));
-2

It's really easy :

$array = $array1 + $array2
5
  • This will not work, as this only computes the union between the two arrays. See php.net/manual/en/language.operators.array.php Commented Feb 3, 2014 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] Commented Feb 3, 2014 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. Commented Feb 3, 2014 at 16:36
  • You should probably also explain possible problems with using this method if the input happens to not work out so nicely. Commented Feb 3, 2014 at 16:55
  • The major problem is that the union operator works on array keys and not array values, and both arrays are (basically) numerically indexed Commented Feb 3, 2014 at 17:00

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.