Closed Thread Icon

Topic awaiting preservation: PHP - convert multidimensional array to associative array easily? (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=26857" title="Pages that link to Topic awaiting preservation: PHP - convert multidimensional array to associative array easily? (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: PHP - convert multidimensional array to associative array easily? <span class="small">(Page 1 of 1)</span>\

 
Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 10-17-2005 06:44

I have an array returned as result of a SQL query, but I need to feed the contents of that array to a preg_match() function. So, I need to convert it to an associative array, and I'm curious to know if there is an easier way to do that. Currently, I use a WHILE loop using mysql_fetch_array and just build an array from that.

Thanks for any comments and ideas.

DmS
Maniac (V) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

posted posted 10-17-2005 14:44

How does your query look like?
If you use "mysql_fetch_array($result, MYSQL_ASSOC)"
or "mysql_fetch_assoc()"
you'll get an associative array with fieldnames as keys.

Note that if your query returns more than one row you still need to loop the result
and build an array with the rows.
/Dan

{cell 260} {Blog}
-{Proudly running OSX, Debian, WXP, W98, well not so proudly on the last 2...}-
-{ ?There are two major products that come out of Berkeley: LSD and UNIX. We don't believe this to be a coincidence. - Jeremy S. Anderson" }-
-{"Light travels faster than sound. This is why some people appear bright until you hear them speak.?}-

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 10-17-2005 15:41

Basically, I have two associative arrays that I use in some preg_replace statements. They look like this:

$citySearch = array ("'^CENTERLINE$'", "'^FAIRHAVEN$'", "'^ROSEVILLEN$'" .....);
$cityReplace = array ("CENTER LINE", "FAIR HAVEN", "ROSEVILLE" ....);

But I'm hardcoding those arrays. Since I'm constantly updating those arrays by hand, I want to build a web based admin app that will just let me dump the two arrays into two mysql tables and do the admin there.. Then, when my functions run, I can select them. But the only way I can think of doing this is WHILE looping via mysql_fetch_array, and just creating the associative arrays that way.

Something like this:
$i=1;
while ($rows=mysql_fetch_array($mysearch)){
$mysearcharray[$i] = "$mysearch['city'],";
$i++;
}

I know this will work, as I'm doing it elsewhere. I just want to know if there is an easier way. Maybe some super secret function that does that automatically.

DmS
Maniac (V) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

posted posted 10-17-2005 20:49

I'm sort of getting what you need out of this, but not quite...
As for your code snip, that's basically how I do it if I want to load an array with arrays of data representing rows in the db. I seldom use the $i in a while loop though unless I've got a very good reason.

But your search and replace arrays...
What you are doing is basically correcting citynames, right?

Where do the faulty ones come from? User input or?
If so you can get a country/city database that you can query through AJAX as the user starts to enter the name of the city, 'ala googles autosuggest. That way you'll get correct data in the db to start with. We've done that in a signup process at one of the sites we run at work.
That really cleaned up the "less than correct" user input

Or perhaps I'm way off...
/Dan

{cell 260} {Blog}
-{Proudly running OSX, Debian, WXP, W98, well not so proudly on the last 2...}-
-{ ?There are two major products that come out of Berkeley: LSD and UNIX. We don't believe this to be a coincidence. - Jeremy S. Anderson" }-
-{"Light travels faster than sound. This is why some people appear bright until you hear them speak.?}-

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 10-18-2005 06:37

Nope - the 'bad' city names are from data I'm scrapping. Not much I can do about it. And, the list of bad cities is growing.

What I'm thinking of doing is just having a table with a bad city name in one field, and the corrected in another. Then, building two arrays, I can search & replace.

I wish AJAX would do it, but the mistakes are already in the database that I'm accessing. (although I wouldn't mind seeing your AJAX method - I've always like Google Suggest)

I'm just trying to get away from editing the PHP pages.

DmS
Maniac (V) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

posted posted 10-18-2005 10:54

Sorry, a no can do on the suggest thingie, company code...

For the other part, I think you might be able to do it with regex directly in the SQL, but I don't know how, nor how fast it would be.

I think one way might be to use a single query grabbing info from both tables using something like: ...SELECT correct_data.city as goodCity, bad_data.city as badCity FROM correct_data, bad_data... etc. and then work it from there. At least the good & bad will be clearly marked.


Or, just go with the way you already know
/D

{cell 260} {Blog}
-{Proudly running OSX, Debian, WXP, W98, well not so proudly on the last 2...}-
-{ ?There are two major products that come out of Berkeley: LSD and UNIX. We don't believe this to be a coincidence. - Jeremy S. Anderson" }-
-{"Light travels faster than sound. This is why some people appear bright until you hear them speak.?}-

« BackwardsOnwards »

Show Forum Drop Down Menu