Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Not very strong in PHP so here is my question:

I am building a simple array to return as json, populating it with data from another array. The $eventarray may have the index gas or may not so I need to check for existing gas index and if it exist get the value if not populate with a default value.

How would I do that the most optimal way?

Again not that strong in PHP.

Here is the array:

 $somearr = array(
       "actiontaken"=>sprintf("%s", $eventarray['desc']),
        "actionname" =>sprintf("%s", $eventarray['name']),
        "type" =>sprintf("%s", $eventarray['type']),
        "subtype" =>sprintf("%s", $eventarray['name']),
        "min" =>sprintf("%s", $eventarray['min']),
        "gas" =>sprintf("%s", $eventarray['gas']),
        "playerId"=>$value['p'],
        "name" =>$value2['name'],
        "race" =>$value2['race']
);
share|improve this question
Fyi, you do not need the sprintf calls. Just use the variables directly! – ThiefMaster May 20 '12 at 23:54

5 Answers

up vote 3 down vote accepted

You can use isset() to check if the element exists (and is not null):

isset($eventarray['gas']) ? $eventarray['gas'] : 'defaultvalue'

Note: In case null is a possible value where you do not want the default value to be used you cannot use isset but have to use array_key_exists($eventarray, 'gas') instead.

share|improve this answer
php doc recommand using array_key_exists instead. I also used to use isset but it seems it return false result on some case (if the key is null?!!). – lcfseth May 20 '12 at 23:54
Yes, as I mentioned: If the value is NULL it will return false. If that's not a problem for him, isset() is fine. – ThiefMaster May 20 '12 at 23:55
Thanks wasn't sure about the syntax in php, it very similar to C# syntax which I use all the time, and yes returning false on null is fine. – Vasile Laur May 21 '12 at 0:06
isset is supposedly quite a bit faster than array_key_exists (although it probably doesn't matter much) and it also has the ability to check several items at once. You can pass in any number of variables and it will only return true if they are all set. That can come in handy sometimes. – Okonomiyaki3000 May 21 '12 at 0:18

You could use a ternary operator to check for its existence, and set a default value.

The way it works is: ( condition ) ? [if true] : [ if false]

...
"gas" => (isset($eventarray['gas'])) ? $eventarray['gas'] : 'default',
share|improve this answer

use this

array_key_exists('gas', $somearr);

http://www.php.net/manual/en/function.array-key-exists.php

share|improve this answer

Initialise your $somearr array first with default values:

$somearr = array(
  'actiontaken' => 'default action',
  'actionname' => 'default action name',
  etc.. );

Then loop through $eventarray:

foreach( $eventarray as $event => $eventval ) {
  $somearr[$event] = $eventval;
}
share|improve this answer
This is a good solution in many cases but I'd recommend using array_merge instead of the loop. $output = array_merge($default_values, $input); – Okonomiyaki3000 May 21 '12 at 0:22

Use ternary operator:

"actiontaken"=>sprintf("%s", (isset($eventarray['desc']) && !empty($eventarray['desc'])) ?$eventarray['desc'] : 'Default Value'),
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.