php将2个数组合并到一个关联数组中

时间:2022-06-24 12:19:01

Using PHP I need to merge 2 arrays (of equal length into one associative array) here is an excerpt from my current data set:

使用PHP,我需要合并两个数组(长度相等)到一个关联数组中。

[1] => Array
    (
        [0] => C28
        [1] => C29
    )

[2] => Array
    (
        [0] => 1AB010050093
        [1] => 1AB008140029
    )

both elements [1] and [2] are actually a lot longer than just 2 sub-elements (like I said, this is an excerpt).

[1]和[2]两个元素实际上都比两个子元素要长得多(就像我说的,这是摘录)。

The deal is that "C28" in the first array corresponds to "1AB010050093" in the second array, and so on... The result I need is to create a new associative array that looks like this:

协议是第一个数组中的“C28”对应于第二个数组中的“1AB010050093”,以此类推。我需要的结果是创建一个新的关联数组,如下所示:

[1] => Array    
    (
        ['ref']  => C28
        ['part'] => 1AB010050093
    )
[2] => Array
    (
        ['ref'] => C29
        ['part'] => 1AB008140029
    )

and so on...

等等……

3 个解决方案

#1


4  

If you are willing to compromise with an array structure like this:

如果您愿意与这样的数组结构妥协:

array(
    'C28' => '1AB010050093',
    'C29' => '1AB008140029'
);

Then you can use the array_combine() (Codepad Demo):

然后可以使用array_combine() (Codepad Demo):

array_combine($refNumbers, $partIds);

Otherwise, you'll need to use a foreach (Codepad Demo):

否则,您将需要使用foreach (Codepad演示):

$combined = array();

foreach($refNumbers as $index => $refNumber) {
    if(!array_key_exists($index, $partIds)) {
        throw OutOfBoundsException();
    }

    $combined[] = array(
        'ref'  => $refNumber,
        'part' => $partIds[$index]
    );
}

#2


0  

If you're using PHP 5.5+, there is a new method called array_column(), which will get all of the values in a particular column. That could potentially be used, although I think just a simple foreach loop will probably still be your best bet.

如果您使用的是PHP 5.5+,则有一个名为array_column()的新方法,它将获取特定列中的所有值。这可能会被使用,尽管我认为一个简单的foreach循环可能仍然是您最好的选择。

#3


0  

How about:

如何:

$arr1 = array(
    0 => 'C28',
    1 => 'C29',
);

$arr2 = array(
    0 => '1AB010050093',
    1 => '1AB008140029',
);
$result = array();
for ($i=0; $i<count($arr1); $i++) {
    $result[] = array('ref' => $arr1[$i], 'part' => $arr2[$i]);
}
print_r($result);

ouptut:

ouptut:

[1] => Array
    (
        [0] => C28
        [1] => C29
    )

[2] => Array
    (
        [0] => 1AB010050093
        [1] => 1AB008140029
    )

#1


4  

If you are willing to compromise with an array structure like this:

如果您愿意与这样的数组结构妥协:

array(
    'C28' => '1AB010050093',
    'C29' => '1AB008140029'
);

Then you can use the array_combine() (Codepad Demo):

然后可以使用array_combine() (Codepad Demo):

array_combine($refNumbers, $partIds);

Otherwise, you'll need to use a foreach (Codepad Demo):

否则,您将需要使用foreach (Codepad演示):

$combined = array();

foreach($refNumbers as $index => $refNumber) {
    if(!array_key_exists($index, $partIds)) {
        throw OutOfBoundsException();
    }

    $combined[] = array(
        'ref'  => $refNumber,
        'part' => $partIds[$index]
    );
}

#2


0  

If you're using PHP 5.5+, there is a new method called array_column(), which will get all of the values in a particular column. That could potentially be used, although I think just a simple foreach loop will probably still be your best bet.

如果您使用的是PHP 5.5+,则有一个名为array_column()的新方法,它将获取特定列中的所有值。这可能会被使用,尽管我认为一个简单的foreach循环可能仍然是您最好的选择。

#3


0  

How about:

如何:

$arr1 = array(
    0 => 'C28',
    1 => 'C29',
);

$arr2 = array(
    0 => '1AB010050093',
    1 => '1AB008140029',
);
$result = array();
for ($i=0; $i<count($arr1); $i++) {
    $result[] = array('ref' => $arr1[$i], 'part' => $arr2[$i]);
}
print_r($result);

ouptut:

ouptut:

[1] => Array
    (
        [0] => C28
        [1] => C29
    )

[2] => Array
    (
        [0] => 1AB010050093
        [1] => 1AB008140029
    )