Access members of array

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
noon
Forum Newbie
Posts: 5
Joined: Tue Nov 21, 2006 12:16 pm

Access members of array

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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);
noon
Forum Newbie
Posts: 5
Joined: Tue Nov 21, 2006 12:16 pm

Post 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.?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
Post Reply