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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.