Jump to content

[SOLVED] >>>How can I search an Array?<<<


lotrfan

Recommended Posts

Hi all,

 

I started PHP about a month ago. Loving it.

 

Anyway, I am trying to use an code with this:

 

$_REQUEST[angle_a]=$angle_a;
$_REQUEST[angle_b]=$angle_b;
$_REQUEST[angle_c]=$angle_c;

 

and this...

 

$angle[0]=$angle_a;
$angle[1]=$angle_b;
$angle[2]=$angle_c;

 

I want this to be an array so that the PHP can see which angle (in the triangle) is in a certain place.

I want now to be able to find out if no value has been given for these angles. Because if no value for an angle has been given by the user in an input form, the code will then proceed to find the remaining angles.

 

So how do I "search" this array to find which $angle[?] has no value?

 

Thanks.

 

PS -- Please use layman's terms, I'm not very good at PHP yet...

Link to comment
https://forums.phpfreaks.com/topic/71284-solved-how-can-i-search-an-array/
Share on other sites

Ok, first of all you are doing it in the wrong direction:

 

<?php

//WRONG
$_REQUEST[angle_a]=$angle_a;
$_REQUEST[angle_b]=$angle_b;
$_REQUEST[angle_c]=$angle_c;

//RIGHT
$angle_a=$_REQUEST[angle_a];
$angle_b=$_REQUEST[angle_b];
$angle_c=$_REQUEST[angle_c];

?>

 

You want to set the $angle_* variables, not the $_REQUEST[ * ] variables :) I remember I was making these mistakes once too.

 

 

Now, as for checking if a value is empty, you simply use the empty() function. This function returns TRUE if the var is empty, FALSE if it is not. So if you want to check if $var is empty you'd do something like this:

 

<?php
if(empty($var))
{
echo "it's empty!";
}else{
echo "it's not!";
}
?>

 

 

Feel free to ask more questions :)

 

Orio.

Thanks for the quick reply -- it really means a lot!  You obviously know what you are talking about. I fixed the transposition of the $_REQUEST as well.  Helped a lot  :).

 

Anyway, here's what is did:

 

if (empty($angle[0])){
echo "No value for angle0 given.";}

if (empty($angle[1])){
echo "No value for angle1 given.";}

if (empty($angle[2])){
echo "No value for angle2 given.";}

 

This works, but is there a way that I wouldn't have to write it out three times? Could I somehow tell PHP to look for ALL keys given within the $angle[] array?

 

Thanks!

Neat. That would make things a lot easier for a decagon!

 

Now what if I wanted to identify the angles I DO have? Would I use the 'is_array' function? Is that the opposite of the 'empty'? How would I do that?

 

I am trying to use the $number_of_angles variable to tell user how many angles there are. I just tried to use an else statement after the if statement, but that didn't work.

 

This is what I'm trying to do:

$number_of_angles=0;

for($i=0; $i<=2; $i++)
{
if (empty($angle[$i])){
	echo "No value for angle $i given.";
                else ($number_of_angles= $number_of_angles + 1)}
}

echo "There are " ,$number_of_angles , " angles in this triangle.";

 

Thanks once again. If I am wasting your life away with my questions, please tell me -- I don't want to be an irritant.

More like this ;)

 

<?php

$number_of_angles=0;

for($i=0; $i<=2; $i++)
{
if (empty($angle[$i])){
	echo "No value for angle $i given.";
}else{
	$number_of_angles= $number_of_angles + 1;
}
}

echo "There are " .$number_of_angles ." angles in this triangle.";

?>

 

 

Orio.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.