skip to Main Content

How to match value/text Trykk front when the result from var_dump($haystack) looks like this:

array(2) {
    [0] => array(11) {
        ["id"] => string(13) "64085aafcb9db"
        ["type"] => string(10) "checkboxes"
        ["raw"] => array(1) {
            [2] => string(5) "b3ygo"
        }
        ["values"] => array(1) {
            [0] => array(6) {
                ["label"] => string(11) "Trykk front"
                ["price"] => float(42)
                ["price_type"] => string(2) "qt"
                ["slug"] => string(5) "b3ygo"
                ["calc_price"] => float(42)
                ["pricing_hint"] => string(23) "(+kr 42)"
            }
        }
        ["clone_type"] => string(0) ""
        ["label"] => string(31) "Ønsker du trykk på produktet?"
        ["hide_cart"] => bool(false)
        ["hide_checkout"] => bool(false)
        ["hide_order"] => bool(false)
        ["clone_idx"] => int(0)
        ["calc_weight"] => bool(false)
    }
    [1] => array(11) {
        ["id"] => string(13) "64085aaf29908"
        ["type"] => string(4) "file"
        ["raw"] => string(0) ""
        ["values"] => array(1) {
            [0]=>array(4) {
                ["label"] => string(0) ""
                ["price"] => int(0)
                ["price_type"] => string(4) "none"
                ["formatted_label"]=>string(31) ""
            }
        }
        ["clone_type"] => string(0) ""
        ["label"] => string(56) "Om du ønsker trykk på produktet laster du opp logo her"
        ["hide_cart"] => bool(false)
        ["hide_checkout"] => bool(false)
        ["hide_order"] => bool(false)
        ["clone_idx"] => int(0)
        ["calc_weight"] => bool(false)
    }
}
$matches  = preg_grep ('/^Trykk front (w+)/i', $haystack);

2

Answers


  1. You can use array_filter like:

    print_r(array_filter($array, function($a){
        return $a['values'][0]['label'] === 'Trykk front';
    }));
    

    Fiddle

    Login or Signup to reply.
  2. Based on your var_dump output, "Trykk front" is from $haystack[0]['values']. So, assuming your code needs to be updated like this:

    $values = $haystack[0]['values'];
    
    foreach ($values as $value) {
        $label = $value['label'];
    
        if (preg_match('/^Trykk front/i', $label)) {
            // Match found
            echo "Match found: " . $label;
            break;
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search