2

I have an array in a class and want to obtain the 'Apple' key value ('iPhone'). I assume a for each loop needs to be used.

How would I go about doing this?

UPDATE: I also need to specify the customerType and productType key values.

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'
                ),
            )
        );
    }
}
5
  • this will come multiple times inisde main array or only one time? Commented Oct 4, 2017 at 13:06
  • have you tried to use the foreach and it didn't work?
    – AllisonC
    Commented Oct 4, 2017 at 13:07
  • It could be multiple times in the main array.
    – ianhman
    Commented Oct 4, 2017 at 13:10
  • @ianhman can you give an example for this:- It could be multiple times in the main array? May be i can give a much shorter code? Commented Oct 4, 2017 at 13:18
  • So the Apple key could appear more than once in the array.
    – ianhman
    Commented Oct 4, 2017 at 13:32

4 Answers 4

2

I have created a function that you can more or less give it any product and it will return the key and value from the array

<?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();

To use it within the class just call

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

from without the class call

$phone_products = new Products();



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

NB: $phoneType, $productsArr will either be defined the methods they are being used in or passed from other methods or define global variables within the class.

3
  • Thanks although how would this work in a class? BTW have updated my question as I also need to specify the customerType and productType key values.
    – ianhman
    Commented Oct 4, 2017 at 14:10
  • @ianhman I have edited the solution. To call customer type just replace '$phonetype' with the customerType since they are all within the same array. e.g $this->get_value($customerType , $productsArr); or $this->get_value($productType , $productsArr);
    – Kevin R
    Commented Oct 4, 2017 at 14:18
  • thanks for editing although I cannot get it to work with the class. I'm using PHPStorm IDE which does not recognise array $productsArr in the start of the method: public function get_value($product, array $productsArr) Also where I am echoing the class value the variables appear as undefined echo ($phone_products->get_value($phoneType, $productsArr));
    – ianhman
    Commented Oct 4, 2017 at 15:35
1

If you want single entry:

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

If there are multiple data, you can use foreach:

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

just try

foreach($productsArr[0]['productType']['phones'] as $phones){
     echo $phones[$phoneType]; }
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";
                    }
                }
        }

}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.