Frosty
Frosty

Reputation: 334

Display certain variables from inside an array

I have a product array that is coming from a database.

I just want to print all the variables that are called [sku] inside that said array.

If I print it out like this

<?php echo $products[0]['sku']; ?>

I get only the 1-st value of SKU

how can i display all of the SKU variables without going [0] , [1] , [2] and so on.

Upvotes: 1

Views: 34

Answers (3)

Chuks Udoka
Chuks Udoka

Reputation: 1

You can try print_r() instead of echo.

Upvotes: 0

catalin
catalin

Reputation: 31

You can try print_r, but that will give you all the array values. A simple foreach will do the job and only print the SKU for every product.

foreach($products as $value) {
    echo $value['sku'] . "\n";
}

Upvotes: 1

1.618033
1.618033

Reputation: 46

I think this will do what you want

<?php

foreach ($products as $key => $row) {
    var_export($row['sku']);
}

http://php.net/manual/en/control-structures.foreach.php

Upvotes: 3

Related Questions