views:

308

answers:

6

Hi

Been kind of stuck on this one for a while now, so any help would be appreciated. I have one array (left) that contains a list of elements, the goal is to sort another arrays (right) keys with the values from the left array.

The left array

Array
(
    [0] => ID
    [1] => FirstName
    [2] => LastName
    [3] => Address
)

The right array

Array
(
    [0] => Array
    (
        [FirstName] => Pim
        [Address] => Finland
        [LastName] => Svensson
        [ID] => 3
    )
    [1] => Array
    (
        [FirstName] => Emil
        [Address] => Sweden
        [LastName] => Malm
        [ID] => 5
    )
)

What I'm trying to accomplish would be similar to this

Array
(
    [0] => Array
    (
        [ID] => 3
        [FirstName] => Pim
        [LastName] => Svensson
        [Address] => Finland
    )

Anyone? :) Oh, I'm running php 5.3, if it helps!

+1  A: 
$output = array();
foreach ( $right as $array ) {
    foreach ( $left as $field ) {
        $temp[$field] = $array[$field];
    }
    $output[] = $temp;
}
kemp
+1 for being simple, clear, effective.
chris
Wonderful, it did the trick!
Trikks
+2  A: 

You must explode the array Store the array in a variable like this

$array = Array
(
    [0] => Array
    (
        [ID] => 3
        [FirstName] => Pim
        [LastName] => Svensson
        [Address] => Finland
    );

and then explode the array after exploding the array you will get the parameters of the array seperated then you can use implode function the arrange them in anyorder as you wish

udaya
did you got the result
udaya
+1  A: 

I'd take a step back and look at what you really need to do. The array is associative, so you can access the correct element instantly, right? So you don't really need it to be in order, unless you print output with foreach.

I'd suggest one of the following solutions:

  1. If you need the "right" array to be in key-order, then look at the database query / similar and select the columns in the order you need them.
  2. Foreach person you want to print, look up the order in the "left" array, then print the corresponding value of that key in the "right" array.
PatrikAkerstrand
I don't select anything from a db, I get the array from an xml which is loaded with simplexml!
Trikks
A: 

Well, your question it's uncommon, usually the associative arrays are used to resolve any problems about "position". Anyway, there are many way to do what you are looking for what are you looking for.

You can use list() but it's position based:

foreach($oldArray as $o)
{
    list($firstName,$lastName,$address,$id)=$oldArray;
    $newArray[]=array($id,$firstName,$lastName,$address);
}

But the best way to solve your problem it's fill the array directly in the right order instead of re-sort after :)

Cesar
Thanks!I second that, but I get the information in xml from another service, this way I can't always guarantee the order of the data. Thus this odd quest! :)
Trikks
+2  A: 

You can use uksort() which lets you sort array keys by a user defined function. E.g.:

function sort_by_array($array) {
    // create a sorting function based on the order of the elements in the array
    $sorter = function($a, $b) use ($array) {
        // if key is not found in array that specifies the order, it is always smaller
        if(!isset($array[$a])) return -1; 
        if($array[$a] > $array[$b]) {
            return 1;
        }
        return ($array[$a] == $array[$b]) ? 0 : -1;
    };
    return $sorter;
}
// $array contains the records to sort
// $sort_array is the array that specifies the order
foreach($array as &$record) {
    uksort($record, sort_by_array(array_flip($sort_array)));
}

I make use of the possibility in 5.3 to define functions dynamically and I use array_flip() to transform:

Array
(
    [0] => ID
    [1] => FirstName
    [2] => LastName
    [3] => Address
)

to

Array
(
    [ID] => 0
    [FirstName] => 1
    [LastName] => 2
    [Address] => 3
)

This makes it easier to compare the keys later.

Felix Kling
1+ for being flexible, but I think it's overly complex for this particular case.
chris
@chris: Yes, in this case, depending what the OP really wants to do with the array, it might be to complex. But I wondered why no one mentioned this method and I wanted to show that it is possible.
Felix Kling
A: 

still trying to find something quicker.... though I like kemp's answer...