Jump to content


Multidimensional Array Problem...


2 replies to this topic

#1 Pax

    P2L Jedi

  • Members
  • PipPipPip
  • 911 posts
  • Gender:Male
  • Location:Windsor, ON, Canada

Posted 11 January 2006 - 12:37 AM

Hi, I'm in the midst of using php to parse a text file, and thus far, all is going well. The problem that I am running in to now is that when I try to put the variables into an array to recall later, it only seems to have put the last item into the array.

$i=0;
while($i < $mech_weapon_num){
$count = $i + 32;

$splitstring = split(",", $mechstats[$count], 2);
$wepsplitstring = split(" ", $splitstring[0], 2);

$mech_weapon_count = $wepsplitstring[0];
$mech_weapon = $wepsplitstring[1];
$mech_weapon_loc = $splitstring[1];
echo $i;
echo $mech_weapon_count."<br />";
echo $mech_weapon."<br />";
echo $mech_weapon_loc."<br />";
echo "<br />";

$mech_wep_info = array($i => array($mech_weapon_count, $mech_weapon, $mech_weapon_loc));

$i++;
}
echo $i;
echo $mech_weapon_count."<br />";
echo $mech_weapon."<br />";
echo $mech_weapon_loc."<br />";
echo "<br />";

outputs:
0 1
CLFlamer
Center Torso

1 1
CLHeavyLargeLaser
Right Arm

2 1
CLHeavyLargeLaser
Left Arm

But my array only consists of:
$counter=0;
while($counter < $mech_weapon_num){
echo $counter;
echo $mech_wep_info[$counter][0]." ".$mech_wep_info[$counter][1]." ".$mech_wep_info[$counter][2]."<br />";

$counter++;
}
0
1
2 1 CLHeavyLargeLaser Left Arm

Array ( [2] => Array ( [0] => 1 [1] => CLHeavyLargeLaser [2] => Left Arm ) ) <- that is the contents of the whole array...

Sorry for the length of this post...any help would really be appreciated! I have a couple of group members at school who are relying on me to get this done. Thanks all.

-Pax

#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 11 January 2006 - 04:52 PM

// Change
$mech_wep_info = array($i => array($mech_weapon_count, $mech_weapon, $mech_weapon_loc));

// To
$mech_wep_info[$i] = array($mech_weapon_count, $mech_weapon, $mech_weapon_loc);
The way it is currently resets the variable every time you run through the loop. After you change it, it will keep the previous entries, and add the new ones as expected.

#3 Pax

    P2L Jedi

  • Members
  • PipPipPip
  • 911 posts
  • Gender:Male
  • Location:Windsor, ON, Canada

Posted 11 January 2006 - 10:21 PM

Excellent! Thanks rc69, works perfectly now!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users