簡體   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