Corjava
Corjava

Reputation: 340

PHP Array Looping & Exploding

I'm not very experienced in using PHP, but I'm having a hard time thinking of a way to do this. I'm reading from a file and exploding on ":" as you can see here.

<?php
$datainfo = file('data.txt');
for($i = 0; $i < count($datainfo); $i++){
$expdata = explode(':', $datainfo[$i]);
}

?>

The issue is that I need to reference specific indexes of the resulted explosion like this.

<p> <?php echo $expdata[1] ?> </p>

I'm getting back an array of the last line inside the data.txt file. I know why It's happening, I just don't know how to get what I want here. (Sorry Very New). Data.txt contain the following.

name:Octopod Juice Stand
balance:20
price:0.5
customers:12
starting:2014-05-26
end: 2014-09-01
juice:15.25
fruit:10

Upvotes: 0

Views: 52

Answers (1)

Nathan Dawson
Nathan Dawson

Reputation: 19308

Change your code to

<?php
$datainfo = file('data.txt');
$expdata = array();

for($i = 0; $i < count($datainfo); $i++){
    $expdata[] = explode(':', $datainfo[$i]);
}

?>

And then to get the first label.

<p><?php echo $expdata[0][0]; ?></p>

Or the first value

<p><?php echo $expdata[0][1]; ?></p>

Upvotes: 1

Related Questions