0

How can I specify what order items should appear in an array?

I am given a 3 level array, somewhat like the following:

Array
(
[AK] => Array
    (
        [ACO] => Array
            (
                [FFS] => Array
                    (
                        [Medicaid] => -
                        [CHIP] => -
                    )
            )
        [ZCO] => Array
            (
                [FFS] => Array
                    (
                        [Medicaid] => -
                        [CHIP] => -
                    )
            )
        [Other] => Array
            (
                [FFS] => Array
                    (
                        [Medicaid] => -
                        [CHIP] => -
                    )
            )
    )
[AR] => Array
    (
        [Other] => Array
            (
                [Other] => Array
                    (
                        [Medicaid] => -
                        [CHIP] => -
                    )
            )
        [ACO] => Array
            (
                [FFS] => Array
                    (
                        [Medicaid] => -
                        [CHIP] => -
                    )

            )
        [ZCO] => Array
            (
                [FFS] => Array
                    (
                        [Medicaid] => -
                        [CHIP] => -
                    )
            )
    )
)

How can I make them sort list the first element [AK], by the second level named element(I need them both to be [ACO], [ZCO], [Other])?

5
  • You may need to write your own sort function : fr2.php.net/manual/fr/function.usort.php Commented Apr 7, 2014 at 15:21
  • Is it that Other is always last, but other items are arbitrary or do you always have the same three elements ACO, ZCO, Other? Commented Apr 7, 2014 at 15:23
  • I actually have alot more items than this, but I wanted to keep it simple for the question. Order needs to be exactly like the [AK] element, via [ACO], [ZCO], [Other] Commented Apr 7, 2014 at 15:25
  • You can either sort AR by the position of elements in AK, or you can devise a rule that would sort both as you want - ie sorting by length ascending and then normal ascending. Which would be best for your real problem? Also, do you know by what rule AK is sorted? Commented Apr 7, 2014 at 15:32
  • No rules. The incoming array is a state list from a database table, pre-sorted on state. ie... AK, AL, AR etc... like I said, I kept a bunch out of this to keep the question shorter :) Commented Apr 7, 2014 at 15:37

2 Answers 2

1

Dictionary is datastructure without order.

But php by default iterate items in order which elements been pasted.
So, you can simple recreate array in necessary order
Or write custom sort function using php usort method.

small example:

function compare($a, $b) 
{
  $order = array('ACO', 'ZCO', 'Other');
  $ai = array_search($a, $order);
  $bi = array_search($b, $order);
  return $ai<$bi ? -1 : $ai>$bi;
}
$a = array('ZCO'=>1, 'Other'=>1, 'ACO'=>1);
uksort($a, "compare");
print_r($a);
Sign up to request clarification or add additional context in comments.

5 Comments

not sure how I would do this... sorry, little bit of a rookie when coming to custom array sorting methods ;)
thanks for this. I can't really tell if it has worked or not though. Instead of displaying the ACO, ZCO, Other it is displaying the number of what I assume that particular items index is...
uksort You provide compare callback as second argument, which should return -1|0|1 - lower, equal, greater for any pair of keys..
I think we're close. Now, it's in alpha order though by that 2nd key :)
will task solved, if you go through all first key by foreach and apply this method?
0
// CREATE THE TEST DATA
$arr = Array
( 'AK' => Array
  ( 'ACO'   => Array( 'FFS' => Array( 'Medicaid' => '-', 'CHIP' => '-' ) )
  , 'ZCO'   => Array( 'FFS' => Array( 'Medicaid' => '-', 'CHIP' => '-' ) )
  , 'Other' => Array( 'FFS' => Array( 'Medicaid' => '-', 'CHIP' => '-' ) )
  )
, 'AR' => Array
  ( 'Other' => Array( 'FFS' => Array( 'Medicaid' => '-', 'CHIP' => '-' ) )
  , 'ACO'   => Array( 'FFS' => Array( 'Medicaid' => '-', 'CHIP' => '-' ) )
  , 'ZCO'   => Array( 'FFS' => Array( 'Medicaid' => '-', 'CHIP' => '-' ) )
  )
)
;

// ACTIVATE THIS TO ANSWER THE QUESTION: DOES IT LOOK RIGHT?
// print_r($arr);

// GET THE ORDER FOR THE [AK] ARRAY
$pat = array_keys($arr['AK']);

// ITERATE OVER THE ARRAY TO PRODUCE THE ORDERED OUTPUT
$out = array();
foreach($arr as $key => $inner)
{
    // SKIP THE SIGNAL ARRAY
    if ($key == 'AK')
    {
        $out[$key] = $inner;
        continue;
    }
    // REORDER THE OTHER ARRAYS
    else
    {
        $temp = array();
        foreach ($pat as $inner_key)
        {
            $temp[$inner_key] = $inner[$inner_key];
        }
        $out[$key] = $temp;
    }
}
// SHOW THE WORK PRODUCT
print_r($out);

Comments

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.