Jump to content

[SOLVED] PHP5 and Arrays


premiso

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.