Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post

    referencing array data

    Hey,

    I've got an array which I've made up with data from the database. It's looking like this at the moment:

    Code:
    $staff_array[$staffID] = $db_row['staff_id'];
    how would I then loop through the contents of the staff array?? I normally use something like

    Code:
    for ($x=0;$x<sizeof($staff_array);$x++) {
    	echo "This staff = " . $staff_array[$x] . "<br />";
    }
    the above won't send anything out, as the staff numbers in the array are set up like "A332012" and "A332673". How can I reference the actual name of the array element?? I also have other elements within that array like the name, age etc.

    thanks for any help!

  • #2
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    PHP Code:
    foreach($staff_array as $staff){
    echo 
    "This staff = " $staff "<br />";


  • #3
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    yeah that's the sort of thing - maybe I should be clearer about what's in the array!! I was cutting the code down so that I could try and get a quick answer..

    PHP Code:


    while ($row mysql_fetch_array($result) {
    $years_array = array("2003" => $row['2003'], "2004" => $row['2004'], "2005" => $row['2005']);
    $staff_array[$row['staff_id'] = $years_array;

    so the above would create an array like

    Code:
    array(
    	"A118221" =>
    		array(
    			"2003" => 221,
    			"2004" => 215,
    			"2005" => 227
    		),
    	"A118226" =>
    		array(
    			"2003" => 212,
    			"2004" => 288,
    			"2005" => 221
    		)
    )
    so the actual array is named the staff ID, and it has a nested array inside it. All I need to do is loop through the whole staff array listing the staff ID then each "year" within it. Sorry if this isn't clear!!

  • #4
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    PHP Code:
    foreach($staff_array as $k1 => $v1){
    echo 
    "This staff = " $k1 "<br />";
    foreach (
    $v1 as $k2 => $year){
    echo 
    ' Year '.$k2.': '.$year.'<br />';
    }
    echo 
    '<br />';

    Is that what you wanted?

  • Users who have thanked Mwnciau for this post:

    homerUK (09-04-2007)

  • #5
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    worked perfectly!! thanks!!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •