Showing posts with label PHP array Sorting. Show all posts
Showing posts with label PHP array Sorting. Show all posts

Thursday, 30 August 2018

PHP: Sort set based on another table


 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.

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);

Thursday, 25 September 2014

rsort in PHP

PHP rsort() function is used to sort an array by values in reverse order.
PHP rsort() function returns TRUE on success, or FALSE on failure.

Syntax:


rsort(array,sorttype)
array : Required. Specifies the array to use.
sorttype : Optional. Specifies how to sort the array values.
    Possible values:

  • SORT_REGULAR - Default. Treat values as they are (don't change types)
  • SORT_NUMERIC - Treat values numerically
  • SORT_STRING - Treat values as strings
  • SORT_LOCALE_STRING - Treat values as strings, based on local settings

Example:


<?php
$daysarr = array( "a"=>"Sunday", "b"=>"Monday", "c"=>"Tuesday" );
rsort($daysarr);
print_r($daysarr);
?>

Output will be:


Array ( 
        [0] => Tuesday 
        [1] => Sunday 
        [2] => Monday 
      )

arsort in PHP

arsort() function is utilized to sort a data array in converse request.
The definitive keys relating to values are kept as same.
PHP arsort() function returns True for success, or False on inadequacy.

Syntax:

 arsort(array,sorttype)
array : Required. Specifying an array
sorttype : Optional. Specifies how to sort the array values. Possible values:
  • SORT_REGULAR - Default. Treat values as they are (don't change types)
  • SORT_NUMERIC - Treat values numerically
  • SORT_STRING - Treat values as strings
  • SORT_LOCALE_STRING - Treat values as strings, based on local settings

Example:

<?php
$daysarr1 = array( "a"=>"Sunday","b"=>"Monday","c" =>"Tuesday" );
arsort($daysarr1);
print_r($daysarr1);
?>

Output will be:

Array ( 
         [c] => Tuesday
         [a] => Sunday 
         [b] => Monday 
        )