2011-02-12 4 views
0

PHPの配列変数

$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 ";} 

これは私が配列データのようなデータを持っている"DK","Bloomsberry","McGrawHill","OXFORD","DC Books","Springer" mysqlの結果、ある$names変数を持っているだけでなく

に動作します。

が、私は$value= array($names);代わりの$value= array("DK","Bloomsberry","McGrawHill","OXFORD","DC Books","Springer");のような内部変数を配置するとき、私は結果"Bloomsberry is not there "を得る代わりに"Bloomsberry is there inside the array"

答えて

1

私は「兆候が脱出し得ることをしても配列がシングルとして渡されていると思われます。。文字列あなたは私はあなたが私は、MySQLに提出する場所場合、配列に文字列を分割する必要があります:「DK、Bloomsberry、McGrawHill」など、その後行う

<?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 ";} 

explodeコマンドは、コンマで区切られた配列を返します。 私はこれがあなた

1

を期待しています$namesがすでにアレイであれば、array($names)は一つの要素(一つの要素であるを含む配列でありますあなたの$names配列)。あなたの条件in_array("Bloomsberry", $value);を行うその後

$value = $names; 

:あなたは、配列$names$valueを割り当てる場合

、あなたは単に代入演算子を使用します。または、割り当てを避けて、in_array("Bloomsberry", $names)を実行してください。

0

問題:

in_array開始がルートノードで確認し、それは干し草の山

の例の中にチェック何レベルに依存するどのように多くのレベル針が有するに依存するからです

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

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

$aは、実際には多次元と呼ばれる2レベルの配列です。

あなたのコード内で、ルート配列を別の配列に配置すると、DBの配列in_array("string")を使用して最初のノードをチェックするとarray(array("results"))になります。ルートhaystack内でそれを見つけることができません。

可能な修正:

ちょうどあなたのin_arrayチェック、一例として、実際の結果を使用します。

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. 
    } 
} 
1

お知らせ以下の違いのために働く願っています:

として
$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 
) 
*/ 

:「Bloomsberryは」の最初の次元の値である場合

$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)のみtrueを返します配列。しかし、$ newValueの最初の次元要素は$ value配列だけです。

0

は今、あなたの指定したコードの下、この

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

が動作しますしてみてください。

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