print value of given array with PHP

1

I have this array

Array ( [0] => stdClass Object ( 
  [product_image] => http://placehold.it/250x150/2aabd2/ffffff?text=Product+2 
  [product_name] => Product 2 
  [product_desc] => Product details 
  [product_size] => S 
  [product_price] => 5435.50 
  [product_id] => 145 
  [product_quantity] => 1 
  [unique_key] => 1504538986397 
) 
[1] => stdClass Object ( 
  [product_image] => http://placehold.it/250x150/2aabd2/ffffff?text=Product+1 
  [product_name] => Product 1 
  [product_desc] => Product details 
  [product_size] => S 
  [product_quantity] => 1 
  [product_price] => 2990.50 
  [product_id] => 12 
  [unique_key] => 1504539624302 
) 
[2] => stdClass Object ( 
 [product_image] => http://placehold.it/250x150/2aabd2/ffffff?text=Product+3 
  [product_name] => Product 3 
  [product_desc] => Product details 
  [product_size] => S 
  [product_price] => 5435.50 
  [product_id] => 145 
  [product_quantity] => 1 
  [unique_key] => 1504539625645 
) 
[3] => stdClass Object ( 
  [product_image] => http://placehold.it/250x150/2aabd2/ffffff?text=Product+6 
  [product_name] => Product 6 
  [product_desc] => Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
  [product_size] => S 
  [product_price] => 5435.50 
  [product_id] => 145 
  [product_quantity] => 1 
  [unique_key] => 1504539627863 
) 
[4] => stdClass Object ( 
  [product_image] => http://placehold.it/250x150/2aabd2/ffffff?text=Product+5 
  [product_name] => Product 5 
  [product_desc] => Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
  [product_size] => S 
  [product_price] => 3410.00 
  [product_id] => 155 
  [product_quantity] => 1 
  [unique_key] => 1504539628585 
) 
[5] => stdClass Object ( 
  [product_image] => http://placehold.it/250x150/2aabd2/ffffff?text=Product+4 
  [product_name] => Product 4 
  [product_desc] => Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
  [product_size] => S 
  [product_price] => 435.50 
  [product_id] => 154 
  [product_quantity] => 1 
  [unique_key] => 1504539629309 
) 
) 

How would you print the value of [product_price] only with PHP?

    
asked by anonymous 04.09.2017 / 18:08

3 answers

1

To print only the product_price field that is in a% of objects #

$items = array(); // esse array é do pergunta

foreach($items as $item)
{
    echo $item->product_price;
}

04.09.2017 / 18:19
0

Because the data is within array , you need to use loop as for , foreach or while , see below an example with foreach :

$dados = [];
foreach($dados as $item) {
    echo $item->product_price;
}
    
04.09.2017 / 18:20
0

Assuming this vector is in the $ vector variable.

Code to print the price of a specific item, for example, from the first (position 0):

echo $vetor[0]->product_price;

Code to print the price of all vector items:

foreach ($vetor as $produto){
    echo "Valor: ". $produto->product_price."<br/>";
}
    
04.09.2017 / 18:22