处理数组以始终包含来自另一个数组的值的键(和顺序)

时间:2022-06-14 01:12:33

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

我有以下'key'数组:(数组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:

此数组应确定数组2中应存在的键和键/值的顺序。因此,在完美的世界中,数组2将如下所示:

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:

a)数组2中键的顺序与数组1 b)不同,数组1中定义的某些键在数组2中不存在 - 如下所示:

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:

我想要一些帮助创建一个PHP函数,它将采用一个'格式错误'的数组2,如上面的那个,并将其转换为应该如何:按照数组1定义的顺序,并添加任何缺少的键成为“正确”如此:

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.

此示例中使用的密钥是任意的,可能在阵列1中添加,删除或重新排序新密钥,我需要阵列2来尊重阵列1。

Thank you in advance.

先谢谢你。

3 个解决方案

#1


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;
}

#2


1  

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

尝试使用array-key-exists函数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)

如果不存在,则添加缺少键的空元素。 (你不关心关联数组中的顺序)

#3


0  

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

#1


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;
}

#2


1  

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

尝试使用array-key-exists函数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)

如果不存在,则添加缺少键的空元素。 (你不关心关联数组中的顺序)

#3


0  

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