array function?

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
deras
Forum Newbie
Posts: 24
Joined: Sun Nov 02, 2003 10:26 am

array function?

Post by deras »

i have two variables ($sums1, $sums2) which have nine values in them(seperated by ":") 8:2:12:23:12:23:33:32:23

i wanted to get each of those values seperated into nine seperate variables

i tried this

Code: Select all

for ($i=0; $i<=8; $i++)
{
$sums1[$i]=split(":",$sums1);
$sums2[$i]=split(":",$sums2);
 }
but that did not seem to work. any ideas on how to do this properly?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

A few ways to do this, here's one:

Code: Select all

<?php
$sums1 = '8:2:12:23:12:23:33:32:23';
$sums2 = '7:3:12:3:17:20:33:11:33';
for($x=1;$x<3;$x++){
  $sum = 'sums'.$x;
  $sum_array = 'sums'.$x.'_array';
  foreach(explode(':', $$sum) as $val){
    ${$sum_array}[] = $val;
  }
}
echo '<pre>';
var_dump($sums1_array);
var_dump($sums2_array);
echo '</pre>';
?>
deras
Forum Newbie
Posts: 24
Joined: Sun Nov 02, 2003 10:26 am

Post by deras »

thanks...
Post Reply