0

I have an array in my script like this

$acceptedID = array ('su', 'root');

I thought that eventually it would be hard to add more and more as this array would get really long. So I wondered if it was possible to have an external file like this

su
root
asdf
etc.

and have it read in the script as

$acceptedID = array ('su', 'root', 'asdf', 'etc.'); 

and have that keep changing as the external text file changes. Is there any way to do this? Also after the array is there, it has to run and if statement to see if it is in an array.

if (in_array($id, $acceptedID))
        return 'YES';
    else
        return 'NO';    
3
  • are these 4 files in 1 folder, or 4 lines in 1 file ? Commented Mar 9, 2014 at 15:04
  • $acceptedID = file('myList.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); Commented Mar 9, 2014 at 15:04
  • @MohammadAbuShady 4 lines in 1 file Commented Mar 9, 2014 at 15:23

3 Answers 3

2

You can just keep your array in an external file and include it where you want it:

//commands.php

// This file just contains a return statement which returns the the array
// with the commands

return array(

    'su',
    'root',
    'other',

);

//yourscript.php

// You can include your array anywhere you want
$validIds = include('commands.php');  
Sign up to request clarification or add additional context in comments.

Comments

0

http://uk.php.net/manual/en/function.file.php

file — Reads entire file into an array

Comments

0

Use this way, file does it:

$acceptedID = file("myList.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

1 Comment

I get this in_array() expects parameter 2 to be array, boolean given in script.php on line 5 when trying to run the if statement

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.