print value of a given array with PHP

Asked

Viewed 866 times

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 with PHP the value of [product_price]?

  • some of the answer served you ???

3 answers

1

0

How the data is inside a array, you will need to use a loop as for, foreach or while, see below an example with foreach:

$dados = [];
foreach($dados as $item) {
    echo $item->product_price;
}

0

Assuming that this vector is in the variable $vector.

Code for printing the price of a specific item, for example of the first (heading 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/>";
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.