Performing array validation within another array

Asked

Viewed 238 times

2

I have the following array, when I give a var_dump():

array(2) {
  [""]=>
  array(10) {
    ["js/ultramegamenu.js"]=>
    string(19) "js/ultramegamenu.js"
    ["js/padoo/jquery.cycle.js"]=>
    string(24) "js/padoo/jquery.cycle.js"
    ["js/jquery.mask.js"]=>
    string(17) "js/jquery.mask.js"
    ["js/smartheader.js"]=>
    string(17) "js/smartheader.js"
    ["js/stickyheader.js"]=>
    string(18) "js/stickyheader.js"
    ["js/app.js"]=>
    string(9) "js/app.js"
    ["js/carousel/script.js"]=>
    string(21) "js/carousel/script.js"
    ["js/lazyload.js"]=>
    string(14) "js/lazyload.js"
    ["js/script.js"]=>
    string(12) "js/script.js"
    ["js/plumrocket/pslogin/pslogin.js"]=>
    string(32) "js/plumrocket/pslogin/pslogin.js"
  }
  ["defer"]=>
  array(2) {
    ["js/tm/ajaxsearch/xregexp-all.js"]=>
    string(31) "js/tm/ajaxsearch/xregexp-all.js"
    ["js/tm/ajaxsearch.js"]=>
    string(19) "js/tm/ajaxsearch.js"
  }
}

With the following code below, I try to make the comparison where, if the position value is equal to string, which in case would be the directory of my script, he would perform the same(the var_dump() I give in the code is what is being displayed above).

foreach ($lines as $if => $items) {
    if (empty($items)) {
        continue;
    }
    if (!empty($if)) {
        // open !IE conditional using raw value
        if (strpos($if, "><!-->") !== false) {
            $html .= $if . "\n";
        } else {
            $html .= '<!--[if '.$if.']>' . "\n";
        }
    }

    // static and skin css
    $html .= $this->_prepareStaticAndSkinElements('<link rel="stylesheet" type="text/css" href="%s"%s />'."\n",
        empty($items['js_css']) ? array() : $items['js_css'],
        empty($items['skin_css']) ? array() : $items['skin_css'],
        $shouldMergeCss ? array(Mage::getDesign(), 'getMergedCssUrl') : null
    );

    var_dump($items['skin_js']);

    foreach ($this->_data['items'] as $key => $value) {
        echo $value['name'];
    }
    if (($items['skin_js'] == "js/tm/ajaxsearch/xregexp-all.js") || ($items['skin_js'] == "js/tm/ajaxsearch.js") || ($items['skin_js'] == "js/jquery.mask.js")) {
        echo "Entrou";
        $html .= $this->_prepareStaticAndSkinElements('<script type="text/javascript" src="%s"%s></script>' . "\n",
            empty($items['js']) ? array() : $items['js'],
            empty($items['skin_js']) ? array() : $items['skin_js'],
            $shouldMergeJs ? null : null
        );
    } else{
        // static and skin javascripts
        $html .= $this->_prepareStaticAndSkinElements('<script type="text/javascript" src="%s"%s></script>' . "\n",
            empty($items['js']) ? array() : $items['js'],
            empty($items['skin_js']) ? array() : $items['skin_js'],
            $shouldMergeJs ? array(Mage::getDesign(), 'getMergedJsUrl') : null
        );
    }

    // other stuff
    if (!empty($items['other'])) {
        $html .= $this->_prepareOtherHtmlHeadElements($items['other']) . "\n";
    }

    if (!empty($if)) {
        // close !IE conditional comments correctly
        if (strpos($if, "><!-->") !== false) {
            $html .= '<!--<![endif]-->' . "\n";
        } else {
            $html .= '<![endif]-->' . "\n";
        }
    }
}

The problem is he’s not getting inside the if, thus, is not being executed properly.

  • 1

    The problem with your code is that you are comparing an array with a string, in your case it will always be false. $items['skin_js'] is a vector of a vector, to access it you must use $items["skin_js"][""]["js/tm/ajaxsearch/xregexp-all.js"] or $items["skin_js"]["defer"]["js/tm/ajaxsearch/xregexp-all.js"], so you will be able to access the String inside the vector

1 answer

2


The if will work properly if you do it the following way:

foreach ($items['skin_js'] as $value) {
    if (( $value["js/tm/ajaxsearch/xregexp-all.js"] != null ) || ($value["js/tm/ajaxsearch.js"] != null) || ($value["js/jquery.mask.js"] != null)) {
            echo "Entrou";
            ...
        } else{
            // static and skin javascripts
            ...
        }
}

The foreach initial scan the two elements of your vector [""] and ["defer"] and the $value is the array contained in these keys. It is checked whether it contains any specified key elements. As the key and value are equal, it was not necessary to check whether $value["js/tm/ajaxsearch/xregexp-all.js"] == "js/tm/ajaxsearch/xregexp-all.js" for example. Only if the position value was not empty.

  • It worked! That’s just what I needed. Thank you so much for all your help and attention!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.