简体   繁体   English

循环遍历数组在PHP中具有2个或更多属性

[英]Loop Through An Array In PHP with 2 or more properties

I'm trying to create an array with two properties and loop through it. 我正在尝试创建一个包含两个属性的数组并循环遍历它。 But I don't know how to do it. 但我不知道该怎么做。

In this example a have one property: 在此示例中,a具有一个属性:

$foodArray = ["apple", "banana"];

foreach ($foodArray as $food)  {
    echo $food ."<br />";
}

Now I want to add in the array - green to apple and yellow to banana and loop the same way. 现在我想添加数组 - 绿色到苹果,黄色到香蕉,并以相同的方式循环。 How to do it in the best way? 如何以最好的方式做到这一点?

You can either add multiple properties by adding multiple elements within a sub-array 您可以通过在子数组中添加多个元素来添加多个属性

$foodArray = [['name' => 'Apple', 'color' => 'Yellow'], 
              ['name' => 'Banana', 'color' => 'yellow']];

foreach($foodArray as $fruit) {
    echo $fruit['name']." - ".$fruit['color']." <br />";
}

Or if you just need these two properties, you can use the key as the name, and the value as the color. 或者,如果您只需要这两个属性,则可以使用键作为名称,将值作为颜色。

$foodArray = ['Apple' => 'green', 'Banana' => 'yellow'];
foreach($foodArray as $fruit => $color) {
    echo $fruit." - ".$color ." <br />";
}

For looping over multiple properties, you need to use multi-dimensional arrays. 要循环多个属性,您需要使用多维数组。

Basic concept is that an array should contain arrays. 基本概念是数组应包含数组。

Now these arrays can have multiple properties. 现在这些数组可以有多个属性。

You need to take key value pairs. 您需要获取键值对。

So, your array would be: 所以,你的数组将是:

$foodArray = ['green' => 'apple', 'yellow' => 'banana'];
foreach ($foodArray as $foodColor => $food)  {
 echo $foodColor . ' = ' $food ."<br />";
}

Or else, you can define multi-dimensional array and loop over it: 或者,您可以定义多维数组并在其上循环:

$foodArray = [];
$foodArray['apple']['name'] = ['apple'];
$foodArray['apple']['color'] = ['green'];

$foodArray['banana']['name'] = ['banana'];
$foodArray['banana']['color'] = ['yellow'];

And loop over it. 并循环它。

foreach ($foodArray as $food)  {
 echo $food['color'] . ' = ' $food['name'] ."<br />";
}

You can multiple properties for each food item as it is a multi-dimensional array. 每个食品都可以有多个属性,因为它是一个多维数组。

Using this method you can add other properties 使用此方法可以添加其他属性

$foodArray = [
   [
     'name' => 'apple',
     'color' => 'green',
   ],
   [
     'name' => 'banana',
     'color' => 'yellow',
   ]
];

//loop
foreach($foodArray as $food) {
    echo $food['name'] . ' have color ' . $food['color'];
}

If you want to store multiple properties using an array, next approach using multidimensional array may help: 如果要使用数组存储多个属性,则使用多维数组的下一个方法可能有所帮助:

<?php
$foodArray = array(
    "apple" => array(
        "color" => "green",
        "size" => "small"
    ), 
    "banana" => array(
        "color" => "yellow",
        "size" => "medium"
    )
);

foreach ($foodArray as $fruit => $properties)  {
    echo $fruit."<br />";
    foreach ($properties as $key => $value)  {
        echo $key.": ".$value."<br />";
    }   
    echo "<br />";
}
?>

Output: 输出:

apple
color: green
size: small

banana
color: yellow
size: medium

you can do something like this: 你可以这样做:

//keyed array
$foodArray = ['apple' => 'green', 'banana' => 'yellow'];
// loop through array as key/value prop
foreach($foodArray as $fruit => $color) {
    echo $fruit." - ".$color ." <br />";
}

Your one property array 你的一个属性数组

 $foodArray = ["apple", "banana"]; 

new array to add keys in first array 用于在第一个数组中添加键的新数组

$key_array = ["green", "yellow"];

now to create array with green->apple, yellow->banana 现在用绿色 - >苹果,黄色 - >香蕉创建数组

$output = array_combine($key_array, $foodArray);

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

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