简体   繁体   中英

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

I'm very new to PHP so I could be possibly overlooking something simple here.

A quick overview of what my script does:

  • Interprest a CSV file and stores each row as an array ( $line ).
  • Loops through the array $line and stores the results of the specified key as aa string ( $csv_product_list ) if the array is not empty.
  • Retrieves a list of product SKU's from a Magento database and stores them as an array ( $sku ).

I am then attempting to loop through the $sku array whilst checking if the value of $sku exists within the string $csv_product_list . The problem is that I am receiving the same results even if the value does exist within the string $csv_product_list . For this validation I am using:

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>';
}

In the screenshot below, I have output the value of $csv_product_list and below that line are the loop results for two of the values of $sku .

在此处输入图片说明

As you can see, CB853 is resulting that it does not exist within $csv_product_list despite the result above showing that it does.

Please note that does not exist within $csv_product_list , so the results should be different from each other when this script is working correctly. $csv_product_list 中不存在,因此,当此脚本正常工作时,结果应彼此不同。

Please could anybody offer insight as to what is wrong here? My full code is included below for any clarification required. Thank you very much in advance for your time.

<?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) */
?>

You are not using it correctly:

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

The result of $sku[$s] !== false is a boolean, true or false , so you are checking whether a boolean exists in $csv_product_list .

You need:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM