私はPHPにはとても新しいので、簡単に私に行ってください。この問題は数時間前から私には夢中になっていたので、私は自分の誇りを呑み込んで、なぜこれが問題になるのか理解できることを望んでいると思った。PHP in_array()誤った結果を返す
私は次のコードでオブジェクト配列から変換された配列を有する:
$category_tree = $client->catalogCategoryTree($session_id); // Get an object array of all categories and assign to $category_tree.
$category_list = (array) $category_tree; // Convert $category_tree into an array and assign to $category_list.
この出力print_r($category_list)
を使用する場合は、以下のように:
Array
(
[category_id] => 1
[parent_id] => 0
[name] => Root Catalog
[position] => 0
[level] => 0
[children] => Array
(
[0] => stdClass Object
(
[category_id] => 2
[parent_id] => 1
[name] => Root Category
[is_active] => 1
[position] => 1
[level] => 1
[children] => Array
(
[0] => stdClass Object
(
[category_id] => 8
[parent_id] => 2
[name] => Designer
[is_active] => 1
[position] => 1
[level] => 2
[children] => Array
(
)
)
[1] => stdClass Object
(
[category_id] => 7
[parent_id] => 2
[name] => Shop Bags
[is_active] => 1
[position] => 2
[level] => 2
[children] => Array
(
)
)
[2] => stdClass Object
(
[category_id] => 5
[parent_id] => 2
[name] => DifferentCategory
[is_active] => 1
[position] => 3
[level] => 2
[children] => Array
(
[0] => stdClass Object
(
[category_id] => 6
[parent_id] => 5
[name] => 1
[is_active] => 1
[position] => 1
[level] => 3
[children] => Array
(
)
)
)
)
[3] => stdClass Object
(
[category_id] => 4
[parent_id] => 2
[name] => Sample Category
[is_active] => 1
[position] => 6
[level] => 2
[children] => Array
(
)
)
)
)
)
)
Iは、IFのためにテストしてい文字列がin_array()
を使用してこの配列内に存在する場合、その結果は、実行していないにもかかわらず、文字列内に存在することを示しています。
$string = 'Chanel';
if (in_array($string, $category_list)) {
echo 'The string is in the array';
} else {
echo 'The string is not in the array';
}
この出力:このために以下のコードを参照してください
The string is in the array
をしかし、(上記のアレー出力から見ることができるように)そうではありません。
は、あなたがこの新しい男を提供することができます任意の洞察力のために事前にありがとう:)
==現在、 ' '文字列、厳密な型の比較http://nl1.php.net/in_arrayを使用して0'' – Kisaragi