我怎样才能通过两个数组循环一次? [英] How can I loop through two arrays at once?

查看:127
本文介绍了我怎样才能通过两个数组循环一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想两个数组安排在一起,结果不断出来不正确。我会告诉你我的code,我得到的结果,结果我要找的。

我想我只是做错了,但不知道怎么回事做到这一点。

我的code:

  $数据1 = [
    答:1,
    'a2',
    一:3',
    一:4,
    A:5'
];$数据2 = [
    'B:1',
    'B:2',
    'B:3',
    'B:4',
    'B:5'
];
的foreach($ DATA1为ITEM1 $)
{
    的foreach($ DATA2作为ITEM2 $)
    {
        回声$ item1的。 '< BR />';
        回声$ ITEM2。 '< BR />';
        呼应'< BR />< BR />';
    }
}

结果:(简称为节省空间)

  A:1
A:1答:1
乙:2答:1
B:3-答:1
B:4-答:1
B:5-

我在寻找的结果是:

  A:1
A:1a2
乙:2答:3
B:3-答:4
B:4-答:5
B:5-


解决方案

您code的问题:

好这个问题当然是你的嵌套foreach循环。因为你的 $数据1 数组中的每个元素您完成整个 $数据2 数组循环(因此总共有 $数据1 * $数据2 迭代)。

解决方案:

要通过两个数组一下子解决这个问题,你必须循环。你可以做到这一点 array_map() 例如,如:

array_map() 方法(PHP> = 5.3):

  array_map(函数($ V1,V2 $){
    回声$ V1。 < BR>中;
    回声$ V2。 < BR>< BR>中;
},$数据1,$数据2 / *,如果需要手动添加更多阵列* /);

<分> 注意:如果元素的量参差不齐你不会根任何错误,它只是打印 NULL (均值你不会看到任何东西)

编辑:

也有许多其他的解决办法做到这一点。只有少数的:

使用一个 MultipleIterator 和<一个HREF =htt​​p://php.net/manual/en/multipleiterator.attachiterator.php>附加 2倍的 ArrayIterator ,如:

MultipleIterator() 方法(PHP > = 5.3):

  $吧=新MultipleIterator();
$ IT-&GT; attachIterator(新ArrayIterator($ DATA1));
$ IT-&GT; attachIterator(新ArrayIterator($数据2));
如果需要的话//添加更多阵列的foreach($它为$ a){
    回声$ A [0]。 &LT; BR&gt;中;
    回声$ A [1]。 &LT; BR&GT;&LT; BR&gt;中;
}

<分> 注意:如果元素的量参差不齐它只是打印所有值,其中两个数组仍然有值

使用for循环使用计数器变量,它可以作为钥匙两个阵列,例如使用。

循环 法( PHP:> = 4.3):

  $ keysOne = array_keys($数据1);
$ keysTwo = array_keys($数据2);$分=分钟(计数($数据1),计数($数据2));为($ I = 0; $ I&LT; $分钟; $ I ++){
    回声$ DATA1 [$ keysOne [$ i]]。 &LT; BR&gt;中;
    回声$ DATA2 [$ keysTwo [$ i]]。 &LT; BR&GT;&LT; BR&gt;中;
}

<分> 注意:使用 array_keys( ) 就是这样,如果阵列不具有相同的密钥或具有关联性,这也有效。 MIN() 通过仅用于循环尽可能多的元素,因为每个阵列有

或者,如果阵列只有唯一值,可以 array_combine() 两个阵列,使 $数据1 可以作为键来访问和 $数据2 作为值,如:

array_combine() 方法(PHP:> = 5.0):

 的foreach(array_combine($数据1,$数据2)为$ D1 =&GT; $ D2){
    回声$ D1。 &LT; BR&gt;中;
    回声$ D2。 &LT; BR&GT;&LT; BR&gt;中;
}

<分> 注意:的数组必须有内容相同数量的,否则 array_combine()将抛出一个错误

如果您想打印在同一时间超过2个数组或数组的只是一个未知量,你可以结合 array_map()方法以<一个href=\"http://php.net/manual/en/function.call-user-func-array.php\"><$c$c>call_user_func_array()调用,例如

