4

I have the following 'key' array: (Array 1)

Array
(
    [0] => first_name
    [1] => surname
    [2] => position
    [3] => age
)

This array should determine the keys and the order of keys/values which should exist in Array 2. So in a perfect world Array 2 would look like this:

Array
(
    [0] => Array
        (
            [first_name] => James
            [surname] => Johnstone
            [position] => Striker
            [age] => 42
        )

    [1] => Array
        (
            [first_name] => Al
            [surname] => MacLean
            [position] => Defender
            [age] => 22
        )

    ...

)

The problem I'm having is in the following example array. Sometimes:

a) the order of keys in Array 2 is not the same as Array 1
b) and some of the keys defined in Array 1 don't exist in Array 2 - like so:

Array
(
    [0] => Array
        (
            [position] => Defender
            [first_name] => James
            [surname] => McDonald

        )

    [1] => Array
        (
            [position] => Striker
            [first_name] => Ben
            [surname] => Lailey
        )

    ...
)

I'd like some assistance creating a PHP function which will take a 'badly formed' Array 2 such as the one directly above, and convert it to how it should be: Order as defined by Array 1, and add any missing keys to become 'correct' like so:

Array
(
    [0] => Array
        (
            [first_name] => James
            [surname] => McDonald
            [position] => Defender
            [age] => 

        )

    [1] => Array
        (

            [first_name] => Ben
            [surname] => Lailey
            [position] => Striker
            [age] =>
        )

    ...
)

The keys used in this example are arbitrary, there could be a new key added, removed or re-ordered in Array 1 and I need Array 2 to respect Array 1.

Thank you in advance.

2
  • loop through array 2, create new array using array 1 keys and array 2 data Commented Apr 2, 2012 at 22:46
  • why not ensure that the data is correct in the first place instead of making a converter? where are you getting the data? Commented Apr 2, 2012 at 23:25

3 Answers 3

1

A simple implementation:

// Pass in an array of keys, and an array of arrays
function cleanArray($keys, $arrays)
{
    // Create an empty container for our final output
    $final = array();

    // Loop through array of arrays
    foreach($arrays as $a)
    {
        // Create empty value for current item
        $next = array();

        // Loop through keys, in order
        foreach($keys as $k)
        {
            // Assign next key and value if we have it, or a blank string if we don't
            $next[$k] = isset($a[k]) ? $a[k] : '';
        }

        // Add current item to output
        $final[] = $next;
    }

    // Return final values, each item now having its keys ordered and normalized
    return $final;
}
Sign up to request clarification or add additional context in comments.

1 Comment

We are all beginners once, and some people learn better from concrete examples than abstract theory.
1

try using array-key-exists function http://php.net/manual/en/function.array-key-exists.php

if not exists add an empty element with the missing key. (you don't care for the order in an associative array)

Comments

0
$new=array();    
foreach ($array2 as $a2){
  foreach($array1 as $k1=>$a1){
  $new[][$k1]=$a2[$k1];
  } 
}

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.