1

I recently posted this question on Stack - Search array for specific string - and got the perfect answer for my question, which I marked as correct. However I now have a related issue, which I'm finding tricky to solve, but it seems like it should be quite easy!

I have searched an array for the string 'GB' and 'US' using the following, which was provided to me on the previous post:

/* Get Movie release dates */
$releases = $tmdb->getMovieReleases($tmdb_id);

echo "<pre>";
print_r($releases);
echo "</pre>";

$uk_release = -1;

foreach($releases['countries'] as $k=>$v) {
    if(array_search('GB', $v)) {
        $uk_release = $k;
        break;
    } else {
        $uk_release = null;
    }
}

$us_release = -1;

foreach($releases['countries'] as $k=>$v) {
    if(array_search('US', $v)) {
        $us_release = $k;
        break;
    } else {
        $us_release = null;
    }
}

$uk_rating = $releases['countries'][$uk_release]['certification'];
$us_rating = $releases['countries'][$us_release]['certification'];

which is then parsed into the page using a simple <?php echo $uk_rating; ?>/<?php echo $us_rating; ?>.

I've found, however that some arrays don't actually contain the data the query is searching for, such as the following, which doesn't have a UK rating or release date:

Array
(
    [id] => 13655
    [countries] => Array
        (
            [0] => Array
                (
                    [iso_3166_1] => US
                    [certification] => G
                    [release_date] => 2008-06-20
                )

            [1] => Array
                (
                    [iso_3166_1] => CA
                    [certification] => 
                    [release_date] => 2008-06-20
                )

            [2] => Array
                (
                    [iso_3166_1] => JP
                    [certification] => 
                    [release_date] => 2008-08-01
                )

        )

)

I've attempted to construct various IF statements to check if one of the two queries is null, empty, == "" etc etc. but nothing seems to work. Currently, I'm attempting the following, basically using the logic IF neither results are equal to nothing, show both, or if one of the two is empty, only show the one which isn't empty (apologies for the formatting):

if ($uk_date !== "" && $us_date !== "") {?>
                                <section class="span3">
                                    <p class="overview-title">RELEASE DATE</p>
                                    <p class="section_body">
                                        <?php echo $uk_date; ?> (UK)<br>
                                        <?php echo $us_date; ?> (US)
                                    </p>
                                </section><?php
                            } else if ($uk_date = "") {?>
                                <section class="span3">
                                    <p class="overview-title">RELEASE DATE</p>
                                    <p class="section_body">
                                        <?php echo $us_date; ?> (US)
                                    </p>
                                </section><?php
                            } else if ($uk_date == "") {?>
                                <section class="span3">
                                    <p class="overview-title">RELEASE DATE</p>
                                    <p class="section_body">
                                        <?php echo $uk_date; ?> (UK)
                                    </p>
                                </section><?php
                            }
Community
  • 1
  • 1
Alex Ryans
  • 1,865
  • 5
  • 23
  • 31

2 Answers2

1

Try

$uk_rating = isset($releases['countries'][$uk_release]['certification'])?$releases['countries'][$uk_release]['certification']:'';
$us_rating = isset($releases['countries'][$us_release]['certification'])?$releases['countries'][$us_release]['certification']:'';
Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
Arvind
  • 938
  • 9
  • 23
1

The !== operator checks for equality and type. In your example - if UK doesn't exist in the array - the interpreter would have (flagged a warning and) assigned the value null to $uk_date. (IE. not an empty string).

If you changed !== to != it should work but best practice though usually involves eliminating warnings too so for integrity your check should be made on the array itself.

if(isset($releases['countries'][$uk_release])){
  // UK exists in array
} else {
  // does not
}

The other answer solves this too - it's just a short-hand ternary-operator
(as opposed to the conditional if-else).

Community
  • 1
  • 1
Emissary
  • 9,954
  • 8
  • 54
  • 65
  • Thanks for your help, how would I make the check on the array itself? Would I have to attach an else {} statement to the if which searches the array and, if so, how would I go about this? – Alex Ryans Mar 24 '13 at 11:55
  • That's the conditional operator and it is *a* ternary operator. – Marcel Korpel Mar 24 '13 at 12:02
  • This worked perfectly, coupled with a little IF statement inside this to see if the individual elements (such as certification and release date) were present. Thank you! – Alex Ryans Mar 24 '13 at 12:18