...

View Full Version : php variable array



thesavior
01-01-2007, 04:43 AM
How can i do something like this:

$ranks[$rank]

I have a populated $ranks array that looks like this:



$ranks = array(
'Recruit' => '1',
'Private' => '2',
'Private 1st Class' => '3',
'Gunner Silver' => '4',
'Gunner Gold' => '5',
'Corporal Silver' => '6',
'Corporal Gold' => '7',
'Lance Corporal Silver' => '8',
'Lance Corporal Gold' => '9',
'Master Corporal Silver' => '10',
'Master Corporal Gold' => '11',
'Sergeant Silver' => '12'
);


Then $rank is supposed to be one of the values on the left hand side of the above array. I want the number attributed to the text returned in $rank. Any ideas?

firepages
01-01-2007, 05:02 AM
<?
$rank = 'Recruit';
echo $ranks[$rank];
?>


are you saying that the above does not work ?

thesavior
01-01-2007, 05:07 AM
Im thinking the issue is the way i included it into my script. Here is the line im getting the error on:


$vbulletin->db->query("INSERT INTO assimxnamerank (pid,
name, rank) VALUES (".$value.",".$nickname.",".$ranks[$rank].")");


Parse error: parse error, unexpected T_STRING

and here are the surrounding lines:


$num_rows = mysql_num_rows($result);
if ($num_rows == 0)
{
$vbulletin->db->query("INSERT INTO assimxnamerank (pid,
name, rank) VALUES (".$value.",".$nickname.",".$ranks[$rank].")");
}
else
{
$vbulletin->db->query("UPDATE assimxnamerank SET
`name`='".$nickname."',`rank`='".$ranks[$rank]."' WHERE
`pid`='".$value."'");

}


*EDIT*
When I change


$vbulletin->db->query("INSERT INTO assimxnamerank (pid,
name, rank) VALUES (".$value.",".$nickname.",".$ranks[$rank].")");
to

echo $ranks[$rank];

I get the same error, so the error has to be on the lines above it, but I dont see it.

vinyl-junkie
01-01-2007, 06:02 AM
I'm not in a position to test it right now, but I think the following will work:


$vbulletin->db->query("INSERT INTO assimxnamerank
(pid, name, rank)
VALUES (\"$value\",\"$nickname\",\"$ranks[$rank]\")");

thesavior
01-01-2007, 07:27 PM
thanks alot, that did it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum