0

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']
);
1
  • Fyi, you do not need the sprintf calls. Just use the variables directly! Commented May 20, 2012 at 23:54

5 Answers 5

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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?!!).
Yes, as I mentioned: If the value is NULL it will return false. If that's not a problem for him, isset() is fine.
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.
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.
1

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',

Comments

0

use this

array_key_exists('gas', $somearr);

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

Comments

0

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;
}

1 Comment

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);
0

Use ternary operator:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.