FAQ
FAQ

Reputation: 13

Checking if values equal true in PHP

How can I check if each value in the variable $c = 90,89,78,88; equal true I think you call it an array in my script one at a time?

This part checks the variable.

if($cat['id'] == $c){
    $url = $parent_url . $cat['url'];
    echo '<a href="' . $url . '" title="' . $cat['category'] . '">' . $cat['category'] . '</a>';            
}

Here is the PHP code.

$c = 90,89,78,88;

function make_list ($parent = 0, $parent_url = '') {
    global $link;
    global $c;

    foreach ($parent as $id => $cat) {

        if($cat['id'] == $c){
            $url = $parent_url . $cat['url'];
            echo '<a href="' . $url . '" title="' . $cat['category'] . '">' . $cat['category'] . '</a>';            
        }

        $url = $parent_url . $cat['url'];

        if (isset($link[$id])) {
            make_list($link[$id], $url); // $url adds url value to sub categories
        }               
    }       
}

Upvotes: 1

Views: 692

Answers (3)

Gatman
Gatman

Reputation: 594

$your_array = explode(",",$c);
foreach( $your_array as $value ) {
    if ( (int) $value === true ) {
         // here type what you what to do if true
    }
}

Upvotes: 0

Mark Baker
Mark Baker

Reputation: 212402

global $c; 

$cArray = explode(',',$c);

foreach ($parent as $id => $cat) { 

    if (in_array($cat['id'],$cArray)) {

Upvotes: 4

Luke Barker
Luke Barker

Reputation: 915

your $c is not an array there - you need to make it an array e.g.

$arrayC = explode(',', $c);

check out the function on php.net/explode

then your if($cat['id'] == $c) would be better as if(in_array($cat['id'], $arrayC)

Upvotes: 0

Related Questions