php - Combine post values and remove empty -
i have 2 sets of arrays coming $_post
. keys both numeric , count same, since come in pairs names , numbers:
$_post[names] ( [0] => first [1] => second [2] => [3] => fourth ) $_post[numbers] ( [0] => 10 [1] => [2] => 3 [3] => 3 )
now need combine two, remove each entry either values missing.
the result should like:
$finalarray ( [first] => 10 [fourth] => 3 )
post data dynamically created there might different values missing based on user input.
i tried doing like:
if (array_key_exists('names', $_post)) { $names = array_filter($_post['names']); $numbers = array_filter($_post['numbers']); if($names , $numbers) { $final = array_combine($names, $numbers); } }
but can't seem filter correctly, since giving me error:
warning: array_combine(): both parameters should have equal number of elements
how using array_filter
array_filter_use_both
flag on?
<?php $array1 = [ 0 => "first", 1 => "second", 2 => "", 3 => "fourth", ]; $array2 = [ 0 => 10, 1 => "", 2 => 3, 3 => 3, ]; var_dump(array_filter(array_combine($array1, $array2), function($value, $key) { return $key == "" || $value == "" ? false : $value; }, array_filter_use_both )); /* output: array(2) { ["first"]=> int(10) ["fourth"]=> int(3) } */
Comments
Post a Comment