Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save shohag-cse-knu/270882ffd6be99c35442808c78b583f0 to your computer and use it in GitHub Desktop.
Save shohag-cse-knu/270882ffd6be99c35442808c78b583f0 to your computer and use it in GitHub Desktop.
Array column search in 2D array and return the index and it's corresponding parameters.
<?php
$arr_dist_sro = array(
array('id'=>127, 'district'=>'field_6', 'sro'=>'field_9'),
array('id'=>128, 'district'=>'field_6', 'sro'=>'field_7'),
array('id'=>133, 'district'=>'field_4', 'sro'=>'field_6'),
array('id'=>136, 'district'=>'field_6', 'sro'=>'field_7'),
array('id'=>145, 'district'=>'field_4', 'sro'=>'field_5')
);
$search_id = 133;
if(array_search($search_id, array_column($arr_dist_sro, 'id')) !== false){
//Index of 133. Such as it's index is 2
$index = array_search($search_id, array_column($arr_dist_sro, 'id'));
$district = $arr_dist_sro[$index]['district'];
$sro = $arr_dist_sro[$index]['sro'];
echo $district." ".$sro;
}
//This wil print the district and sro of 133 id
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment