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. Quote 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. Quote 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! Quote 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. Quote 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! Quote 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. Quote 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. Quote Link to comment https://forums.phpfreaks.com/topic/132058-solved-php5-and-arrays/#findComment-686250 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.