Alfred
Alfred

Reputation: 21406

php array variable

in my script,

$value= array("DK","Bloomsberry","McGrawHill","OXFORD","DC Books","Springer");

if(in_array("Bloomsberry",$value)){
echo "Bloomsberry is there inside the array";}
else{ echo "Bloomsberry is not there ";}

this works well

i have a variable $names which is a mysql result, which has data "DK","Bloomsberry","McGrawHill","OXFORD","DC Books","Springer" like an array data.

but when i place the variable inside like $value= array($names); instead of $value= array("DK","Bloomsberry","McGrawHill","OXFORD","DC Books","Springer"); , i am getting result "Bloomsberry is not there " instead of expected "Bloomsberry is there inside the array"

Upvotes: 0

Views: 257

Answers (5)

Poonam Bhatt
Poonam Bhatt

Reputation: 10332

try this

$name = '"DK","Bloomsberry","McGrawHill","OXFORD","DC Books","Springer"';
$name = str_replace('"', '', $name);
$value = explode(',', $name);

Now your below given code will work.

if(in_array("Bloomsberry",$value)){
echo "Bloomsberry is there inside the array";}
else{ echo "Bloomsberry is not there ";}

Upvotes: 0

xzyfer
xzyfer

Reputation: 14135

Notice the following difference:

$value = array("DK","Bloomsberry","McGrawHill","OXFORD","DC Books","Springer");
print_r($value);

/* produces:
Array
(
    [0] => DK
    [1] => Bloomsberry
    [2] => McGrawHill
    [3] => OXFORD
    [4] => DC Books
    [5] => Springer
)
*/

where as:

$value = array("DK","Bloomsberry","McGrawHill","OXFORD","DC Books","Springer");
$newValue = array($value);
print_r($newValue );

/* produces:
Array
(
    [0] => Array
    (
        [0] => DK
        [1] => Bloomsberry
        [2] => McGrawHill
        [3] => OXFORD
        [4] => DC Books
        [5] => Springer
    )
)
*/

in_array("Bloomsberry", $newValue) will only return true if "Bloomsberry" is a value in the first dimension of the array. However the only first dimension element in $newValue is the $value array.

Upvotes: 1

Ronald
Ronald

Reputation: 106

I suspect that the " signs get escaped and that also the array is passed as an single string. You will have to split the string to an array. If I where you I would submit to mysql: "DK,Bloomsberry,McGrawHill"etc and then do

<?php
$string = "DK,Bloomsberry,McGrawHill,OXFORD,DC Books,Springer";
$array = explode(",", $string);
if(in_array("Bloomsberry",$array)){
  echo "Bloomsberry is there inside the array";}
else{ echo "Bloomsberry is not there ";}

The explode command returns an array split on the commas. I hope this works for you

Upvotes: 1

mike
mike

Reputation: 4433

If $names is already an Array, then array($names) is an Array containing one element (the one element is your $names array).

If you want to assign $value to the array $names, you simply use the assignment operator:

$value = $names; 

Then do your conditional in_array("Bloomsberry", $value);. Or you could just avoid the assignment and do in_array("Bloomsberry", $names).

Upvotes: 1

RobertPitt
RobertPitt

Reputation: 57268

Issue:

This is because in_array starts checking at the root node, and depending on how many levels the needle has depends in how many levels it checks within the haystack

An example of the above:

$a = array(array('p', 'h'), array('p', 'r'), 'o');

if (in_array(array('p', 'h'), $a))
{
    echo "'ph' was found\n";
}

$a is actually 2 levels of arrays, also called multi-dimensional.

Within your code your placing your root level array, into another array, thus the array of the DB Then becomes array(array("results")) this when you check the first node using in_array("string") it cant find it within the root haystack.

Possible Fix:

Just use the actual result as your in_array check, example:

while($row = mysql_fetch_array($result))
{
    /*
        $row is  a single dimension and can be used like so:
    */
    if(in_array("Bloomsberry"),$row))
    {
        //Echo your success.
    }
}

Upvotes: 0

Related Questions