PHP:访问嵌套数组元素的有效方法?
我正在整理我在工作中遇到的一些旧代码,包括各种数组,我想将它们组合成嵌套数组,并想知道一种简单的方法来循环嵌套数组的内容(可能有一种比嵌套数组更好的方法来存储数据,如果是的话,欢迎建议) 以下代码只是原始代码结构的一个示例,其行为方式与一般化输出相同 $someArray元素过去是单独定义的,管理起来似乎非常耗时,因此我想更改它:PHP:访问嵌套数组元素的有效方法?,php,arrays,nested-attributes,multidimensional-array,Php,Arrays,Nested Attributes,Multidimensional Array,我正在整理我在工作中遇到的一些旧代码,包括各种数组,我想将它们组合成嵌套数组,并想知道一种简单的方法来循环嵌套数组的内容(可能有一种比嵌套数组更好的方法来存储数据,如果是的话,欢迎建议) 以下代码只是原始代码结构的一个示例,其行为方式与一般化输出相同 $someArray元素过去是单独定义的,管理起来似乎非常耗时,因此我想更改它: $fruit['fruit']['apple']['size'] = 'small'; $fruit['fruit']['apple']['colour'] = 'r
$fruit['fruit']['apple']['size'] = 'small';
$fruit['fruit']['apple']['colour'] = 'red';
$fruit['fruit']['apple']['taste'] = 'bitter';
$fruit['fruit']['pear']['size'] = 'big';
$fruit['fruit']['pear']['colour'] = 'green';
$fruit['fruit']['pear']['taste'] = 'sweet';
下面是我正在构建的嵌套数组的示例:
class someEntity
{
public function someFunction()
{
$someArray = array
(
'apple' => array(
'size' => 'small',
'colour' => 'red',
'taste' => 'bitter'
),
'pear' => array(
'size' => 'big',
'colour' => 'green',
'taste' => 'sweet'
)
);
return($someArray);
}
public function anotherFunction()
{
# some other stuff
}
}
通过foreach循环调用:
$someArray= someEntity::someFunction();
var_dump($someArray);
foreach($someArray as $key)
{
foreach($key as $key => $value)
{
print($key.': '.$value.'<br>');
}
}
array (size=2)
'apple' =>
array (size=3)
'size' => string 'small' (length=5)
'colour' => string 'red' (length=3)
'taste' => string 'bitter' (length=6)
'pear' =>
array (size=3)
'size' => string 'big' (length=3)
'colour' => string 'green' (length=5)
'taste' => string 'sweet' (length=5)
$someArray=someEntity::someFunction();
变量转储($someArray);
foreach($someArray作为$key)
{
foreach($key as$key=>$value)
{
打印($key.':'.$value.
);
}
}
数组(大小=2)
“苹果”=>
数组(大小=3)
'size'=>字符串'small'(长度=5)
“颜色”=>字符串“红色”(长度=3)
“味道”=>字符串“苦”(长度=6)
‘梨’=>
数组(大小=3)
'size'=>字符串'big'(长度=3)
'color'=>字符串'green'(长度=5)
'taste'=>字符串'sweet'(长度=5)
输出:
尺寸:小
颜色:红色
味道:苦
尺寸:大
颜色:绿色
味道:甜
问题:
class fruit
{
private $_type;
private $_size;
private $_colour;
private $_taste;
public function __construct($type,$size,$colour,$taste)
{
$this->_type = $type;
$this->_size = $size;
$this->_colour = $colour;
$this->_taste = $taste;
}
public function displayFruitData()
{
echo'<br>'.$this->_type.'<br>'.$this->_size.'<br>'.$this->_colour.'<br>'.$this->_taste.'<br><br>';
}
}
$fruit1 = new fruit("apple","small","red","bitter");
$fruit2 = new fruit("pear","medium","yellow","sweet");
$fruit3 = new fruit("pineapple","large","brown","sweet");
$output = $fruit1->displayFruitData();
$output = $fruit2->displayFruitData();
$output = $fruit3->displayFruitData();
exit();
类水果
{
私有$_型;
私人美元大小;
私人色彩;
私家口味;
公共功能结构($type、$size、$color、$taste)
{
$this->_type=$type;
$this->\u size=$size;
$this->_color=$color;
$this->_taste=$taste;
}
公共函数displayFruthData()
{
回显“
”.$this->u类型“
”.$this->u大小“
”.$this->u颜色“
”.$this->u味道。”
;
}
}
$FROUT1=新水果(“苹果”、“小”、“红”、“苦”);
$FROUT2=新水果(“梨”、“中”、“黄”、“甜”);
$FROUT3=新水果(“菠萝”、“大”、“棕色”、“甜”);
$output=$FROUT1->DISPLAYFROUTDATA();
$output=$fruit2->displayFruitData();
$output=$fruit3->displayFruitData();
退出();
您可以像这样简单地完成此操作
<?php
$fruits = array('apple', 'pear');
$size = array('apple' => 'small', 'pear' => 'big');
$colour = array('apple' => 'red', 'pear' => 'green');
$taste = array('apple' => 'bitter', 'pear' => 'sweet');
foreach($fruits as $fruit)
{
echo "$fruit Size is {$size[$fruit]}<br />";
echo "$fruit Colour is {$colour[$fruit]}<br />";
echo "$fruit Taste is {$taste[$fruit]}<br />";
echo '<br />';
}
?>
不使用对象的任何原因?不要使用$key表示数组的不同级别元素,也不要同时使用key和VALE;这简直令人困惑。
apple Size is small
apple Colour is red
apple Taste is bitter
pear Size is big
pear Colour is green
pear Taste is sweet