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
https://forums.phpfreaks.com/topic/145045-solved-loop-through-an-array/
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>";

 

?>

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?

<?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>";

 

?>

<?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

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.