0

I have a file that has some values inside, an ordinary txt file, but I have made it look like an array

tekstovi.php

That looks like this

   '1'    => 'First',
   '2'    => 'Second',
   '3'    => 'Third',

Or someone have a better solution for file look :) From that file I want to make a new array $promenjive in scope, so what is the best way to make an array from a file in php? It is easy to make explode in an ordinary array, but I don't know how to put all that in a multidimensional array :)

6 Answers 6

4

why don't you just make a .php file which will return an array, something like:

// somefile.php
return array(
   'key1' => 'value1',
   'key2' => 'value2'
);

and when you need it:

$something = require('/path/to/somefile.php');
echo $something['key1'];
Sign up to request clarification or add additional context in comments.

3 Comments

Note require is a language construct and parentheses are not needed around its argument.
@Orangepill, agreed if this file is only read by PHP. Otherwise, something like JSON would be more flexible.
@JasonMcCreary And from a performance standpoint it pretty much beats the pants off of every other serialization method.
3

Or someone have a better soluton for [the] file [format]

Create an INI file and use parse_ini_file().

If it's just an array only read by PHP, simply use include/require.

Comments

0

Have the file store the values in some sort of data-storing file type, e.g. xml, json, csv

PHP numerous ways of parsing and retrieving that data meaningfully. My recommendation is to use json, so with your example above it would be:

{"1":"First","2":"Second","3":"Third"}

And the corresponding PHP method to decode it:

json_decode($contents, true)

Check out the http://php.net/manual/en/function.json-decode.php for the method's full documentation, note that I set the 2nd optional argument to true since you're parsing an associative array, otherwise it will convert the data into an Object

Comments

0

Use JSON:

{
  "foo" : "bar",
  "baz" : [2, 3, 5, 7, 11]
}

PHP:

$content = file_get_contents('path/to/file');
$array =  json_decode($content, true);

Et voila'.

1 Comment

@Populus yes, I have edited with the true parameter - thank you!
0

parse_ini_file is a good solution with for a 1 or 2 dimensional array but if you need a more deeply nested structure you can use json.

 $config = json_decode(file_get_contents($configFile)); // return an object, add true as a second parameter for an array instead

with the file looking something like

 {
       "1":"First",
       "2":"Second",
       "3":"Third"
 }

if JSON formatting is not palatable for you then YAML or XML might be an option.

Comments

0

You don't need to do much, you can just create a variable in one file, and after including it will be available in the other.

file1.php:

$x = 5;

file2.php:

include file1.php
echo $x; //echoes 5

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.