PHP如何从多维数组中获取值?

时间:2017-10-04 13:04:59

标签: php arrays multidimensional-array

我在课堂上有一个阵列,想要获得' Apple'关键价值(' iPhone')。 我假设需要使用每个循环。

我将如何做到这一点?

更新:我还需要指定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'
                ),
            )
        );
    }
}

4 个答案:

答案 0 :(得分:2)

我创建了一个函数,您可以或多或少地为它提供任何产品,它将从数组中返回键和值

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

注意:$ phoneType,$ productsArr将定义它们在其他方法中使用或传递的方法,或者在类中定义全局变量。

答案 1 :(得分:1)

如果您想要单次入场:

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

如果有多个数据,您可以使用foreach:

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

答案 2 :(得分:0)

试试

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

答案 3 :(得分: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";
                    }
                }
        }

}