array_push not working from within function

Hi

I am using the following code:

$productQty = array_combine(($_POST['products']), ($_POST['items_to_add']));
$length = count($productQty);


$selectedProductIDs = array();

function checkQty($item1, $key, $array){
for($i = 0; $i<$length; $i++){
	if($item1[i] >0)
	{
	array_push($array, $key);
	}
	}
}

checkQty($productQty, key($productQty), $selectedProductIDs);

print_r($selectedProductIDs);

The purpose of this code is to select values of the array $productQty which are greater than 0, then add their keys to the array $selectedProductIDs (which are effectively the product_ids to searched for via a MySQL WHERE IN query).

However when I run this code, all that is printing is a blank array Array(). I’ve tried using array_walk instead of the for loop with the same effect.

Please help.

Have you looked at [fphp]array_filter/fphp ?

<?php
$array = array(
  1 => 45,
  3 => 0,
  5 => 1,
  9 => 45,
  67 => null,
  'foo' => 0,
  'bar' => 56
);

$array = array_filter(
  $array,
  create_function(
    '$element',
    'return 0 < $element;'
  )
);

print_r(
  $array
);

/*
  Array
  (
      [1] => 45
      [5] => 1
      [9] => 45
      [bar] => 56
  )
*/

That seems a more elegant solution. I’ll give it a try. Thanks!

So much code for something so trivial, there’s no need for so many array functions, let alone for create_function.

$productQty = array_combine(($_POST['products']), ($_POST['items_to_add']));

foreach($productQty as $key => $value)
{
    if($value > 0) $products[] = $value;
}

if(is_array($products))
{
    print_r(array_keys($products));
    // or 
    echo implode(',', $products); // so you can put it within mysql's IN() statement
}

Thanks, it worked fine, here is the final code used. There are probably much easier ways of achieving the same effect of storing product ids and associated purchase quantity, using controller/view relationship but I had to work with legacy javascript and form from a previous developers work.

$productQty = array_combine(($_POST['products']), ($_POST['items_to_add']));
$length = count($productQty);



$selectedProducts = array_filter(
  $productQty,
  create_function('$element','return 0 < $element;')
);

$selectedProductsArray = array_merge(array_keys($selectedProducts));
//print_r($selectedProductsArray);
$selectedProductIDs = "'" . implode("','", $selectedProductsArray) . "'";

Your function isn’t returning anything.
What you’re doing is filling an array and that’s it.

Solution 1: return the newly constructed array from the function.
Solution 2: pass the $array parameter as reference.

I’m not posting modified code since implementation is rather trivial and it’s a good practice to do it yourself after being pointed in right direction.

Strangely if I replace the array_push with an echo of the $key, it displays fine, but the $array is not being written to $selectedProductIDs.

I am trying with this code but it gives the same result:

$selectedProductIDs = array();

function checkQty($item1, $key, $array){
	if($item1 >0)
	{
	array_push($array, $key);
	}
	return $array;
}


array_walk($productQty, 'checkQty', $selectedProductIDs);

print_r($selectedProductIDs);