繁体   English   中英

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

[英]PHP: Efficient way to access nested array elements?

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

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

$ 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. 有没有更好的方法来调用$ someArray数据而没有嵌套的foreach循环?

考虑

  1. 嵌套数组可能会变得更复杂,可能会有更深层次的附加层
  2. 不希望在此方案中使用数据库

先感谢您

更新

我使用一个对象重新编写如下。

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

你可以这样轻松地做到这一点

<?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

暂无
暂无

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

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