2013-04-06 156 views
3

我正在整理一些工作中遇到的舊代碼,包括各種數組,我想將它們組合成嵌套數組,並希望知道循環嵌套數組內容的一種簡單方法(可能是首先存在比嵌套數組更好的數據存儲方式,如果有的話,歡迎提供建議)。PHP:訪問嵌套數組元素的有效方法?

以下代碼僅僅是原始代碼結構的一個示例,其行爲與普通輸出相同。

使用$的someArray元素被indifvidually定義似乎非常耗時,因此管理我爲什麼要改變它:

$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(); 
+2

你不使用對象的原因嗎? – 2013-04-06 13:31:01

+0

請勿使用$ key來表示數組的不同級別元素以及鍵和值;這只是令人困惑。 – 2013-04-06 13:32:20

回答

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 
+0

謝謝,對不起,我直到現在纔看到您的回覆。謝謝你的時間。 – Jim 2013-04-06 15:27:04