PHP 'strpos($string, $value !== false)' 工作不正常

PHP 'strpos($string, $value !== false)' Not Working Correctly

我是 PHP 的新手,所以我可能会忽略这里的一些简单内容。

快速了解我的脚本的作用:

然后我尝试遍历 $sku 数组,同时检查 $sku 的值是否存在于字符串 $csv_product_list 中。问题是,即使字符串 $csv_product_list 中存在值 ,我也会收到相同的结果。对于此验证,我使用:

if (strpos($csv_product_list, $sku[$s] !== false)) {
    echo '<b>', $sku[$s], '</b> does exist within <b>$csv_product_list</b><br>';
} else {
    echo '<b>', $sku[$s], '</b> does not exist within <b>$csv_product_list</b><br>';
}

在下面的屏幕截图中,我输出了 $csv_product_list 的值,该行下方是 $sku.

中两个值的循环结果

如您所见,CB853 导致它不存在于 $csv_product_list 中,尽管上面的结果表明它存在。

请注意 CB967$csv_product_list 中不存在,因此当此脚本正常运行时,结果应该彼此不同。

任何人都可以提供有关这里出了什么问题的见解吗?我的完整代码包含在下面,用于任何需要的澄清。非常感谢您的宝贵时间。

<?php
/** GLOBAL VARIABLES (START) */
    $client = new SoapClient('***'); // Magento API URL.
    $session_id = $client->login('***', '***'); // Standard API 'User Name' and 'API Key'. 
/** GLOBAL VARIABLES (END) **/

/** CSV (START) */
    $csv = array(); // ?
    $line = array(); // ?
    // ?
    if (FALSE !== $handle = fopen("test.csv", "r")) {
        while (FALSE !== $row = fgetcsv($handle)) {
            $csv[] = $row;
        }
    }
    // ?
    foreach (array_slice($csv, 1) as $row) {
        $new_row = array();
        for ($i = 0, $c = count($csv[0]); $i < $c; ++$i) {
            $new_row[$csv[0][$i]] = $row[$i]; 
        }
        $line[] = $new_row;
    }
/** CSV (END) */

/** CSV SKU CONVERSION (START) */
    $csv_product_list = ''; // Create $csv_product_list variable before results of the loop are added.

    // Loop through each CSV row and assign the results to $csv_product_list.
    foreach ($line as $iLineNumber => $data_line) {
        $data_line_without_change = $data_line;

        if (!empty($data_line['Product SKU'])) {
                $csv_product_list .= $data_line['Product SKU'] . ' ';
        }
    }
/** CSV SKU CONVERSION (END) */

/** ZOEY PRODUCTS (START) */
    // Filter for where the 'FMA Stock' attribute is set to 'Yes'.
    $fma_stock_filter = array('complex_filter'=>
        array(
            array('key'=>'fma_stock', 'value'=>array('key' =>'eq', 'value' => 'Yes')),
        ),
    );
    // Retrieve list of products using the filter and assign the result to $zoey_product_list.
    $zoey_product_list = $client->catalogProductList($session_id, $fma_stock_filter);

    // Convert the result from $zoey_product_list into an array of SKU's as $sku.
    $sku = [];
    foreach ($zoey_product_list as $item) {
        $sku[] = $item->sku;
    }
/** ZOEY PRODUCTS (END) */

    echo 'The current contents of <b>$csv_product_list</b> are ', $csv_product_list, '<br><br>';

/*** DISABLE PRODUCTS (START) */
    for ($s = 0; $s < count($sku); $s++) {
        if (strpos($csv_product_list, $sku[$s] !== false)) {
            echo '<b>', $sku[$s], '</b> does exist within <b>$csv_product_list</b><br>';
        } else {
            echo '<b>', $sku[$s], '</b> does not exist within <b>$csv_product_list</b><br>';
        }
    }
/*** DISABLE PRODUCTS (END) */
?>

你没有正确使用它:

if (strpos($csv_product_list, $sku[$s] !== false)) {
                                                ^ this is wrong

$sku[$s] !== false 的结果是布尔值,truefalse,因此您正在检查 $csv_product_list 中是否存在布尔值。

你需要:

if (strpos($csv_product_list, $sku[$s]) !== false) {
                                      ^ here