Home > Enterprise >  Extract element from array
Extract element from array

Time:02-18

I know i can extract the value of price using echo $prod_option[0]['product_option_value'][0]['price'];

How can i extract all the [price] values from my array if i don't know how many arrays the product_option_value has ?

EDIT: Seems i didn't explain what i am trying to do very well. What i need to do is extract all products from an Opencart website and if the product has options create each option as it's own product in the csv This is my function

    public function index()
    {
        /*header("Content-Type: text/csv"); 
        header("Content-Disposition: attachment; filename=faceboook-feed.csv");
        header("Cache-Control: no-cache, must-revalidate");
        header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");   
        print "title,description,google_product_category,link,image_link,availability,price,brand,condition".PHP_EOL;*/
        $this->load->model('catalog/product');
        $products = $this->model_catalog_product->getProducts();



        
        foreach($products as $product)
        {
            $price=($product['special'] ? $product['special'] :$product['price']);
            $prod_option = $this->model_catalog_product->getProductOptions($product['product_id']);
            
        
            
            if(!empty($prod_option)){
                foreach($prod_option as $innerArray)
                {
                    if(is_array($innerArray)){
                        foreach($innerArray as $innerArray2) {
                            if(is_array($innerArray2)){
                                foreach($innerArray2 as $innerArray3){
                                    $csv_line.='"'.$this->clean($product['name']).$innerArray3['name'].'",';
                                    $csv_line.='"'.$this->clean($product['description']).'",';
                                    $csv_line.='"Hardware > Building Materials > Flooring & Carpet",';
                                    $csv_line.='"'.str_ireplace("&","&",$this->url->link('product/product', 'product_id='.$product['product_id'])).'",';
                                    $csv_line.='"'.trim(HTTP_SERVER,"/")."/image/".$product['image'].'",';
                                    $csv_line.='"'.($product['status']==1 ? 'in stock':'out of stock').'",';
                                    $tmpPrice = round($this->tax->calculate($price,$product['tax_class_id'], $this->config->get('config_tax')),2);
                                    if($innerArray3['price_prefix'] == "-")
                                    {
                                        $tmpPrice = $tmpPrice - $innerArray3['price'];
                                    }
                                    else if($innerArray3['price_prefix'] == " ")
                                    {
                                        $tmpPrice = $tmpPrice   $innerArray3['price'];
                                    }
                                    $csv_line.='"'.$tmpPrice.'",';
                                    $csv_line.='"'.(empty($product['manufacturer']) ? $product['name'] : $product['manufacturer']).'",';
                                    $csv_line.='"new"';
                                    print $csv_line.PHP_EOL;
                                }
                            }
                        }
                        
                    }
                }
            } else {
                $csv_line.='"'.$this->clean($product['name']).'",';
                $csv_line.='"'.$this->clean($product['description']).'",';
                $csv_line.='"Hardware > Building Materials > Flooring & Carpet",';
                $csv_line.='"'.str_ireplace("&","&",$this->url->link('product/product', 'product_id='.$product['product_id'])).'",';
                $csv_line.='"'.trim(HTTP_SERVER,"/")."/image/".$product['image'].'",';
                $csv_line.='"'.($product['status']==1 ? 'in stock':'out of stock').'",';
                $csv_line.='"'.round($this->tax->calculate($price,$product['tax_class_id'], $this->config->get('config_tax')),2).' '.$this->session->data['currency'].'",';
                $csv_line.='"'.(empty($product['manufacturer']) ? $product['name'] : $product['manufacturer']).'",';
                $csv_line.='"new"';
                print $csv_line.PHP_EOL;
            }
        }
    }

My array looks like this

Array
(
    [0] => Array
        (
            [product_option_id] => 226
            [product_option_value] => Array
                (
                    [0] => Array
                        (
                            [product_option_value_id] => 15
                            [option_value_id] => 39
                            [name] => Red
                            [image] => 
                            [quantity] => 2
                            [subtract] => 1
                            [price] => 10.0000
                            [price_prefix] =>  
                            [weight] => 0.00000000
                            [weight_prefix] =>  
                        )

                    [1] => Array
                        (
                            [product_option_value_id] => 16
                            [option_value_id] => 40
                            [name] => Blue
                            [image] => 
                            [quantity] => 5
                            [subtract] => 1
                            [price] => -10.0000
                            [price_prefix] =>  
                            [weight] => 0.00000000
                            [weight_prefix] =>  
                        )

                )

            [option_id] => 5
            [name] => Select
            [type] => select
            [value] => 
            [required] => 1
        )

)

CodePudding user response:

To make it visual for yourself you can do this.

$prices = array(); 

foreach($prod_option[0]['product_option_value'] as $product_option_value) {
    $prices[] = $product_option_value["price"];
}

CodePudding user response:

Here's a way to do it without a loop using array_column():

$prices = array_column($prod_option[0]['product_option_value'], 'price');

CodePudding user response:

Personally I'd make a loop for collecting the prices

$arr = $prod_option[0]['product_option_value'];
$prices = [];
for(let i=0;i<arr.length;i  )
    $prices[] = $arr[i]['price'];
  • Related