1

I have a very big array. Arrays within arrays. Below is a small portion of it;

[lta/] => Array(
    [2012-12-31/] => Array(
        [0] => 31_december_2012.pdf
        [1] => 31_december_2012.xls
        [2] => key_points.html
    )
    ...
)

What I need to do, is get the "key_points.html" value to always start at the top of it's array. Example;

[2012-12-31/] => Array
    (
        [2] => key_points.html            
        [0] => 31_december_2012.pdf
        [1] => 31_december_2012.xls  
    )
    ...
)

I cannot do a simple asort because I never know at which point "key_points.html" is going to appear in the array.

I tried to rename the values "key_points.html" with a view to sorting it and then un-renaming it after;

foreach ($the_array as $array_object => $array_item) {
    if ($array_item == "key_points.html") {
        $array_item = "0001_key_points.html";
    }
}

But that literally seemed to have no effect! it didn't even rename my value. I also tried the same thing with string replace;

$the_array = str_replace("key_points.html", "0001_key_points.html", $the_array);

Is there a function perhaps that allows you to specify a string, and move that to the top of each array each time if it finds it?

0

3 Answers 3

1

Use uasort to specify a custom comparator callback:

uasort($array, function($a, $b) {
  if($a == 'key_points.html') return -1; // Smaller than all
  if($b == 'key_points.html') return 1;  // Larger than all
  return ($a < $b) ? -1 : 1;             // Default sorting
});

Syntax is assuming an up to date PHP (5.3+) with support for anonymous functions.

0

Use custom function

  function customSort (&$array, $key) {
        $sorter=array();
        $ret=array();
        reset($array);
        foreach ($array as $ii => $val) {
            $sorter[$ii]=$va[$key];
        }
        asort($sorter);
        foreach ($sorter as $ii => $val) {
            $ret[$ii]=$array[$ii];
        }
        $array=$ret;
    }
    customSort($your_array,"2");
0

It might be a commonly considered solution to make iterated calls of uasort(), but this will not be optimized for performance (which may be a concern for a "big array"). It will work, but I probably wouldn't use it because it is running a relatively expensive sorting algorithm on each row.

Code: (Demo)

array_walk(
    $array['lta'],
    fn(&$v) => uasort(
        $v,
        fn($a, $b) => ($b === 'key_points.html')
            <=>
            ($a === 'key_points.html')
    )
);
var_export($array);

Instead, it will be less "work" to search for the key of the sticky element, then append the row to a new declaration of the sticky element. The union operator (+) will not allow the original sticky value to exist late in the row.

Code: (Demo)

foreach ($array['lta'] as &$row) {
    if (($k = array_search('key_points.html', $row)) !== false) {
        $row = [$k => 'key_points.html'] + $row;
    }
}
var_export($array);

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.