Reputation: 399
I have a file containing a two dimensional array and the first term of each nested array is of form id=>2.
I want to extract the array into another file, find the last id in the array, add one to it, create a new array element containing that new id term, add it to the array and then save the edited array back to the original file.
This seemingly simple task has got me beat. I can do the whole thing as long as the processing is done within the original file and I can use file_get_contents() to get the contents of one file into another. But then if I try to use any of the contents I've extracted, eg the array name, php tells me it is undefined.
Grateful if someone could give me a pointer on how to do this.
Upvotes: 0
Views: 74
Reputation: 70470
include 'thefile.php';
$id = 0;
foreach($thearray as $item){
if($item['item_id'] > $id) $id = $item['item_id'];
}
$id++;
$thearray[] = array('item_id' => $id);
file_put_contents('thefile.php','<?php $thearray = '.var_export($thearray, true).';');
Upvotes: 3