PHP将多个数组的值转换为单个数组

时间:2023-01-14 12:12:03

I am fairly new to PHP and working my way through it. I've managed to solve most problems independently, but this one has had me scuppered for a while. Any help is much appreciated!

我对PHP非常陌生,并且一直在使用它。我已经设法独立地解决了大多数问题,但是这个问题让我困惑了一段时间。非常感谢您的帮助!

I have a series of arrays produced by a form, as follows:

我有一系列由表单生成的数组,如下所示:

description_array = description1, description2, description3
account_array = account1, account2, account 3

Where I am trying to get to is:

我想说的是:

item_array = 1 => description1, account1, 2 => description2, account2

It may be that I am struggling with the logic more than the actual code. I've tried all sorts of manipulations with foreach and while loops with little success.

可能是我在逻辑上比实际的代码更纠结。我试过各种各样的方法,但没有成功。

Any thoughts gratefully received.

任何想法感激地接受。

Thanks Aaron

谢谢亚伦

4 个解决方案

#1


3  

If the arrays are guaranteed to be balanced in size the you could do this.

如果数组的大小保证是平衡的,那么可以这样做。

$res = array();
for($x=0; $x<count($description_array); $x++){
       $res[] = array($description_array[$x], $account_array[$x]);
}

#2


1  

You could use the key from 1 array to get the value of another array:

可以使用1个数组中的键获取另一个数组的值:

$description_array = array('description1', 'description2', 'description3');
$account_array = array('account1', 'account2', 'account3');

$item_array = array();
foreach($description_array as $key=>$val){
    $item_array[] = array($val,$account_array[$key]);
}

echo '<pre>',print_r($item_array),'</pre>';

#3


0  

Derp. I totally misread. And in the time it took me to fix it other people answered, but oh well. Here it is :)

Derp。我完全误解了。在我修好它的那段时间里,其他人回答说,但是哦,好吧。在这里:)

$items = array();
foreach($description AS $k=>$v){
    if(isset($account[$k])){ 
        $items[$k] = array($v, $account[$k]); // assuming you don't really want a comma sep string, but another array.
    }
}

#4


0  

If they are both equally large, it's probably simplest to iterate over one of the arrays and fill in the new array.

如果它们都一样大,那么迭代一个数组并填充新数组可能是最简单的。

$item_array = array();
foreach ($description_array as $key => $description) {
    $item_array[$key] = array(
        'description' => $description,
        'account' => $account_array[$key]
    );
}

That's also easy to extend if you add further arrays.

如果您添加更多的数组,这也很容易扩展。

#1


3  

If the arrays are guaranteed to be balanced in size the you could do this.

如果数组的大小保证是平衡的,那么可以这样做。

$res = array();
for($x=0; $x<count($description_array); $x++){
       $res[] = array($description_array[$x], $account_array[$x]);
}

#2


1  

You could use the key from 1 array to get the value of another array:

可以使用1个数组中的键获取另一个数组的值:

$description_array = array('description1', 'description2', 'description3');
$account_array = array('account1', 'account2', 'account3');

$item_array = array();
foreach($description_array as $key=>$val){
    $item_array[] = array($val,$account_array[$key]);
}

echo '<pre>',print_r($item_array),'</pre>';

#3


0  

Derp. I totally misread. And in the time it took me to fix it other people answered, but oh well. Here it is :)

Derp。我完全误解了。在我修好它的那段时间里,其他人回答说,但是哦,好吧。在这里:)

$items = array();
foreach($description AS $k=>$v){
    if(isset($account[$k])){ 
        $items[$k] = array($v, $account[$k]); // assuming you don't really want a comma sep string, but another array.
    }
}

#4


0  

If they are both equally large, it's probably simplest to iterate over one of the arrays and fill in the new array.

如果它们都一样大,那么迭代一个数组并填充新数组可能是最简单的。

$item_array = array();
foreach ($description_array as $key => $description) {
    $item_array[$key] = array(
        'description' => $description,
        'account' => $account_array[$key]
    );
}

That's also easy to extend if you add further arrays.

如果您添加更多的数组,这也很容易扩展。