@ 2022-10-21T19:38:01Z <?php
declare(strict_types=1);
function findNetworkWithIPAddress(array $payload, string $ipAddress): array
{
$network = array_values(array_filter($payload, function (array $networkInfo) use ($ipAddress) {
$address = $networkInfo['address'] ?? '';
/** for PHP < 8.0 */
// return substr($address, 0, strlen($ipAddress)) === $ipAddress;
/** for PHP >= 8.0 */
return str_starts_with($address, $ipAddress);
}));
return reset($network) ?: [];
}
$payload = [
[
".id" => "*1",
"address" => "10.9.4.109/24",
"network" => "10.9.4.0",
"interface" => "ether1",
"actual-interface" => "ether1",
"invalid" => false,
"dynamic" => false,
"disabled" => false,
],
[
".id" => "*2",
"address" => "10.9.4.209/24",
"network" => "10.9.4.0",
"interface" => "ether2",
"actual-interface" => "ether2",
"invalid" => false,
"dynamic" => false,
"disabled" => false,
],
];
$network = findNetworkWithIPAddress($payload, '10.9.4.209');
$interfaceName = $network['interface'] ?? '';
var_dump($interfaceName);
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.0.1 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.6 string(6) "ether2"
Output for 7.4.0 - 7.4.33 Fatal error: Uncaught Error: Call to undefined function str_starts_with() in /in/EYugm:14
Stack trace:
#0 [internal function]: {closure}(Array)
#1 /in/EYugm(15): array_filter(Array, Object(Closure))
#2 /in/EYugm(44): findNetworkWithIPAddress(Array, '10.9.4.209')
#3 {main}
thrown in /in/EYugm on line 14
Process exited with code 255 . preferences:dark mode live preview
130.98 ms | 401 KiB | 121 Q