Jump to content

Recommended Posts

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

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!

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.