Question

On a site I'm working on there's a subheadline that only should be shown if the information isn't already shown in the main headline. The main headline is an arbitrary string, while the subheadline is created programatically. The subheadline is generated from the contents of a multi-dimensional array (the contents of which are also used for other parts of the page.)

I used the PHP example of foreach to drill down through the array (only half-understanding how it was working), and then tried strpos to see if the values in the array were in the headline string.

Unfortunately, it doesn't work. There is a high chance that I made a stupid mistake in how I thought it's supposed to work. Or that the variable that tells the site to hide the subhead ("hider") is constantly reset to "no" as a result of the other values in the array.

foreach ($arr_info as $i1 => $n1) {    
    foreach ($n1 as $i2 => $n2) {     
        foreach ($n2 as $i3 => $n3) {
            $pos = strpos($headline, $n3);
            if ($pos === false) {
                $hider="no";
            } else {
                $hider="yes";
            }
        }
    }

Any ideas? Greatly appreciate the help.

Was it helpful?

Solution

Add this:

$hider="yes";
break;

Hope it helps

OTHER TIPS

I think a cleaner approach would be to construct a regex out of the values and see if it matches your string:

$values = array();
array_walk_recursive($arr_info, function($k){$GLOBALS['values'][] = preg_quote($k);});
$hider = preg_match('/(' . implode($values, '|') . ')/', $headline) ? 'yes' : 'no';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top