Jump to content

php flat file element array handling


devouk

Recommended Posts

hi

 

I been working on a little snippet of code <about the level of my skill>

 

The idea i trying to work out is

 

I have a text file which i open, it has the heading id,image,link,desc and the value are separated by commas, I then load them in to a multi dimension array

 

Create a random number and select the appropriate id in the array, then do this again and make sure that they are not the same, if that is ok then load the results of each element in to variable so i can then manipulate in my code

 

I been playing with bits of code and so far got this

<?php

// the HTML source of a URL.

$lines = file('textfile.txt');

 

//total lines available

$limit = count($lines);

 

//get the keys/headers

$headers = explode(",",$lines[0]);

 

//to store the data

$ListItems = array();

 

// Loop through array to extract data

for($i=1; $i < $limit; ++$i)

{

//extract the values

$values = explode(",",$lines[$i]);

 

//create temporary array

$temp = array();

 

//store the values

for( $j=0, $inner_limit=count($values); $j < $inner_limit; ++$j)

{

$temp[ $headers[$j] ] = $values[$j];

}

 

//append to "master array"

$ListItems[]=$temp;

}

  // count how many rows are in the ray

  $NoListElements=count($ListItems)-1;

 

  echo "Elements " .$NoListElements. "<br />";

//reset random nos

  $randomID1=0;

$randomID2=0;

 

// get random number 1

  $randomID1 = mt_rand(1,$NoListElements);

 

// testing to ensure get 2nd random number which is not the same

while($randomID2 == 0)

{

$randomID2 = mt_rand(1,$NoListElements);

if ($randomID2 == $randomID1) $randomID2 = 0 ;

}

 

// Display values

echo "Random 1 =" .$randomID1;

echo "<br/>";

echo "Random 2 =" .$randomID2;

echo "<br/>";

 

//Need to get the row element for random id 1 from ListItems

 

<help needed here>

 

//Need to get the row element for random id 2 from ListItems

 

<help needed here>

 

 

but i not too sure how to get any further

 

if you can explain how i would put these in to a variable and also how if i can just access them direct to put in to my html code .. ie echo i presume !!!

 

thanks for the missing links

 

Link to comment
https://forums.phpfreaks.com/topic/166675-php-flat-file-element-array-handling/
Share on other sites

Have you tried $ListItems[0][1]?

 

At a glance, $ListItems[] holds several arrays. Arrays always begin at `0` and move forward. If you KNOW what the key is going to be you can just list them as such.

 

$ListItems[0][0] would be the first item of the first array

$ListItems[1][0] would be the first item of the second array

$ListItems[0][1] would be the second item of the first array

 

...and so on.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.