1

I use the PHP variable $sku to remove certain products from the export. The code below works good if I only want to remove the product with SKU X from the export.

foreach ($skus as $key => $sku){if ($sku == "X") return "";

But if I want to remove products with SKU X and/or Y and/or Z from the export, the code below gives empty output. Any idea what is the issue here?

foreach ($skus as $key => $sku){if ($sku == "X" || $sku == "Y" || $sku == "Z") return "";

Update: I have changed the function, but now I am getting Fatal error: 'continue' not in the 'loop' or 'switch' context in your code on line 32 (continue;).

function get_order_item_lines($skus = '', $qty = ''){
    
    $xml = '';
    if (!empty($skus) && is_array($skus)){
        
        foreach ($skus as $key => $sku){
            if (strpos(strtoupper($sku, "Z")) === 0) return "";
            $xml .= "<L>".PHP_EOL;
            $xml .= "<P>" . $sku . "</P>".PHP_EOL;
            $xml .= "<Q>" . (empty($qty[$key]) ? "" : $qty[$key]) . "</Q>".PHP_EOL;
            $xml .= "</L>".PHP_EOL;
        }

    } else {
        $skus = explode( ",", $skus );
        if ( !empty( $skus[0] ) && !empty( $qty[0] ) ) {
            if (strpos(strtoupper($skus[0], "Z")) === 0) return "";
            $xml .= "<L>".PHP_EOL;
            $xml .= "<P>" . trim( $skus[0] ) . "</P>".PHP_EOL;
            $xml .= "<Q>" . (empty($qty[0]) ? "" : $qty[0]) . "</Q>".PHP_EOL;
            $xml .= "</L>".PHP_EOL;
        }
    }

    return $xml;
}
1
  • What have you tried to debug the problem? Commented Jul 17, 2020 at 7:21

4 Answers 4

2

Pay attention to the return , do not return (leave the function) when you mean to continue the foreach iteration. Your code is erroneous in doing this:

foreach ($skus as $key => $sku){
    $sku = strtoupper($sku);
    if ($sku == "X" || $sku == "Y" || $sku == "Z") return "";

The above return ""; leaves the function entirely when it finds a $sku being X, Y, or Z and returns NOTHING. Your question/problem states you get an EMPTY value, that's exactly why. If it finds 1 sku with either X, Y, or Z it's going to RETURN. If all you want to do is continue , then let it continue while skipping X, Y, Z skus.

foreach ($skus as $key => $sku){
  $sku = strtoupper($sku);
  if ( $sku == "X" || $sku == "Y" || $sku == "Z") {
    //ignore these skus and continue foreach
    continue;
  }

  $good_skus[] = $sku;
}

var_dump($good_skus);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I have added strtoupper in both if and else statement, but still empty output. I have added the complete function in the initial post.
The reason it's returning no value it you're having it return. Replace return with continue
Thanks for your help. I have tried to add your code, but I get a fatal error. I have updated the initial post. I am sorry, I am not that skilled..
1

Maybe this will do the trick.

function get_order_item_lines($skus = '', $qty = '')
{
    $xml = '';

    if (!empty($skus) && is_array($skus))
    {
        foreach ($skus as $key => $sku)
        {
            $skuUpper = strtoupper($sku); // change to upper
            
            if ($skuUpper != 'WANHAO-STICKER'
                && strpos($skuUpper, 'NWS-', 0) === false)  // string not contains..
            {
                $xml .= "<L>".PHP_EOL;
                $xml .= "<P>" . $sku . "</P>".PHP_EOL;
                $xml .= "<Q>" . (empty($qty[$key]) ? "" : $qty[$key]) . "</Q>".PHP_EOL;
                $xml .= "</L>".PHP_EOL;
            }
        }
    } 
    else 
    {
        $skus = explode( ",", $skus );

        if ( !empty( $skus[0] ) && !empty( $qty[0] ) ) 
        {
            $skuUpper = strtoupper($skus[0]); // change to upper
            
            if ($skuUpper != 'WANHAO-STICKER'
                && strpos($skuUpper, 'NWS-', 0) === false)
            {
                $xml .= "<L>".PHP_EOL;
                $xml .= "<P>" . trim( $skus[0] ) . "</P>".PHP_EOL;
                $xml .= "<Q>" . (empty($qty[0]) ? "" : $qty[0]) . "</Q>".PHP_EOL;
                $xml .= "</L>".PHP_EOL;
            }
        }
    }

    return $xml;
}

1 Comment

Thanks John, You always save my day!
1

There are several ways to do this, if the array is more complex you could use array_filter() and use in_array() with an array of the SKU's to remove...

$filteredSkus = array_filter($skus, function ($sku) use ($skusToRemove){
    return !in_array($sku, $skusToRemove);
});

Or if the SKU is simply the value in the array, you can array_diff() with an array of the SKU's to remove...

$difSkus = array_diff($skus, $skusToRemove);

Comments

0

Related to Nigel Ren's answer which is correct in my opinion, here an example to your question:

// remove skus X, Y, and Z
$filteredSkus = array_filter($skus, static function($sku) {
    return !($sku === 'X' || $sku === 'Y' || $sku === 'Z')
});

If you want to filter skus which start with these letters you need to use strpos like that:

// remove skus starting with X, Y, and Z
$filteredSkus = array_filter($skus, static function($sku) {
    return !(
        strpos(strtoupper($sku, 'X')) === 0 ||
        strpos(strtoupper($sku, 'Y')) === 0 ||
        strpos(strtoupper($sku, 'Z')) === 0
    )
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.