3v4l.org

run code in 300+ PHP versions simultaneously
<?php if ( ! function_exists('subarray_element')) { /** * Gets the sub-array containing a value of certain key present in it * * @param array $arr contains arrays with key => val pairs * @param string $id_key key of the subarray to filter * @param string $id_val value of the corresponding key * @return first element matching the value provided or FALSE * @link https://stackoverflow.com/q/13933454/6225838 * @author CPHPython */ function subarray_element($arr, $id_key, $id_val = NULL) { return current(array_filter( $arr, function ($subarr) use($id_key, $id_val) { if(array_key_exists($id_key, $subarr)) return $subarr[$id_key] == $id_val; } )); } } $list = [ [ 'id' => '3243', 'link' => 'fruits', 'lev' => '1', ], [ 'id' => '6546', 'link' => 'apple', 'lev' => '2', ], [ 'id' => '9348', 'link' => 'orange', 'lev' => '2', ], [ 'id' => '9000', 'link' => 'orange', 'lev' => '2', ] ]; echo 'Matching pair: '.var_export(subarray_element($list, 'id', 9348), TRUE).PHP_EOL.'-------'.PHP_EOL. 'Non-matching pair: '.var_export(subarray_element($list, 'id', 42), TRUE).PHP_EOL.'-------'.PHP_EOL. 'Duplicate pair: '.var_export(subarray_element($list, 'link', 'orange'), TRUE).PHP_EOL.'-------'.PHP_EOL; usort($list, function($f1, $f2){ return $f1['id'] > $f2['id']; }); echo 'Duplicate pair after sortind by id: '.var_export(subarray_element($list, 'link', 'orange'), TRUE);
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Matching pair: array ( 'id' => '9348', 'link' => 'orange', 'lev' => '2', ) ------- Non-matching pair: false ------- Duplicate pair: array ( 'id' => '9348', 'link' => 'orange', 'lev' => '2', ) ------- Deprecated: usort(): Returning bool from comparison function is deprecated, return an integer less than, equal to, or greater than zero in /in/XioRa on line 51 Duplicate pair after sortind by id: array ( 'id' => '9000', 'link' => 'orange', 'lev' => '2', )
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Matching pair: array ( 'id' => '9348', 'link' => 'orange', 'lev' => '2', ) ------- Non-matching pair: false ------- Duplicate pair: array ( 'id' => '9348', 'link' => 'orange', 'lev' => '2', ) ------- Deprecated: usort(): Returning bool from comparison function is deprecated, return an integer less than, equal to, or greater than zero in /in/XioRa on line 51 Duplicate pair after sortind by id: array ( 'id' => '9000', 'link' => 'orange', 'lev' => '2', )
Output for 7.1.0 - 7.1.21, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33
Matching pair: array ( 'id' => '9348', 'link' => 'orange', 'lev' => '2', ) ------- Non-matching pair: false ------- Duplicate pair: array ( 'id' => '9348', 'link' => 'orange', 'lev' => '2', ) ------- Duplicate pair after sortind by id: array ( 'id' => '9000', 'link' => 'orange', 'lev' => '2', )

preferences:
163.16 ms | 402 KiB | 168 Q