-1

I have this code that works:

foreach ($plentymarkets_variation['properties'] as $row) {

            if (strpos($row['values']['0']['value'], $needle_de_1) === 0) {
                $result_de_1 = $row['values']['0']['value'];
                $shopware_product["translations"]["0"]["customFields"]["free1"] = str_replace("Text_de_1_","",$result_de_1);
                $shopware_product["customFields"]["free1"] = str_replace("Text_de_1_","",$result_de_1);
                break;
            }

Now when i try to make an elseif statement i get an 500 error if the value $row['values']['1']['value'] is false like this:

foreach ($plentymarkets_variation['properties'] as $row) {

            if (strpos($row['values']['0']['value'], $needle_de_1) === 0) {
                $result_de_1 = $row['values']['0']['value'];
                $shopware_product["translations"]["0"]["customFields"]["free1"] = str_replace("Text_de_1_","",$result_de_1);
                $shopware_product["customFields"]["free1"] = str_replace("Text_de_1_","",$result_de_1);
                break;
            }
                elseif (strpos($row['values']['1']['value'], $needle_de_1) === 0) {
                    $result_de_1 = $row['values']['1']['value'];
                    $shopware_product["translations"]["0"]["customFields"]["free1"] = str_replace("Text_de_1_","",$result_de_1);
                    $shopware_product["customFields"]["free1"] = str_replace("Text_de_1_","",$result_de_1);
                    break;
                }
                elseif (strpos($row['values']['2']['value'], $needle_de_1) === 0) {
                    $result_de_1 = $row['values']['2']['value'];
                    $shopware_product["translations"]["0"]["customFields"]["free1"] = str_replace("Text_de_1_","",$result_de_1);
                    $shopware_product["customFields"]["free1"] = str_replace("Text_de_1_","",$result_de_1);
                    break;
                }
                elseif (empty($plentymarkets_variation['item']['free1'])) {
                    $shopware_product["customFields"]["free1"] = " ";
                    $shopware_product["translations"]["0"]["customFields"]["free1"] = " ";
                }
        }

Actually there is a value in the first if statement but there should be a false response from first elseif statement. Shouldn't that work?

Actually it already worked but now it seems to not work anymore and i can't explain why. The strukture from the array changed a little bit but i adapt it already as far as i can see.

  • 1
    _"i get an 500 error"_ - then go check what the error log has to say about the _reason_ for this 500 error, first of all. – CBroe Nov 09 '22 at 09:09
  • Under development and just in the development environment I suggest switching on the error messages. Or what @CBroe suggested, check the PHP error log. https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – hNczy Nov 09 '22 at 09:13
  • @CBroe Sorry guys you are right. I get following error: request.CRITICAL: Uncaught PHP Exception TypeError: "strpos() expects parameter 1 to be string, null given. – Scuturici David Nov 09 '22 at 09:17
  • Well then one of the `$row['values'][...]['value']` you are trying to access, appears to not actually exist. So check that using isset/empty, before you try to work with it. – CBroe Nov 09 '22 at 09:21
  • I don't get it. First why is the numbered array index in row not a number based index? And why are you using the plantymarkets array in the last elseif case? Is there more then "properties" in this array? And at last why not normalize the values into a single variable and do a switch on that? – Rene M. Nov 09 '22 at 09:27

1 Answers1

0

Looking at your error the best thing you can do is check if the value you accessing in the array is set with something like:

isset($row['values']['1']['value']) && 
strpos($row['values']['1']['value'], $needle_de_1) === 0

Also I suggest you to refactor your code in a foreach for the variable $row['values'] (or a for loop if you gonna check only the indexes in the snippets and you know that there are other indexes) and use a control variable to get out of the foreach statement.

foreach ($plentymarkets_variation['properties'] as $row) {
    $flag = false;
    foreach($row['values'] as $value) {
        if(isset($value['value'] && strpos($value['value'], $needle_de_1) === 0) {
            $result_de_1 = $value['value'];
            $shopware_product["translations"]["0"]["customFields"]["free1"] = str_replace("Text_de_1_","",$result_de_1);
            $shopware_product["customFields"]["free1"] = str_replace("Text_de_1_","",$result_de_1);
            $flag = true;
            break;
        }
    }
    if($flag) {
        break;
    }
    if(empty($plentymarkets_variation['item']['free1'])) {
        $shopware_product["customFields"]["free1"] = " ";
        $shopware_product["translations"]["0"]["customFields"]["free1"] = " ";
    }
}
  • Your comment is awesome thanks. I already advanced with that problem and could work somehow with this: '''foreach ($plentymarkets_variation['properties'] as $row) { if (is_array($row['values'])){ foreach ($row['values'] as $valuerow) { $needle_de_1 = 'Text_de_1_'; if (strpos($valuerow["value"], $needle_de_1) === 0) { print_r($valuerow["value"]); $flag = true; break; } } } }''' The problem is that the loop is not aktually breaking and continues. There are 2 same values and when i print $valuerow["value"] it shows both. – Scuturici David Nov 09 '22 at 13:45