View Full Version : display array value only if present

11-06-2007, 01:05 PM
Hello, I'm stuck on an array problem. I have the following array of session vars from a user form:

$quantity = array(1 => $_SESSION['a2dfam'], $_SESSION['a2dind'], $_SESSION['a2dstu'], $_SESSION['a1dfam'], $_SESSION['a1dind'], $_SESSION['a1dstu']

Note that not all of these values will be set (e.g., if the user didn't need to order something from each category). Therefore, on a verification page, I want to display totals only for the items that a user wants. You'll notice that the array values are in sets of 3 (family, individual, student).

Is it possible to loop through a set of 3 specific values in this array, determine whether ANY of those 3 are set, and if so, output specific code? I can't figure out how to search a subset of an array, and execute code if any of those values are present.



11-06-2007, 01:11 PM
in_array (http://php.net/in_array)

11-06-2007, 01:27 PM
I can't figure out how in_array would work in this case. Because these array values came from a user form, they are all "set". However, I only want the non-blank values.

Please let me know if there's an easier way, but I'm trying to do the following:

function checkarr($start, $end){
for ($i=$start; $i<=$end; $i+=1) {
if ($quantity[$i]!="") {
return $c;

if (checkarr(1,3) > 0) {code to execute}