Jump to content

[SOLVED] loop through an array


Roy Barten

Recommended Posts

hello,

 

I create the following code:

 

$array = array (

"name" => "laptop 1",

"username" => "user 1",

"start" => "2009-01-31 10:00",

"end" => "2009-01-31 12:00"

);

 

$array = array (

"name" => "laptop 1",

"username" => "user 2",

"start" => "2009-02-01 10:00",

"end" => "2009-02-03 12:00"

);

 

$array = array (

"name" => "laptop 2",

"username" => "user 1",

"start" => "2009-03-01 10:00",

"end" => "2009-03-03 12:00"

);

 

$array = array (

"name" => "laptop 2",

"username" => "user 2",

"start" => "2009-03-01 12:00",

"end" => "2009-03-03 14:00"

);

 

$array = array (

"name" => "laptop 3",

"username" => "user 2",

"start" => "2009-03-01 12:00",

"end" => "2009-03-03 14:00"

);

echo "<pre>";

print_r($array['name']);

echo "</pre>";

 

unset($array);

 

/* array 1 item */

$array = array (

'name' => "laptop 1",

'username' => "user 1",

'start' => "2009-01-31 10:00",

'end' => "2009-01-31 12:00"

);

 

$array = array (

"name" => "laptop 1",

"username" => "user 2",

"start" => "2009-02-01 10:00",

"end" => "2009-02-03 12:00"

);

 

 

How can I loop trough this array, on that way that i can chose a key and I get all of the values out. For example, I want all the values from $array['start']. I want the results as following:

 

2009-01-31 10:00

2009-02-01 10:00

2009-03-01 10:00

 

How do i do this? PLEASE HELP!!!

Link to comment
Share on other sites

 

 

Hai

 

run this to get teh result

 

<?php

$array  = array(array (

                              "name"      => "laptop 1",

                              "username"    => "user 1",

                              "start"    => "2009-01-31 10:00",

                              "end"      => "2009-01-31 12:00"

                          ),

 

 

array (

                              "name"      => "laptop 2",

                              "username"    => "user 1",

                              "start"    => "2009-03-01 10:00",

                              "end"      => "2009-03-03 12:00"

                          ),

 

 

                         

array (

                              "name"      => "laptop 3",

                              "username"    => "user 2",

                              "start"    => "2009-03-01 12:00",

                              "end"      => "2009-03-03 14:00"

                          ));                         

echo "<pre>";

foreach($array as $val)

{

print_r($val['start'].'<br>');

}

echo "</pre>";

 

?>

Link to comment
Share on other sites

Oke, thanks for you reply but I tried to loop through it with the foreach function, but it does not work. This is the output:

 

Value: l

Value: u

Value: 2

Value: 2

 

I am just started with creating arrays etc so maybe I do some stupid things in my code. But here is the code i use to loop trough it:

 

foreach ($array as $value)

{

  echo "Value: " . $value['start'] . "<br />";

}

 

Please can you help me?

Link to comment
Share on other sites

<?php

$array  = array(array (

                              "name"      => "laptop 1",

                              "username"    => "user 1",

                              "start"    => "2009-01-31 10:00",

                              "end"      => "2009-01-31 12:00"

                          ),

 

 

array (

                              "name"      => "laptop 2",

                              "username"    => "user 1",

                              "start"    => "2009-03-01 10:00",

                              "end"      => "2009-03-03 12:00"

                          ),

 

 

                         

array (

                              "name"      => "laptop 3",

                              "username"    => "user 2",

                              "start"    => "2009-03-01 12:00",

                              "end"      => "2009-03-03 14:00"

                          ));                         

echo "<pre>";

foreach($array as $val)

{

print_r($val['start'].'<br>');

}

echo "</pre>";

 

?>

Link to comment
Share on other sites

<?php
$data = array(array("name"     => "laptop 1",
                    "username" => "user 1",
                    "start"    => "2009-01-31 10:00",
                    "end"      => "2009-01-31 12:00"),
              array("name"     => "laptop 1",
                    "username" => "user 1",
                    "start"    => "2009-02-01 10:00",
                    "end"      => "2009-02-03 12:00"),
              array("name"     => "laptop 1",
                    "username" => "user 1",
                    "start"    => "2009-03-01 10:00",
                    "end"      => "2009-03-03 12:00"),
              array("name"     => "laptop 1",
                    "username" => "user 1",
                    "start"    => "2009-04-01 10:00",
                    "end"      => "2009-04-03 12:00"),
              array("name"     => "laptop 1",
                    "username" => "user 1",
                    "start"    => "2009-05-01 10:00",
                    "end"      => "2009-05-03 12:00"));

foreach($data as $key => $val)
    print $val["start"] ."<br />";

?>

 

Returns:

2009-01-31 10:00
2009-02-01 10:00
2009-03-01 10:00
2009-04-01 10:00
2009-05-01 10:00

 

edit - im slow

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.