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.
It could be multiple times in the main array? May be i can give a much shorter code?