Method: 1
use array_key_exists() & check using ternary.
$product['description'] = array_key_exists($value[500]) ? $value[500] : default value" ;
- Disadvantage:
array_key_exists() will iterate over the whole array thus has more time complexity.
Method: 2
use Null coalescing
$product['description'] = $value[500] ?? "default value";
(Null coalescing is introduced in PHP 7)
or
$product['description'] = isset($value[500]) ? $value[500] : "default value";
both will do the same work, just checks for the value for null, but will also return false if the key exists but value in it is null.(you can use !empty()
for that.)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…