I want to save certain values from $_POST
into a file. But I only want values where the key is in the array $lang
.
As an example:
$_POST = [1 => "a", 2 => "b", 3 => "c"];
$lang = [2, 3];
With this input I would only want the values from $_POST
where the key is in the $lang
array.
Expected output would be:
[2 => "b", 3 => "c"]
Right now I'm trying to archive this using ArrayIterator
and MultipleIterator
, but this loops through both arrays:
$post = new ArrayIterator($_POST);
$lang_array = new ArrayIterator($lang);
$it = new MultipleIterator;
$it->attachIterator($post);
$it->attachIterator($lang_array);
$fh = fopen('name.php', 'w');
foreach($it as $e) {
fwrite($fh , $e[1] .'-' . $e[0] );
fwrite($fh ,"\n" );
}
So I'm a bit stuck how to solve this problem?
Try this :
// Combining both arrays into one.
$combined_array = array_merge($_POST, $lang);
$fh = fopen('name.php', 'w');
foreach($combined_array as $key => $value){
fwrite($fh , $key .'-' . $value );
fwrite($fh ,"\n" );
}