1

I have 1 array that has the right values that I need but it is out of order. I then have another array with the same keys and it is in the right order but the values are not what I need.

Here is my first array with the correct values but is out of order:

Array
(
    [countTotal] => 7268
    [zip] => 
    [yearName] => 
    [countZipRadius] => 
    [Acura] => 1334
    [Cadillac] => 511
    [Ford] => 5423
)

Here is my second array with the right order but the wrong values:

Array
(
    [countZipRadius] => 0
    [zip] => 1
    [yearName] => 2
    [Acura] => 3
    [Cadillac] => 4
    [Ford] => 5
    [countTotal] => 6
)

I am trying to figure out a way to create a new array with the right values from array 1 but that is in the order of array 2.

I have been playing with it for awhile and cannot seem to get it.

Any help would be great.

Thanks!

1
  • What is the ordering you use? Is it a custom ordering? Commented Jan 25, 2013 at 3:05

5 Answers 5

3
$c = array();
foreach (array_keys($b) as $k) {
    $c[k] = $a[k];
}
Sign up to request clarification or add additional context in comments.

2 Comments

Exactly that. Instead of trying to order the existing array, simply create a new one. You have to make sure however that your arrays have the same keys. Otherwise information might get lost.
actually a solution with a fixed parameter name order would be better. but therefore one would need more insight into the code. like $keys = array( 'a', 'b', 'c'); foreach ($keys as $k) ; that would be independent of the array $b
1

You could use php's array_multisort function:

$original = array(
    'countTotal' => 7268,
    'zip' => '',
    'yearName' => '',
    'countZipRadius' => '',
    'Acura' => 1334,
    'Cadillac' => 511,
    'Ford' => 5423,
    );

$right = array(
    'countZipRadius' => 0,
    'zip' => 1,
    'yearName' => 2,
    'Acura' => 3,
    'Cadilac' => 4,
    'Ford' => 5,
    'countTotal' => 6
);

//make sure both arrays are in the same order
ksort($original);
ksort($right);

array_multisort($right, $original);

print_r($original);

When you give it two arrays with the same number of elements it sorts both arrays, based on the order of the first array - in this case the 0, 1, 2, 3, etc. values in $right

1 Comment

Thanks! array_multisort has to be my favorite function
0
  1. Create a New Array (Array C)
  2. Use a FOR loop to go through Array B
  3. For each value in Array B, get the value with the same key from Array A and set Array C append those values to Array C. This will put them in the correct order in C.

Comments

0

Using scones' method:

$original = array(
    'countTotal' => 7268,
    'zip' => '',
    'yearName' => '',
    'countZipRadius' => '',
    'Acura' => 1334,
    'Cadillac' => 511,
    'Ford' => 5423,
    );
$right = array(
    'countZipRadius' => 0,
    'zip' => 1,
    'yearName' => 2,
    'Acura' => 3,
    'Cadilac' => 4,
    'Ford' => 5,
    'countTotal' => 6
);
foreach ($right as $key => $value) {
    $new[$key] = $original[$key];
}
print_r($new);

Comments

0
$array = array('a' => 100, 'b' => '5');

$newArray = array_combine(array_keys($array), range(0, count($array) - 1));

var_dump($newArray);

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.