0

I would like to find out if an array as a certain (number in this example) and print.

If value 0 in array $a contains 1.10 print out yes.

$a = array(1.10, 12.4, 1.13);

if (in_array([0] == '1.10')) {
    echo "Yes";
}
4
  • Use array_search(). Commented Sep 2, 2013 at 10:32
  • Why is everyone using in_array in their answers? He only wants to know if element 0 contains 1.10, not if it's anywhere in the array. Commented Sep 2, 2013 at 10:34
  • @Michael Sórm do you want to know if it is at a specific location, or just to know if it exists in the array? Commented Sep 2, 2013 at 10:35
  • The question reads "if an array contains a certain value", so I take it as "in any position". Commented Sep 2, 2013 at 13:56

11 Answers 11

2

No searching is required, just access the element using ordinary array indexing.

if ($a[0] == 1.10) {
    echo "Yes";
}
Sign up to request clarification or add additional context in comments.

2 Comments

+1 for actually answering the OP's question. "If value 0 in array $a contains 1.10 print out yes."
And don't forget that floating point comparison is tricky because storing floating points in binary format is not exact.
2

Use this code:

<?php
$a = array(1.10, 12.4, 1.13);
if($a[0] == "1.10"){
 echo "Yes";
}
?>

Comments

1

You just need a minor adjustment in your in_array PHP function usage:

<?php
$a = array(1.10, 12.4, 1.13);

if (in_array(1.10, $a)) {
    echo "Yes";
}

Output:

Yes

Comments

0
$a = array(1.10, 12.4, 1.13);

if (array_search('1.10', $a) === TRUE) {
    echo "Yes";
}

1 Comment

While this answers the question, please include explanation of what the code does and why it works.
0

This sounds like a job for array_search

which is used in the following manner:

<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');

$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array);   // $key = 1;
?>

If it is in the data, $key is given the corresponding value of the element that contains the data you are searching for.

Comments

0

You need to learn proper syntax. Try:

if ( in_array('1.10', $a) ) {
    echo "Yes";
}


//syntax
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

Function Reference: http://php.net/manual/en/function.in-array.php

Comments

0

1- if you know the index then you can simply do:

if($array[$index] == '1.10'){

}

2- if you want to search for a value if exists in the array:

if(in_array("1.10", $array)){

}

3- if you want to find key for a given value:

   $key = array_search("1.10", $array);

Comments

0

To just search the first element in the array:

if ($a[0] == "1.10") { echo "Yes"; }

Or to search the whole array:

if (in_array("1.10", $a)) { echo "Yes"; }

Comments

0

Use this code:

<?php
$a = array(1.10, 12.4, 1.13);

$isExists = in_array(1.10, $a) ? "Exists" : "Not Exists";

echo $isExists;
?>

Comments

0

Use this code:

<?php
$a = array(1.10, 12.4, 1.13);
echo ($a[0] == "1.10")? "Yes" :"";
?>

Comments

0

You can use aray_search it returns the corresponding key if successful.

$a = array(1.10, 12.4, 1.13);

if (array_search('1.10',$a) !== false) {
    echo "Yes";
}

output:

Yes

you can speed up the function by turning on the strict mode:

it returns the corresponding key if successful.

$a = array(1.10, 12.4, 1.13);

if (array_search('1.10',$a,true) !== false) {
    echo "Yes";
}

output:

Yes

1 Comment

Please add explanation as to why the code works and what it does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.