0

I have the following array which I want to make readable for users:

Array
(
    [3] => Array
        (
            [id] => 3
            [price] => 4.00
            [per] => day
            [count] => 
            [type] => single
            [status] => T
            [extra_quantity] => 1
        )

    [2] => Array
        (
            [id] => 2
            [price] => 12.00
            [per] => day
            [count] => 
            [type] => single
            [status] => T
            [extra_quantity] => 1
        )

)

I want to echo this to show users the following readable line for every subarray:

[id] = [price] per [day]

I am fairly new to arrays, especially nested one like the one I have above, how should I go with this to get an output like I need? Thanks!

2
  • Hi, try: foreach($array as $item) { echo $item['id'] . ' = ' . $item['price'] . ' per ' . $item['per']; } Commented Apr 12, 2017 at 9:02
  • what you have is a multi-dimensional array. Do a foreach and reference the key Commented Apr 12, 2017 at 9:02

2 Answers 2

2

use foreach() built in function which help you get all data as readable

    foreach ($rows as $row) {
            echo $row['price'].'per'.$row['per'];
            echo "<br>";
        }

check for more information

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for the info, works perfectly. I learn something new every day :)
1

FOreach loops are used when iterating over arrays.The above is a multi-dimensional array. Meaning An array in an array

  foreach($array as $value):
    if(array_key_exists($value['id'], $value) && array_key_exists($value['price'], $value) && array_key_exists($value['per'], $value)):
     #we have the keys present.
     echo $value['price'].'per'.$value['per'];
    endif;
    endforeach;

2 Comments

Thanks a lot for the info, works perfectly. I learn something new everyday :)
Awesome! My pleasure!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.