PHP循环通过数组与循环 [英] PHP Looping through arrays with for loop

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

问题描述

我越来越做法通过阵列使用循环循环,并有我想不通迄今为止的问题。

我有3个循环,并在循环是常见的颜色名称。使用第一个for循环,我通过所有3个回路循环和寻找共同的颜色名称,这工作正常。

第二部分是在那里我难倒就如何做到这一点,如何在共同的价值观数组赋值到另一个数组只显示这些共同的价值观。

我知道我可以使用foreach循环,做如下图所示的伎俩,但我想看看如何使用一个for循环做到这一点吧。

我怎样才能做到这一点? (不使用array_intersect)

code:(此遍历所有阵列和给我的共同价值观)

  $数组1 = ['红','蓝','绿色'];
$数组2 = ['黑','蓝','紫','红'];
$ ARRAY3 = ['红','蓝','橙','棕'];$值= [];$ array_total = array_merge($数组1,$数组2,$ ARRAY3);$ array_length =计数($ array_total);为($ I = 0; $ I< $ array_length; $ I ++){
    如果(!使用isset($价值[$ array_total [$ i])){
        $值[$ array_total [$ i] = 0;
    }    $ a = $值[$ array_total [$ i]] ++;
}
//的print_r($值); - 阵列([红色] => 3 [蓝] => 3 [绿色] => 1 [黑色] => 1 [紫] => 1 [橙] => 1 [棕色] =大于1)

使用foreach循环工作,但想学习如何做一个for循环:

  $ commonValues​​ = [];的foreach($值$值=> $计数){
    如果($计数→2){
        $ commonValues​​ [] = $价值;
    }
}
的print_r($ commonValues​​); - 阵列([0] =>红[1] =>蓝)


解决方案

这应该为你工作:

只需使用 array_keys() 获得与您可以用数字键访问关联数组的数组

 < PHP    $值= [红=> 3,蓝=> 3,绿色= GT; 1,黑=> 1,紫=> 1,橙色=> 1,棕色=> 1];
    $数= COUNT($值);
    $键= array_keys($值);    为($ I = 0; $ I< $计数; $ I ++){
        如果($值[$按键[$ i]于]→2){
            $ commonValues​​ [] = $键[$ i];
        }
    }    的print_r($ commonValues​​);?>

输出:

 阵列([0] =>红[1] =>蓝)

I am getting more practice with using for loops to loop through arrays, and have a question that I can't figure out thus far.

I have 3 loops and in the loops are common color names. Using the first for loop I am looping through all the 3 loops and finding the common color name, this works fine.

The second part is where I am stumped on how to do this, how to assign the common values array into another array to just show those common values.

I know I can use a foreach loop that does the trick as shown below, but I am trying to see how to do this with a for loop instead.

How can I do this? (without using array_intersect)

Code: (this loops through all arrays and gives me the common values)

$array1 = ['red', 'blue', 'green'];
$array2 = ['black', 'blue', 'purple', 'red'];
$array3 = ['red', 'blue', 'orange', 'brown'];

$value = [];

$array_total = array_merge($array1, $array2, $array3);

$array_length = count($array_total);

for ($i = 0; $i < $array_length; $i++) {
    if (!isset($value[$array_total[$i]])) {
        $value[$array_total[$i]] = 0;
    }

    $a = $value[$array_total[$i]]++;
}
//print_r($value); -- Array ( [red] => 3 [blue] => 3 [green] => 1 [black] => 1 [purple] => 1 [orange] => 1 [brown] => 1 )

Using foreach loop works but want to learn how to do it with a for loop:

$commonValues = [];

foreach ($value as $values => $count) {
    if ($count > 2) {
        $commonValues[] = $values;
    }
}
print_r($commonValues); -- Array ( [0] => red [1] => blue )

解决方案

This should work for you:

Just use array_keys() to get an array with which you can access your associative array with numerical keys

<?php

    $value = ["red" => 3, "blue" => 3, "green" => 1, "black" => 1, "purple" => 1, "orange" => 1, "brown" => 1];
    $count = count($value);
    $keys = array_keys($value);

    for($i = 0; $i < $count; $i++) {
        if ($value[$keys[$i]] > 2) {
            $commonValues[] = $keys[$i];
        }
    }

    print_r($commonValues);

?>

output:

Array ( [0] => red [1] => blue )

这篇关于PHP循环通过数组与循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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