Page 1 of 2
How do I figure out if an array is populated or not?
Posted: Thu Apr 06, 2006 3:52 pm
by StrangeRanger
Please excuse my noobness, I'm learning. I have read the manual many many times, googled, read forums etc., but still can not get my code to do an Ohh so simple task that it is driving me nuts.
I have an array that gets populated with values from a db if certain conditions are met. This array can receive from 1-11 values from the db depending on whether a value exists in the db or not. These values that are being passed are basically attributes of products that are being sold.
All I'm trying to figure out is if the array has any values at all in it, and for some reason this task is completely stumping me.
The values passed to the array can be strings, int's etc., basically anything. They are descriptors of the product being presented.
I've read about and tried isset(), empty(), is_array() etc. but nothing seems to work. I hope I post this right, but here is an example of the code for creating the array:
Code: Select all
<? if ($_GET['crn'] >=116 && $crn<=129 or 66 or 72 or 219)
{
$details = array($Skill_Level, $Span, $Area, $Weight_NP, $Loading, $Airfoil, $Motor, $Battery, $Main_Rotor, $Tail_Rotor, $Length, $salsays, $cdsays);
}
?>
And then I just want to know if $details contains any of those values. I don't care what the value is, I just want to know if something is in the array, and if one or all of those values are present I simply want to output a line of text to the screen. Can anyone suggest anything please? Thanks,
j
Posted: Thu Apr 06, 2006 4:05 pm
by feyd
the situation is very likely to do with your if expression. Can you tell us what it's supposed to be doing (in plain english)?
Posted: Thu Apr 06, 2006 4:14 pm
by StrangeRanger
Are you asking about the if stmt where it gets crn? All that if stmt does is look at the product's category number and if that category number is w/in that specified range I pull the details about that product from the db and put them into the $details array which are then output as part of that product's description on the web page in a table. If the particular product that a customer is looking at is not w/in that specified range there are no details being displayed. I hope that makes sense

j
Posted: Thu Apr 06, 2006 4:22 pm
by feyd
aside from being trimmed of other code in the script, is this snippet exactly like you have it in the script? If it is, your if expression is broken. Specifically, the or's you're using are creating faulty logic. i.e. If $_GET['crn'] is greater than or equal to 116, the code inside will always be run, regardless of $crn's value.
Re: How do I figure out if an array is populated or not?
Posted: Thu Apr 06, 2006 6:35 pm
by RobertGonzalez
StrangeRanger wrote:And then I just want to know if $details contains any of those values. I don't care what the value is, I just want to know if something is in the array, and if one or all of those values are present I simply want to output a line of text to the screen. Can anyone suggest anything please? Thanks,
j
Code: Select all
<?php
if (count($details) > 0)
{
//there is something in the array
}
else
{
//there is nothing in the array
}
?>
Of course, the way your "if" logic is set up, you are going to run into problems with this. I would suggest maybe using a switch or making your comparison parameter a little cleaner. But to see if there is anything in an array, use the count(arrayname) function compared to zero.
Re: How do I figure out if an array is populated or not?
Posted: Thu Apr 06, 2006 6:49 pm
by hawleyjr
Everah wrote:StrangeRanger wrote:And then I just want to know if $details contains any of those values. I don't care what the value is, I just want to know if something is in the array, and if one or all of those values are present I simply want to output a line of text to the screen. Can anyone suggest anything please? Thanks,
j
Code: Select all
<?php
if (count($details) > 0)
{
//there is something in the array
}
else
{
//there is nothing in the array
}
?>
Of course, the way your "if" logic is set up, you are going to run into problems with this. I would suggest maybe using a switch or making your comparison parameter a little cleaner. But to see if there is anything in an array, use the count(arrayname) function compared to zero.
You should check to make sure it is an array before you make the count call
Code: Select all
<?php
if (is_array($details) && count($details) > 0)
{
//there is something in the array
}
else
{
//there is nothing in the array
}
?>
Posted: Thu Apr 06, 2006 6:57 pm
by Mr_Plough
Would this not do the same?
Code: Select all
if (!empty($details))
{
// There is something in the array
}else{
// The array is empty
}
Posted: Thu Apr 06, 2006 7:08 pm
by RobertGonzalez
Mr_Plough wrote:Would this not do the same?
Code: Select all
if (!empty($details))
{
// There is something in the array
}else{
// The array is empty
}
If $details was a string with a value of anything then the else would trigger. I like Hawley's point. Check to make sure it is an array, then see if it has a count.
Posted: Fri Apr 07, 2006 8:09 am
by StrangeRanger
Thanks for the help guys! I did know to check to see if $details was an array first and I tried the count>0 test, but I'm guessing the problem goes back to my if stmt.
I'm new to coding in PHP so if you guys would help with some pointers I'd love to hear from the voice of experience. The problem is that I did not design the db, and to change it would be a monster ordeal.
The extra details for the products are only populated/displayed if the product being looked at is w/in certain category ranges. But the category ranges are scattered so I'm not sure of how to approach the if stmt. Can any of you please help with some suggestions of how to approach it, or where I might read up on a better approach? If we assume the following:
- The products w/ category numbers between 116-129 and then a couple random category numbers like I have listed, 66, 72, 91, 111, 219 and a couple others. I was wondering if it would be smarter to use else stmts like: if between 116-129, else check for 66, else check for 72 and so on. Would that be a smarter approach instead of all the "or" conditions? Thanks for any pointers you can help with.
j
Posted: Fri Apr 07, 2006 8:36 am
by RobertGonzalez
It's a sort of cheap hack, but you could put all those category values into an array and then do an if(
in_array) as your if statement.
Posted: Fri Apr 07, 2006 9:42 am
by StrangeRanger
Ohhh... I like that idea Everah... so I could just basically fill an array with all the category numbers I need to check for, compare the current category number to the array and if it's in there do what I need to do. Cool. Why didn't I think of that?

