1

I am storing all of my items into lines in a text file like so, with the ID at the start

1/item name/content/etc
2/item name/content/etc
3/item name/content/etc
4/item name/content/etc

This method of storage is for a simple script we are using on a game that is editable by the different users so security really isn't a big deal, or else we would just use mysqli and the like.

I need to be able to print out variable information for a single entry, found by the id at the beginning, and also print say, 3 and 2, in numerical order. So far I am loading them into an array like so

$content = file('script/items.txt');
$items = array($content);

it throws the whole file into an array like this

array(1) {
    [0]=> array(4) {
        [0]=> string(23) "1/item name/content/etc"
        [1]=> string(23) "2/item name/content/etc" 
        [2]=> string(23) "3/item name/content/etc" 
        [3]=> string(23) "4/item name/content/etc"
     }
}

is there an easier way to pick one, or even multiple using just their id so i don't have to store the whole file into the array (it could get big in the future) or is the best way to store them all, loop through the entire array one line at a time, explode them for each /, and them compare is it's in the required items? Thanks.

2
  • Read file line by line. Or move to a database. Commented Mar 16, 2018 at 7:12
  • remove this line $items = array($content); and loop over $content array itself. Best is to move to database Commented Mar 16, 2018 at 7:21

3 Answers 3

1

You need to use file() flags FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES

Do like below:-

$content = file('script/items.txt',FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
print_r($content);

Note:-

Now loop over the array to get records one-by-one.

Best approach to save data to database table with columns like id,item_name,contents and extra

Sign up to request clarification or add additional context in comments.

Comments

0

You can try the below code to read file line by line.

$handle = fopen("script/items.txt", "r");
if ($handle) {
    while (!feof($handle)) {
    $line = fgets($handle)
        // do your stuffs.
    }

    fclose($handle);
} else {
    // error opening the file.
}

It will not load complete file in memory so you can use this for bigger files as well.

feof($handle) It will save you in case you have a line which contains a boolean false.

Comments

0

You could also use array_reduce to store the ID as key into the array.

$content = file('script/items.txt',FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$content = array_reduce($content, function ($result, $line) {
    list($id, $data) = explode('/', $line, 2) ;
    $result[$id] = $data;
    return $result;
}, array());
print_r($content);

Outputs:

Array
(
    [1] => item name/content/etc
    [2] => item name/content/etc
    [3] => item name/content/etc
    [4] => item name/content/etc
)

So you can get elements using without using a loop:

$content[$wanted_id]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.