5

I have this array I'm doing like so:

Turns out some of those var aren't always set which cause undefined index issues when trying to inserting it into the db?

Is there a quick way to check each value is set before adding it to the array?

    $show_insert = array(
        'title' => $show_data['title'],
        'thetvdb_id' => $show_data['tvdbid'],
        'release_date' => $show_data['FirstAired'],
        'lastupdatethetvdb' => $show_data['lastupdatethetvdb'],
        'moviedb_id' => $show_data['moviedb_id'],
        'imdb_id' => $show_data['IMDB_ID'],
        'img_id' => $show_data['tvdbid'],
        'media_type' => 1,
        'status' => $show_data['status'],
        'length' => $show_data['runtime'],
        'network' => $show_data['network'],
        'overview' => $show_data['overview'],
        'seasons' => $show_data['seasons'],
        'episodes' => $show_data['episodes'],
        'homepage' => $show_data['homepage'],
        'last_airdate' => $show_data['last_air_date'],
        'vote_average' => $show_data['vote_average']
    );
Callombert
  • 1,099
  • 14
  • 38

3 Answers3

6

You can use the ternary operator and isset(). We're checking if the variable and key are set (the part before ?). If it is set, use that variable (the part between ? and :) and if not, set to blank (the part after :).

Ternary operator works like:

$variable = ( comparison ? if true : if false );

Thus:

$show_insert = array(
    'title' => ( isset( $show_data['title'] ) ? $show_data['title'] : '' ),
    'thetvdb_id' => ( isset( $show_data['tvdbid'] ) ? $show_data['tvdbid'] : '' ),
    [etc.]
);
Patrick Moore
  • 13,251
  • 5
  • 38
  • 63
  • 3
    With PHP 5.3+, it's now possible to write `$show_data['title'] ?: ''`. Faster and easier. – BMN Oct 20 '14 at 14:57
  • Didn't know I could just use it this way, thanks. – Callombert Oct 20 '14 at 15:05
  • @YellowBird That will still generate a warning when `$show_data['title']` is not set. – jeroen Oct 20 '14 at 15:06
  • @jeroen Well, I didn't paste the `isset` part... my comment was just a syntax annotation. – BMN Oct 20 '14 at 15:07
  • @YellowBird If you use `isset`, the returned value from the ternary expression will be a boolean. – jeroen Oct 20 '14 at 15:09
  • @jeroen Oh yes you're right, my bad... – BMN Oct 20 '14 at 15:11
  • 1
    I dont see how this answers the question? As the array appears to be column => value pairs should empty columns not be stripped or better yet never put into the array in the first place? I suppose it depends on what the column expects and only the op would know that, but to me that would be the preference, remove or never add if the column allows. – CodingInTheUK Nov 03 '17 at 21:36
  • 2
    With PHP 7 you can use a null coalescing operator like so `$show_data['title'] ?? ''` – Chris Jan 24 '18 at 11:40
2

Expanding on the other answer:

I would use a function to simplify all that typing, also, I would use empty() instead of isset() but if you're just setting it to an empty string I suppose that part doesn't matter much.

function checkVal($val, $show_data){ 
    return empty($show_data[$val]) ? "" : $show_data[$val]; 
} 

$show_insert = array( 
    'title' => checkVal('title',$show_data), 
    'thetvdb_id' => checkVal('tvdbid',$show_data) 
);
I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
2
if (isset($show_data['title']) {
$show_insert[title] = $show_data['title'];
}

This basically means that if $show_data[title] has been initialized then it will add it to the $show_insert array under the key 'title', if it has not been set, nothing will happen so there will be no array key 'title'

or for large arrays you could have:

public function check_set($array, $variable = null, $key_name)
    if ($variable != null) {
    $array[$key_name] = $variable;
    }


check_set($show_insert, $show_data[title], $title = '"title"');
Eujinks
  • 390
  • 1
  • 16
  • 1
    Technically, this would work, but with large arrays, you will duplicate the condition for each array element ? – BMN Oct 20 '14 at 15:05
  • Good point, check edits – Eujinks Oct 20 '14 at 15:12
  • Upvoting because this is the only answer that works if you send the array to a database or an api. An empty, but existing, field will empty any previous value in the database, wherears a completely missing field will (usually) ignore any updates and only modify values that are actually set. – DocWeird Feb 20 '20 at 09:44