How do I figure out if an array is populated or not?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

StrangeRanger
Forum Newbie
Posts: 19
Joined: Tue Jan 31, 2006 2:26 pm

Post by StrangeRanger »

It always helps to see what you are talking about, but in your case, you want to output the table name before the loop. Then let the loop run. There are a lot of different solutions for being to do what you want.
Right, but I only want that table name to display if the $details array contains any values to display. That is the point I'm stuck on. I can not find a way to verify if there is any data in the array. I'm betting there is some easy step to do this, but me in my noobness and weariness of trying to learn it can not figure it out. It's a "see the trees through the forest kinda thing" I'm thinkin' :)
j
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

StrangeRanger wrote:Right, but I only want that table name to display if the $details array contains any values to display.

...

I can not find a way to verify if there is any data in the array.
OK, then check the array for values using a loop and set a check var in the loop. If there is even one array value, then set the check var to a value and use that value to run the array loop for displaying.

Code: Select all

<?php
$crnarray = array (116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 66, 72, 219, 107, 203, 212, 88, 215, 216, 201, 91, 111, 69);

if (in_array($_GET['crn'], $crnarray))
{
    $details = array($Skill_Level, $Span, $Area, $Weight_NP, $Loading, $Airfoil, $Motor, $Battery, $Main_Rotor, $Tail_Rotor, $Length, $salsays, $cdsays);
}

$check_array = 0;
while ( list($key, $value) = each($details) )
{
    if (!empty($value))
    {
        $check_array++;
    }
}

if ($check_array)
{
    // There is a value in the array, so do something
}
?>
StrangeRanger
Forum Newbie
Posts: 19
Joined: Tue Jan 31, 2006 2:26 pm

Post by StrangeRanger »

Ahhh... Everah, I think I get it. Like I said, I'm learning as I go here so please let me see if I can explain what your code represents to make sure I understand it.

- You create the array $check_array and initialize it to 0.
- The while loop looks at each key in the $details array and checks to see if each corresponding value is not empty. If the value is not empty, $check_array gets bumped by 1. And subsequently increased each time a value is found in the $details array.
- Then basically if $check_array is > 0 I know I have some values in my $details array. Voila!!

Thank you so much! I think I'm actually learning this!
j
**EDIT** Thanks for all the help guys, I think I've actually got it working properly!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You got it (mostly, $check_array is an integer, not an array). But you get the point for the most part. Good luck with it. Once you learn PHP you will wonder how you ever lived without it.
StrangeRanger
Forum Newbie
Posts: 19
Joined: Tue Jan 31, 2006 2:26 pm

Post by StrangeRanger »

Hi Everah,
Well, I thought I had it figured out, but now I'm getting an error message that I'm not sure how to handle. If you remember from my post about trying to see an array is populated and display a message if it is. Well, that part works well, but when I look at a product that is not within my specified range I get the following error msg:

Warning: Variable passed to each() is not an array or object in /var/www/html/jim/squirrelcart/themes/nsptheme/product_detail.tpl.php on line 37

Line 37 points to my while loop, code snippet is:

Code: Select all

<? $crnarray = array (116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 66, 72, 219, 107, 	                                          203, 212, 88, 215, 216, 201, 91, 111, 69);
						if (in_array($_GET['crn'], $crnarray))
							{
								$details = array($Skill_Level, $Span, $Area, $Weight_NP, $Loading, $Airfoil, $Motor, $Battery,    			                                           $Main_Rotor, $Tail_Rotor, $Length, $salsays, $cdsays);
								
							}
						$check_array = 0;
						while (list($key, $value) = each($details))
							{
								if (!empty($value))
									{
										$check_array++;
									}
							}
						if ($check_array > 0)
							{
								echo "<b><u>Uhmm...Just the facts please Ma'am</u></b>";
							}
					?>
here is a sample of a non-working page:
http://www.nesail.com/jim/index.php?crn ... how_detail

and here is a sample where the while loop works:
http://www.nesail.com/jim/index.php?crn ... how_detail

It's almost like I'm back to my original problem. If $details isn't getting populated I don't want it doing anything. Grrr... any pointers or advice would be greatly appreciated. Tahnk you,
j
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

The problem is happening because you are passing $details to the each() even if $details is not an array (remember you set $details in the if statement to be an array, but if the if statement fails then $details is uninitialized). Replace what you have with this snippet and see if it works.

Code: Select all

<?php
$crnarray = array (116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 66, 72, 219, 107, 203, 212, 88, 215, 216, 201, 91, 111, 69);
if (in_array($_GET['crn'], $crnarray))
{
    $details = array($Skill_Level, $Span, $Area, $Weight_NP, $Loading, $Airfoil, $Motor, $Battery, $Main_Rotor, $Tail_Rotor, $Length, $salsays, $cdsays);

	$check_array = 0;
	while (list($key, $value) = each($details))
	{
	    if (!empty($value))
	    {
	        $check_array++;
	    }
	}
	if ($check_array > 0)
	{
	    echo "<b><u>Uhmm...Just the facts please Ma'am</u></b>";
	}
}
?>
Hope all this helps.
StrangeRanger
Forum Newbie
Posts: 19
Joined: Tue Jan 31, 2006 2:26 pm

Post by StrangeRanger »

Hi Everah,
Just to make sure I understand the logic, I understand what my problem was and please correct me if I'm wrong, but what you are suggesting below is to basically have the while loop inside the original if (in_array($_GET['crn'], $crnarray)) conditional. Is that it? Right, so if 'crn' is not found in the array the whole while loop gets skipped! Duhh... I gotta learn to relax and THINK. :) Thanks for the help, I'll let you know how it goes.
j
**EDIT** Sweet! It worked! Thank you!**
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Glad it worked.
Post Reply