PHP:访问嵌套数组元素的有效方法?

时间:2013-04-06 13:24:42

标签: php arrays nested-attributes multidimensional-array

我正在整理一些旧的代码,我遇到过一些工作,包括各种数组,我想将它们组合成嵌套数组,并希望知道一种循环嵌套数组内容的简单方法(可能首先存在比嵌套数组更好的存储数据的方法,如果有的话,建议欢迎)。

以下代码只是原始代码结构的一个示例,其行为方式与广义输出相同。

$ someArray元素过去是单独定义的,这似乎非常耗时,因此我想改变它的原因:

$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)  

输出

尺寸:小 颜色:红色 味道:苦 尺寸:大 颜色:绿色 味道:甜蜜

问题

  1. 压缩$ fruits数组的最佳方法是什么?我的$ someArray方法是不正确的?
  2. 有没有更好的方法在没有嵌套的foreach循环的情况下调用$ someArray数据?
  3. 考虑

    1. 嵌套数组可能会变得更复杂,可能会有更深层次的
    2. 不希望为此方案使用数据库
    3. 提前谢谢

      更新

      我使用对象重新编写了如下内容。

      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();
      

1 个答案:

答案 0 :(得分:1)

你可以像这样轻松地完成这个

<?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 />';
  }
?>

输出

apple Size is small
apple Colour is red
apple Taste is bitter

pear Size is big
pear Colour is green
pear Taste is sweet