2

For example, a PHP file contains this:

<?php

return [
    'sample_array' => [
        'sample_index' => 'sample_data',
    ],
];

I would like to append values to sample_array using a PHP script. What would be the best way to do this?

Edit: After running the script, file should be updated to:

<?php

return [
    'sample_array' => [
        'sample_index' => 'sample_data',
        'sample_index2' => 'sample_data2',
    ],
];

Edit: What I'm looking for is the creation of a script that will directly update the contents of the PHP file to append the array.

6
  • If you just want add value (with a new key) just $sample_array['sample_index2'] = 'sample data2' Commented May 2, 2020 at 7:13
  • You want to fetch the contents of the PHP-file? What exactly what you mean by the contents of the PHP-file? All the code? Commented May 2, 2020 at 7:25
  • The file resides locally. What I would like to do is run a script that will update the contents of the file. What I mean by "update the contents of the file" is that the sample_array should be appended with additional data (which would be hardcoded in the script) after running the script. Commented May 2, 2020 at 7:30
  • You want to fetch the array from file1(php) and add som data to that array in file2(php) you mean? Commented May 2, 2020 at 7:31
  • Do you want to update file1 from file2 with the new extended array? Commented May 2, 2020 at 7:32

2 Answers 2

5

You can create valid PHP code through the use of var_export(). You can then store the code in a file with something like file_put_contents().

// load the array from the file
$arr = include 'file.php';

// modify the array
$arr['sample_array']['sample_index2'] = 'sample data 2';

// write it back to the file
file_put_contents("file.php", "return " . var_export($arr, true) . ";");

gives you...

return array (
  'sample_array' =>
  array (
    'sample_index' => 'sample_data',
    'sample_index2' => 'sample data 2',
  ),
);
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your answer but this is not what I'm looking for. Please see my latest edit.
May be worth adding the <?php to the start of the file generated.
@Pardo - I'm glad you got your answer, but wouldn't this be in a second file then? (file2) I'm just curious, I had a hard trying to understand what you were looking for.
Hi @bestprogrammerintheworld, sorry if I didn't make it more understandable. The script given by @Sherif would update the file (file.php) directly after execution. There is no creation of another file.
Aha @Pardo - no problem. Som misunderstanding in communication only. As I said. I'm glad you got help. That's the important thing!
0

I think you might be after array_push

<?php
$sample_array = [
  'sample_index' => 'sample_data',
];

array_push($sample_array, array(
  'sample_index2' => 'sample_data2',
));

return $sample_array;

With array push, the second argument could be another array that you have already defined with multiple values and keys

More info at php.net array_push

2 Comments

Thank you for your answer but this is not what I'm looking for. Please see my latest edit.
It's possible it might be better then to store it as JSON instead, as PHP and JS can both encode and parse your content into a JSON array and write it to a file

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.