navigating arrays

Ask about a PHP problem here.
Post Reply
adamaoc
Posts: 2
Joined: Fri Aug 10, 2012 4:47 pm

navigating arrays

Post by adamaoc »

Hey guys, I'm new to the form but have been watching the Better php vids on Youtube for a while now and love them...

First big stump that I cant really find anything on that makes sense to me... maybe someone here can help.

I'm working with an array with nested arrays
something like this:
[0] => Array
        (
            [id] => 1
            [add] => 4247 DALLAs
            [city] => Dallas
            [state] => Texas
            [zip] => 75216
            [tenate] => open
            [owner] => Tester
            [photo] => /4247_Idaho/images/
            [aqu_date] => 2012-08-09 11:08:08
        )

    [1] => Array
        (
            [id] => 2
            [add] => 7621 McCommas
            [city] => Dallas
            [state] => Texas
            [zip] => 75252
            [tenate] => open
            [owner] => Tester
            [photo] => /7621_Mccallum/images/
            [aqu_date] => 2012-08-09 12:41:16
        )
this info is coming from a mysql db and I'm displaying it onto the page using a foreach loop...
the problem I'm running into is when I'm ready to edit info and UPDATE it in mysql -

I have access to the array and know the "id" number of the property I want to change... but I can't figure out how to tell php that I want to find the array with the id = 2, then grab the parent array[1] in this case, and use that array to display what i'll be editing...

what I have now is just taking the id number and subtracting it by 1 to get the array number... this works until you start to delete rows and the array and id's no longer match up.

Any thoughts??? need more info??

Thanks!

Adam
Last edited by jacek on Sun Aug 12, 2012 12:25 am, edited 1 time in total.
Reason: code tags...
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: navigating arrays

Post by jacek »

When you select the data you could put the ID as the index
while (($row = mysql_fetch_assoc($result)) !== false){
    $rows[$row['id']] = array(/* the things */);
}
Then if you know the Id you know which element you want :)
Image
adamaoc
Posts: 2
Joined: Fri Aug 10, 2012 4:47 pm

Re: navigating arrays

Post by adamaoc »

By cool thanks. That could work too.

I ended up just using a counter for each table row that corasponded with the item number in the array and that ended up working fine so far.

Thanks for the reply.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: navigating arrays

Post by jacek »

Yeah that is the other way :P It would take up a tiny bit more processing time, nothing worth worrying about.
Image
Post Reply