$elements
is an array, which may contain 'x' or 'y' keys, I want to check if they exist, otherwise assign a 0. I'm looking for a clean way to validate it. I'm new to PHP, so I want to make sure my code is created according to PHP best practices.
Input array:
Array
(
[0] => Array
(
)
[1] => Array
(
[x] => 123
)
[2] => Array
(
[x] => 123
[y] => 456
)
[3] => Array
(
[y] => 456
)
)
Code:
foreach ($elements as $element) {
$element_x = $element_y = 0;
if (isset($element['x']))
$element_x = $element['x'];
if (isset($element['y']))
$element_y = $element['y'];
$results[] = sprintf('(%d,%d)', $element_x, $element_y);
}
Is there a cleaner way to do it?