Skip to content
Advertisement

Unable to print multidimensional array in desirable string format dynamically?

I’m unable to print multidimensional array in desirable string format. I tried lot but didn’t get the right string format.

Here are the multidimensional array that I want to print dynamically:

  $cars = array (
    array("Volvo",22,18),
    array("BMW",15,13),
    array("Saab",5,2),
    array("Land Rover",17,15)
  );

And here are the code that tried

  $cars = array (
    array("Volvo",22,18),
    array("BMW",15,13),
    array("Saab",5,2),
    array("Land Rover",17,15)
  );

  foreach ($cars as $car) {
    foreach ($car as $c => $v) {
        echo $v. '<hr>';
    }
  }

Following is the desirable string format, that I want to get:

Volvo: In stock: 22, sold: 18.

BMW: In stock: 15, sold: 13.

Saab: In stock: 5, sold: 2.

Land Rover: In stock: 17, sold: 15.

Advertisement

Answer

Maybe something like this?

foreach ($cars as $car) {
        echo "$car[0]: In stock: $car[1], sold: $car[2] <hr>";
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement