简体   繁体   English

PHP使用for循环遍历数组

[英]PHP Looping through arrays with for loop

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. 我越来越多地使用for循环遍历数组,这是我目前尚无法解决的问题。

I have 3 loops and in the loops are common color names. 我有3个循环,循环中是常用的颜色名称。 Using the first for loop I am looping through all the 3 loops and finding the common color name, this works fine. 使用第一个for循环,我遍历了所有3个循环,并找到了通用的颜色名称,这很好用。

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. 我知道我可以使用一个实现如下所示技巧的foreach循环,但是我正在尝试查看如何使用for循环来完成此操作。

How can I do this? 我怎样才能做到这一点? (without using array_intersect) (不使用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: 使用foreach循环是可行的,但想学习如何使用for循环:

$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 只需使用array_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 )

This does use some other PHP functions, but here is one other way to get the keys without using foreach. 这确实使用了其他一些PHP函数,但这是不使用foreach即可获取密钥的另一种方法。

<?php

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

    for($i = 0; $i < $count; $i++) {

        if (current($value) > 2) {
            $commonValues[] = key($value);
        }
        next($value);
    }

    print_r($commonValues);

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM