2017-10-04 121 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"; 
        } 
       } 
     } 

}