Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change index order in array

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!

like image 300
Eric Herlitz Avatar asked Apr 24 '10 09:04

Eric Herlitz


People also ask

How do you change the order of an array?

sort() - sort arrays in ascending order. rsort() - sort arrays in descending order. asort() - sort associative arrays in ascending order, according to the value. ksort() - sort associative arrays in ascending order, according to the key.

Can we change the starting index of an array?

Can we change the starting index of an array from 0 to 1 in any way? Explanation: No. You can not change the C Basic rules of Zero Starting Index of an Array.

How do you rearrange arrays in C++?

Approach used in the below program is as followsDeclare a variable as max_val and set it with arr[size - 1] + 1. Start loop FOR from i to 0 till i less than size. Inside the loop, check IF i % 2 = 0 then set arr[i] to arr[i] + (arr[max] % max_val) * max_val and decrement the max by 1.


1 Answers

$output = array();
foreach ( $right as $array ) {
    foreach ( $left as $field ) {
        $temp[$field] = $array[$field];
    }
    $output[] = $temp;
}
like image 85
Matteo Riva Avatar answered Sep 30 '22 06:09

Matteo Riva