0

I don't know if this is even called "hardcoding", but I would like to build an array (for example from a mysqli_fetch_assoc query) and output it for reuse in a config.php file.

So this output from mysql would be:

[structure] => Array
    (
        [0] => Array
            (
                [structureid] => 23
                [active] => 1
            )

        [1] => Array
            (
                [structureid] => 25
                [active] => 1
            )

And it would be stored like this in a config.php file:

$structure[0]['structureid'] = 23;
$structure[0]['active'] = 1;
$structure[1]['structureid'] = 25;
$structure[1]['active'] = 1;

Is there an easy and quick way to accomplish this?

As someone has pointed out, I could resolve this with using a json file - which I'm already doing - but I wanted to check if there would be another (quick) way of doing it as described above.

Taapo
  • 1,785
  • 4
  • 18
  • 35
  • why you don't use `foreach` loop? – Saeed M. Jan 10 '16 at 20:17
  • as its stored in the db, why not just querry that when required? –  Jan 10 '16 at 20:17
  • Possible duplicate of [How do I store an array in a file to access as an array later with PHP?](http://stackoverflow.com/questions/2662268/how-do-i-store-an-array-in-a-file-to-access-as-an-array-later-with-php) – some-non-descript-user Jan 10 '16 at 20:18
  • @Dagon: for this specific case it needs to be exported to run without a database - I have a solution through json, but I wanted to know if there was a another way **without** going through a json file. – Taapo Jan 10 '16 at 20:19
  • U can store in cookies if its static – devpro Jan 10 '16 at 20:23
  • quick and dirty write the serialized version. –  Jan 10 '16 at 20:24

2 Answers2

0

You can use var_export(), to create a readable (and in-code-reusable) array, that you can then put in a config file:

$reusableCode = var_export($yourArray, true);

Or to just output it:

var_export($yourArray);

You can put that in a config file with an return, like this:

return array(
    'your' => 'values'
);

and then require it from your code:

$configData = require('yourConfigFileWithTheArray.php');
ArSeN
  • 5,133
  • 3
  • 19
  • 26
  • I wish this would work - since it would be the best answer I found, but trying to do it this way, and then using get_defined_vars() to display them - doesn't work. Is there something I'm overlooking? – Taapo Jan 11 '16 at 11:51
  • @Taapo Well, how exactly are you including it and did you remember to add the `return` in the file? Is error reporting on? – ArSeN Jan 11 '16 at 16:22
  • I was wrong. I forgot to create a variable to read the array into. It works now - very helpful. – Taapo Jan 11 '16 at 18:06
0

I Initially misread your question,

So you want to store mysql resource to a file?

You mentionned that you are using JSON is to solve this.

This is a good way to do it, the other way would be to serialize/deserialize the object than save them to a file. JSON solution is simple human readable.

Example:

$data = serialize($rows);
file_put_contents('data.txt', $data);

$data = file_get_contents('data.txt');
$rows = unserialize($data);

But you won't be able to read the output of the serialization.

meda
  • 45,103
  • 14
  • 92
  • 122