3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = Array ( 0 => (object) array ( 'id' => 8, 'book_category' => 'C Program', 'book_id' => 2, 'book_name' => 'C Language', 'book_category_id' => 8, 'book_in_stock' => 5, ), 1 => (object) array ( 'id' => 8, 'book_category' => 'C Program', 'book_id' => 1, 'book_name' => 'C++', 'book_category_id' => 8, 'book_in_stock' => 10, ), '2' => (object) array ( 'id' => 9, 'book_category' => 'English', 'book_id' => 3, 'book_name' => 'Comp Eng', 'book_category_id' => 9, 'book_in_stock' => 5, ), 3 => (object) array ( 'id' => 9, 'book_category' => 'English', 'book_id' => 4, 'book_name' => 'Eng English', 'book_category_id' => 9, 'book_in_stock' => 5, ), ); if( '7.0.0' <= phpversion() ) { echo "with array column:\n"; $key = array_search( 9, array_column( $array, 'id' ) ); } else { function array_column2( $array, $column ) { return array_map ( function( $row ) use( $column ) { return is_object( $row ) ? $row->$column : $row[$column]; } , $array ); } echo "with array column emulation:\n"; $key = array_search( 9, array_column2( $array, 'id' ) ); } echo $key;
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.26, 7.3.0 - 7.3.13, 7.4.0 - 7.4.1
with array column: 2
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
with array column emulation: 2

preferences:
58.14 ms | 407 KiB | 5 Q