james2008 Posted September 16, 2008 Share Posted September 16, 2008 Hi! I'm new to PHP and I'm using 5.2.6. The old Website I'm looking at hard-codes some arrays. I've found a way to pull this information from a query, but having a hard time converting it to an array. Below is the old code, followed by my last attempt. In the old code, the function that creates the array gets called only once, so I'm removing it and just creating the array in the function that called it. Any help would be appreciated: old Code/ function _gwr_custom_path_location_map(){ return array( 'concord' => 45, 'dells' => 38, 'williamsburg' => 54, 'sandusky' => 52, ); } my attempt. First I tried assigning values and it didn't work, so this try concatinates the variable/ $option = array(); $result = db_query(" select n.nid, n.title, c.field_custom_url_value from node n INNER JOIN content_type_location c ON n.nid = c.nid where n.status and n.type = 'location' order by n.title "); $path_location_map = array(); while($row = db_fetch_array($result)){ $path_location_map."'". $row['field_custom_url_value']."'". " => ". $row['nid'].","; } Link to comment https://forums.phpfreaks.com/topic/124481-dynamically-creating-an-array/ Share on other sites More sharing options...
dave_sticky Posted September 16, 2008 Share Posted September 16, 2008 Hi James, Interesting problem! Got me thinking. What you need, is the lovely square brackets to add things onto an array. Something like $path_location_map[] = array($row['field_custom_url_value'] => $row['nid']); In your while loop should do the job. Edit: D'oh. Doesn't work properly. I'll keep thinking. Are you sure you want an associative array? It would be so much easier if you just wanted an index one (numbers to reference the values within it rather than text) Link to comment https://forums.phpfreaks.com/topic/124481-dynamically-creating-an-array/#findComment-642831 Share on other sites More sharing options...
dave_sticky Posted September 16, 2008 Share Posted September 16, 2008 D'oh. Sorry, ignore my last message. Doesn't work properly. I'll keep thinking. Are you sure you want an associative array? It would be so much easier if you just wanted an index one (numbers to reference the values within it rather than text) Link to comment https://forums.phpfreaks.com/topic/124481-dynamically-creating-an-array/#findComment-642833 Share on other sites More sharing options...
james2008 Posted September 16, 2008 Author Share Posted September 16, 2008 Thanks for getting back to me. I would prefer just to use an indexed array, but the problem is that the old site I'm trying to clean up is huge and this array gets referenced all over. For now, I'm kind of stuck using an associative array. Once I can get this thing working dynamically, I would like to go back and try and replace the associative arrays. Thanks Link to comment https://forums.phpfreaks.com/topic/124481-dynamically-creating-an-array/#findComment-642836 Share on other sites More sharing options...
dave_sticky Posted September 16, 2008 Share Posted September 16, 2008 Oh, well if you want to use indexed arrays anyway, then why not just do: $path_location_map[] = $row['nid']; in your loop? Edit: Sorry. Just re-read your post. Been a long day. What you do is: $path_location_map[$row['field_custom_url_value']] = $row['nid']; I just tried this in a test script: <?PHP $array = array("v0" => "Hello"); $v1 = "World"; $v2 = "Foo"; $v3 = "Bar"; $array["v1"] = $v1; $array["v2"] = $v2; $array["v3"] = $v3; print_r($array); ?> And it works like that. Link to comment https://forums.phpfreaks.com/topic/124481-dynamically-creating-an-array/#findComment-642837 Share on other sites More sharing options...
james2008 Posted September 16, 2008 Author Share Posted September 16, 2008 Thanks, I will give that I try and post my final code Link to comment https://forums.phpfreaks.com/topic/124481-dynamically-creating-an-array/#findComment-642840 Share on other sites More sharing options...
james2008 Posted September 16, 2008 Author Share Posted September 16, 2008 Thanks, I will give that I try and post my final code: That did the trick: $option = array(); $result = db_query(" select n.nid, n.title, c.field_custom_url_value from node n INNER JOIN content_type_location c ON n.nid = c.nid where n.status and n.type = 'location' order by n.title "); while($row = db_fetch_array($result)){ $option[$row['field_custom_url_value']] = $row['nid']; } Link to comment https://forums.phpfreaks.com/topic/124481-dynamically-creating-an-array/#findComment-642900 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.