Reputation: 182
I'm trying to merge two arrays into one (PHP), except in the new array I only want to display values from the combination of both arrays where $b > 0.
I've used array_filter
to get the values where $b > 0
, but now I need help combining the arrays together and returning a final array which I will then insert into a database.
For example if $a = [0] => 351 [1] => 352 [2] => 353
and $b = [2] => 3 //array_filter has removed [0] => 0 [1] => 0
I would want the new array ($c) to be [0] => 353,3 using the following code:
print_r($a = $_POST['price']);
print_r($b = array_filter($_POST['qty']));
$count = count($a);
$i = 0;
while ($i < $count)
{
$c = $a[$i] . "," . $b[$i];
$i++;
}
foreach ($_POST as $key => $value)
{
print_r ($c);
}
However at present my result is this:
Array //$a
(
[0] => 351
[1] => 352
[2] => 353
)
Array //$b
(
[2] => 1
)
353,1353,1353,1 //$c
Upvotes: 0
Views: 64
Reputation: 931
You mean something like this?
$a = array(351, 352, 353);
$b = array(2 => 3);
$c = array();
foreach($b as $key => $value)
{
$c[] = $a[$key];
$c[] = $b[$key];
}
print_r($c);
This will give you:
Array ( [0] => 353 [1] => 3 )
And if you want to have the array $c as comma seperated string use:
$c = implode(',', $c);
This will give you:
'353,3'
Upvotes: 3