<$c$c>call_user_func_array()方法(PHP:> = 5.6):

  $ FUNC =功能(... $号){
    的foreach($数字作为$ V)
        回声$ V。 &LT; BR&gt;中;
    回声&LT; BR&gt;中;
};
call_user_func_array(array_map,[$ FUNC,$数据1,$数据2]);

<分> 注意:因为这有点儿使用 array_map()方法,其行为和元素的大小不均相同。此外,由于这种方法仅适用于PHP> = 5.6你可以删除的参数和使用的 $号 /en/function.func-get-args.php> func_get_args() 在foreach循环中,然后它也适用于PHP> = 5.3

I am trying to arrange two arrays together and the results keep coming out incorrect. I'll show you my code, the results I'm getting and the results I'm looking for.

I guess I'm just doing it wrong but not sure how else to do it.

My code:

$data1 = [
    'a: 1',
    'a: 2',
    'a: 3',
    'a: 4',
    'a: 5'
];

$data2 = [
    'b: 1',
    'b: 2',
    'b: 3',
    'b: 4',
    'b: 5'
];


foreach($data1 as $item1)
{
    foreach($data2 as $item2)
    {
        echo $item1 . '<br/>';
        echo $item2 . '<br/>';
        echo '<br/><br/>';
    }
}

The Results: (shortened to save space)

a: 1
b: 1

a: 1
b: 2

a: 1
b: 3

a: 1
b: 4

a: 1
b: 5

The results I'm looking for is the following:

a: 1
b: 1

a: 2
b: 2

a: 3
b: 3

a: 4
b: 4

a: 5
b: 5

解决方案

Problem of your code:

Well the problem is of course your nested foreach loop. Because for each element of your $data1 array you loop through the entire $data2 array (So in total there are $data1 * $data2 iterations).

Solutions:

To solve this you have to loop through both arrays at once. You can do this with array_map() for example, e.g.

array_map() method (PHP >=5.3):

array_map(function($v1, $v2){
    echo $v1 . "<br>";
    echo $v2 . "<br><br>";
}, $data1, $data2 /*, Add more arrays if needed manually*/);

Note: If the amount of elements are uneven you won't gen any errors, it will just print NULL (Means you won't see anything)

EDIT:

There are also many other solutions to do this. Only a few of them:

Use a MultipleIterator and attach 2x ArrayIterator, e.g.

MultipleIterator() method (PHP >=5.3):

$it = new MultipleIterator();
$it->attachIterator(new ArrayIterator($data1));
$it->attachIterator(new ArrayIterator($data2));
//Add more arrays if needed 

foreach($it as $a) {
    echo $a[0] . "<br>";
    echo $a[1] . "<br><br>";
}

Note: If the amount of elements are uneven it will just print all values where both arrays still have values

Use a for loop with a counter variable, which you can use as key for both arrays, e.g.

for loop method (PHP: >=4.3):

$keysOne = array_keys($data1);
$keysTwo = array_keys($data2);

$min = min(count($data1), count($data2));

for($i = 0; $i < $min; $i++) {
    echo $data1[$keysOne[$i]] . "<br>";
    echo $data2[$keysTwo[$i]] . "<br><br>";
}

Note: Using array_keys() is just so that this also works if the arrays don't have the same keys or are associative. min() is used to only loop through as many elements as every array has

Or if the arrays only have unique values, you can array_combine() both arrays, so that $data1 can be accessed as key and $data2 as value, e.g.

array_combine() method (PHP: >=5.0):

foreach(array_combine($data1, $data2) as $d1 => $d2) {
    echo $d1 . "<br>";
    echo $d2 . "<br><br>";
}

Note: The arrays must have the same amount of elements, otherwise array_combine() will throw an error

If you want to print more than 2 arrays at the same time or just an unknown amount of arrays, you can combine the array_map() method with a call_user_func_array() call, e.g.

call_user_func_array() method (PHP: >=5.6):

$func = function(...$numbers){
    foreach($numbers as $v)
        echo $v . "<br>";
    echo "<br>";
};
call_user_func_array("array_map", [$func, $data1, $data2]);

Note: Since this kinda uses the array_map() method, it behaves the same with uneven amount of elements. Also since this method only works for PHP >=5.6 you can just remove the arguments and change out $numbers with func_get_args() in the foreach loop and then it also works for PHP >=5.3

这篇关于我怎样才能通过两个数组循环一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