Jump to content

Create a while loop from an array


nzol

Recommended Posts

HI all,

 

I need help to get data out of an array using a while loop or something simalar.

 

What I have, is this:

 

<?php

$data = array(

  1  => array(
                         "name" => "Jordan Smith",
		 "age"    => "16 Years 0 Months"
	),
  2  => array(
                        "name" => "Olivia Smith",
                "age"    => "13 Years 12 Months"
	)
);

 

Now, what I did, is I constructed this:

while($result=$data[1]) {
echo $result['name'];
} 

But it echoed "Jordan Smith" for infinity down the page.

 

If anyone can help be do this, it will be great. Thanks

 

Link to comment
Share on other sites

There purpose is to extract specific values from an array

 

Kind of, but I'd say they're for looping through the contents of an array - or object - as opposed to extracting values from them.

Link to comment
Share on other sites

function print_arr($common1)

{

$callback =

function($value,$key)

{

            echo $value.'<br/>';

};

array_walk_recursive($common1,$callback);

 

}

$d=print_arr($data);

echo $d;

 

Hope you find it useful.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.