2017-10-04 55 views
1

我有一個類中的數組,想獲得'Apple'鍵值('iPhone')。 我假設每個循環都需要使用。PHP如何從多維數組中獲取值?

我該怎麼做呢?

更新:我還需要指定customerType和productType鍵值。

class Products { 

    public function products_list() { 

     $customerType = 'national'; 
     $productType = 'phones'; 
     $phoneType = 'Apple'; 

     $productsArr = array(); 
     $productsArr[] = array(
      'customerType' => 'national', 
      'productType' => array(
       'phones' => array(
        'Apple' => 'iPhone', 
        'Sony' => 'Xperia', 
        'Samsung' => 'Galaxy' 
       ), 
       'cases' => array(
        'leather' => 'black', 
        'plastic' => 'red', 
       ), 
      ), 
      'international' => array(
       'phones' => array(
        'BlackBerry' => 'One', 
        'Google' => 'Pixel', 
        'Samsung' => 'Note' 
       ), 
       'cases' => array(
        'leather' => 'blue', 
        'plastic' => 'green' 
       ), 
      ) 
     ); 
    } 
} 
+0

這會來多次inisde主陣列或只有一次? –

+0

您是否嘗試過使用該foreach,但它不起作用? – AllisonC

+0

它可以在主陣列中多次。 – ianhman

回答

1

如果你想要一個條目:

echo $productsArr[0]['productType']['phones']['Apple']."<br />"; 

如果有多個數據,你可以使用的foreach:

foreach ($productsArr as $prod){ 
     echo $prod['productType']['phones']['Apple']."<br />"; 
    } 
0

只是嘗試

foreach($productsArr[0]['productType']['phones'] as $phones){ 
    echo $phones[$phoneType]; } 
1

我創建一個你可以或多或少地給它的函數任何產品,它將返回鍵和值從陣列

<?php 

class Products 
{ 

    public function products_list() 
    { 

     $customerType = 'national'; 
     $productType = 'phones'; 
     $phoneType = 'Apple'; 
     $productsArr[] = array(
      'customerType' => 'national', 'productType' => array(
       'phones' => array(
        'Apple' => 'iPhone', 
        'Sony' => 'Xperia', 
        'Samsung' => 'Galaxy' 
       ), 
       'cases' => array(
        'leather' => 'black', 
        'plastic' => 'red', 
       ), 
      ), 
      'international' => array(
       'phones' => array(
        'BlackBerry' => 'One', 
        'Google' => 'Pixel', 
        'Samsung' => 'Note' 
       ), 
       'cases' => array(
        'leather' => 'blue', 
        'plastic' => 'green' 
       ), 
      ) 
     ); 

     echo $this->get_value($phoneType, $productsArr) .'<br>'; 
     echo $this->get_value($customerType, $productsArr) .'<br>'; 
     echo $this->get_value($productType, $productsArr) .'<br>'; 
    } 

    function get_value($product, array $products) 
    { 
     $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($products), RecursiveIteratorIterator::CHILD_FIRST); 

     foreach ($iterator as $key => $value) { 
      if (is_string($value) && ($key == $product)) { 
       return 'key ->' . $key .' value ->'.$value; 
      } 
     } 
     return ""; 
    } 
} 

$phone_products = new Products(); 


$phone_products->products_list(); 

的類中使用它只需調用

$this->get_value($phoneType, $productsArr); 

從無級呼叫

$phone_products = new Products(); 



echo ($phone_products->get_value($phoneType, $productsArr)); 
//output: key ->Apple value ->iPhone 

NB:$ phoneType,$ productsArr將被定義爲它們在其中使用或從其他方法傳遞的方法,或者定義類中的全局變量。

+0

謝謝,雖然這將如何在課堂上工作? BTW已經更新了我的問題,因爲我還需要指定customerType和productType鍵值。 – ianhman

+0

@ianhman我編輯瞭解決方案。要調用customer類型,只需將'$ phonetype'替換爲customerType,因爲它們都在同一個數組中。例如$ this-> get_value($ customerType,$ productsArr);或$ this-> get_value($ productType,$ productsArr); –

+0

感謝編輯,雖然我無法讓它與課程一起工作。我正在使用PHPStorm IDE,它在方法的開始時不識別數組$ productsArr:public function get_value($ product,array $ productsArr) 此外,在回顯類值時,變量顯示爲未定義的回顯($ phone_products- > get_value($ phoneType,$ productsArr)); – ianhman

0
foreach($productsArr as $key1 => $data1){ 
    if(is_array($data1)) 
     foreach($data1 as $key2 => $data2){ 
      if(is_array($data2)) 
       foreach($data2 as $key3 => $data3){ 
        if(array_key_exists('Apple', $data3)) { 
         echo $data3['Apple'] ."\n"; 
        } 
       } 
     } 

}