j
Posted: Fri Apr 07, 2006 10:55 am
by StrangeRanger
Here's my code snippet:
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);
}
if (is_array($details) && count($details) > 0)
{
echo "<b>The details</b>";
}
?>
My logic is I create the $crnarray and fill it with the crn values I'm watching for.
Then I check to see if the current crn of the product being displayed is in the $crnarray and if it is I create the array $details.
$details is populated with values pulled from the db. There may or may not be a value to be pulled from the db, but the array is still created, or set,but it's empty.
I then check to see if $details is an array and to see if it contains any values from the db. The problem with count() is that it always returns the value 13 because that is the number of elements in the array, even though they are empty elements. So obviously my code above is incorrect.
Is there a function that will check the elements of the array and see if they have a value in them? I've been looking and reading, but have yet not figured it out. Please, does anyone have an idea for me? Maybe I'm not doing a very good job of explaining what I"m trying to do. Thanks,
j
Posted: Fri Apr 07, 2006 11:07 am
by RobertGonzalez
You could do a
foreach() or you can use
list()=
each().
Code: Select all
<?php
foreach($array as $key => $value)
{
echo "The key is $key and the value is $value...<br />";
}
// or you can do...
while ( list($key, $value) = each($array) )
{
echo "The key is $key and the value is $value...<br />";
}
?>
Posted: Fri Apr 07, 2006 12:35 pm
by StrangeRanger
Everah,
I thought of using loops like that, but the customer wanted the data presented in a table. The purpose of trying to see if there was any data in $details was so that I could generate/display a "name" for the table when any of the values are present to be displayed in the table. And I couldn't figure out how to do that with loops without gettting the table name displayed over and over. Never mind creating the table

Would it help to see an example of what I'm trying to do on a web page? Thanks,
j
Posted: Fri Apr 07, 2006 1:38 pm
by RobertGonzalez
StrangeRanger wrote:Would it help to see an example of what I'm trying to do on a web page? Thanks,j
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.