premiso Posted November 9, 2008 Share Posted November 9, 2008 Hello, I have been coding in PHP4 for who knows how long and I am trying to convert some old code to PHP5 and I am running into an issue with associative arrays. I am not sure if there is a variable in the .ini that I can change to allow this but here is what I want. In php4 you could have an array as such: <?php $myArray = array("test" => 1, "test2" => 2); ?> And to access an element of that array you could do either of the following: <?php $myArray = array("test" => 1, "test2" => 2); echo $myArray['test']; // should print 1 echo $myArray[0]; // should also print 1. ?> I am not sure if this is impossible to do now or just a value in the php.ini that I need to change, any help or insight is appreciated. Thanks. Link to comment https://forums.phpfreaks.com/topic/132058-solved-php5-and-arrays/ Share on other sites More sharing options...
trq Posted November 9, 2008 Share Posted November 9, 2008 Arrays are no different in php4 - 5. The functionality you describe does not actually exist in either. Your array is either associative or numeric, not both. Link to comment https://forums.phpfreaks.com/topic/132058-solved-php5-and-arrays/#findComment-686226 Share on other sites More sharing options...
premiso Posted November 9, 2008 Author Share Posted November 9, 2008 hmm, maybe my mind was just making that up then. I could have sworn I used both but I would understand why it should be that way, thanks for the response Thorpe! Link to comment https://forums.phpfreaks.com/topic/132058-solved-php5-and-arrays/#findComment-686234 Share on other sites More sharing options...
trq Posted November 9, 2008 Share Posted November 9, 2008 Maybe you your where thinking about mysql_fetch_array which will return an array with both an associative and numerical index. Link to comment https://forums.phpfreaks.com/topic/132058-solved-php5-and-arrays/#findComment-686236 Share on other sites More sharing options...
premiso Posted November 9, 2008 Author Share Posted November 9, 2008 You know, that is exactly what I was thinking of. I was using that when pulling information out of a database. Silly me for not thinking of that myself. I will correct the issue as this is an issue from pulling data out of a db using the mysql_fetch_assoc() Thanks again for that info Thrope, saved me alot of time and headaches! Link to comment https://forums.phpfreaks.com/topic/132058-solved-php5-and-arrays/#findComment-686239 Share on other sites More sharing options...
trq Posted November 9, 2008 Share Posted November 9, 2008 Your probably better of sticking with mysql_fetch_assoc or at least asking mysql_fetch_array for only one or the other (see the second arg). Otherwise, it makes all your arrays twice the size. eg; Instead of.... array 'foo' => 'bar'; You get... array 0 => 'bar', 'foo' => 'bar' Its no biggie, but its also not usually needed. Link to comment https://forums.phpfreaks.com/topic/132058-solved-php5-and-arrays/#findComment-686242 Share on other sites More sharing options...
premiso Posted November 9, 2008 Author Share Posted November 9, 2008 Yea I was looking at my code and realized that would be too much information to store in the array and pass around in sessions. I would rather use the associative to store the data, and when I just pull out the one column use the indexed to get that so I do not have to parse the sql statement. Thanks. Link to comment https://forums.phpfreaks.com/topic/132058-solved-php5-and-arrays/#findComment-686250 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.