Problem with arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
InternetX
Forum Newbie
Posts: 22
Joined: Tue Jan 21, 2003 1:57 pm
Location: Germany
Contact:

Problem with arrays

Post by InternetX »

Hello,

I have write a script with this line:

array_push($array, "$saved[0]"=>"$username");

but when I open the script in a browser an error comes:

Parse error: parse error, unexpected T_DOUBLE_ARROW in /home/www/web174/html/Toplist.php on line 14

How can I remove this error?
This line is like $array = array("1"=>"test", "2"=>"admin");...
I hope you can help me...

Sorry for my english... I'm 14 and from Germany =/
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post by lazy_yogi »

not sure what your'e trying to do with this:

Code: Select all

array_push($array, "$savedї0]"=>"$username");
But array_push puts an element (or elements) at the END of an array
An example from http://www.php.net :

Code: Select all

$stack = array ("orange", "banana");
array_push ($stack, "mango");
array_push ($stack, "apple", "raspberry");
 
This example would result in $stack having the following elements: Array
(
    ї0] => orange
    ї1] => banana
    ї2] => mango
    ї3] => apple
    ї4] => raspberry
)

hope this helps
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

arrays in php are a mixture between perl's arrays and hashes.
They hold key/value pairs.
If you create an array like

Code: Select all

<?php $arr = array('a', 'b', 'c'); ?>
php assigns the key 0,1,2 to the values, but you may override this behavior, e.g.

Code: Select all

<?php $arr = array('i'=>'a', 'ii'=>'b', 'iii'=>'c'); ?>
which leades to $arr['ii']=='b'.
You can append elements to an array e.g. with an empty element-selector

Code: Select all

<?php // this will create an array that equals the first example
$arr = array('a');
$arr[] = 'b';
$arr[] = 'c';
?>
But you may also add an specific element, e.g.

Code: Select all

<?php // this will create the same array as the second example
$arr = array('i'=>'a');
$arr['ii'] = 'b';
$arr['iii'] = 'c';
The key of course can be contained in a variable

Code: Select all

$arr = array();
$key = 'i';
$arr[$key] = 'a';
$key .='i';
$arr[$key] = 'b';
$key .='i';
$arr[$key] = 'c'; // will also produce the second array
?>
and it can also be an array-elelemt

Code: Select all

<?php
$saved = array('i', 'ii', 'iii');
$arr = array();
$arr[$second[0]] = 'a';
$arr[$second[1]] = 'b';
$arr[$second[2]] = 'c'; // again the second array
?>
So, maybe you want this

Code: Select all

$array[$saved[0]]=$username;
InternetX
Forum Newbie
Posts: 22
Joined: Tue Jan 21, 2003 1:57 pm
Location: Germany
Contact:

Post by InternetX »

Thanks for the help...
I've finnished the script correctly :)
Post Reply