3

Hello guys I just want to ask how can i get the array values within an array? I also used the unserialize function and it returns to array. That's why I got a lists of array.

Here's my sample output for the array:

Array
(
    [0] => Array
        (
            [id] => 332
            [data] => Array
                (
                    [points] => 100
                    [cost] => 100
                )

        )

    [1] => Array
        (
            [id] => 1552
            [data] => Array
                (
                    [points] => 100
                    [cost] => 100
                )

        )

    [2] => Array
        (
            [id] => 1885
            [data] => Array
                (
                    [points] => 294
                    [cost] => 294
                )

        )
     .
     .

Now I loop it again to assign in a variable:

 for($x = 0; $x < sizeof($var_data); $x++){

            echo "ID: ".$var_data[$x]['id']."<br />";

            foreach($var_data as $key => $value){
                // it is the part that i don't know how to get the data array values and assign in a varaile.
            }

 }

Here's what i want to do:

ID: 1
COST: 100.00
TOTAL: 100.00

ID: 2
COST: 65.00
TOTAL: 65.00

ID: 3
COST: 40.00
TOTAL: 40.00
.
.

How can I do that? Is there a simple way?

2 Answers 2

4

You can use foreach.

foreach($var_data as $data)
{
    echo "ID: ".$data['id']."<br />";
    echo "COST: ".$data['data']['cost']."<br />";
    echo "TOTAL: ".$data['data']['points']."<br />";
}
Sign up to request clarification or add additional context in comments.

Comments

0

Use foreach

$output= '';
foreach($var_data as $data) {
  $output = '<div>';
  $output= '<p>ID: '.$data['id'].'</p>';
  $output= '<p>COST: '.$data['data']['cost'].'</p>';
  $output= '<p>TOTAL: '.$data['data']['points'].'</p>';
}

echo $output;

Comments

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.