Page 1 of 1

Access members of array

Posted: Tue Nov 21, 2006 12:22 pm
by noon
Hi,

I'm trying to work with a line of text that comes in the format:

Code: Select all

13:14 692 405 3 339 29619 28 66 7426 5677 405 269 299 405 393 27 29631 714
Right now, what I was doing inside a for loop to rip out the first 5 digits (time) where $result is the string in the format mentioned above:

Code: Select all

for ($i=0; $i <= 4; $i++)
{
	$time = $time . $result[$i];
}
Wondering if there is a better way to do this... because at the way i am doing it now, im going to have to have a for loop for each value (values separated by spaces). Is there a way to read until space, and then increment the variable appropriately?

thanks,

noon

Posted: Tue Nov 21, 2006 12:29 pm
by John Cartwright
Kind of hackish, but a combination of substr and explode should do the trick.

Code: Select all

$result = '13:14 692 405 3 339 29619 28 66 7426 5677 405 269 299 405 393 27 29631 714';

$time = substr($result, 0, 5);
$ints = substr($result, 6, strlen($result));
$ints = explode(' ', $ints);

echo 'Time: '. $time;

echo '<pre>';
print_r($ints);

Posted: Tue Nov 21, 2006 12:36 pm
by noon
Heh, I just found the explode function.

Couldn't I just use:

Code: Select all

$weather = explode(' ', $result);
Space being the delimiter, and $result[0] being time, and etc.?

Posted: Tue Nov 21, 2006 12:39 pm
by John Cartwright
noon wrote:Heh, I just found the explode function.

Couldn't I just use:

Code: Select all

$weather = explode(' ', $result);
Space being the delimiter, and $result[0] being time, and etc.?
yes :oops:, sometimes I may over-complicate things.