I have data array in php like this,
0 =>
"data" =>
0 =>
"id_data": "P-1234",
"name_data": "data 0 warga 1"
1 =>
"id_data": "P-1235",
"name_data": "data 0 warga 2"
1 =>
"data" =>
0 =>
"id_data": "O-1134",
"name_data": "data 1 warga 1"
1 =>
"id_data": "O-1135",
"name_data": "data 1 warga 2"
2 =>
"id_data": "O-1136",
"name_data": "data 1 warga 3"
How i can find data id_data O-1135 in php? Thanks you ^^
CodePudding user response:
$arr = [
0 => [
"data" =>[
0 => [ "id_data"=>"P-1234", "name_data"=>"data 0 warga 1"],
1 => ["id_data"=> "P-1235","name_data"=> "data 0 warga 2"]
]
],
1 => [
"data" => [
0 => ["id_data"=> "O-1134","name_data"=> "data 1 warga 1"],
1 => ["id_data"=> "O-1135","name_data"=> "data 1 warga 2"],
2 => ["id_data"=> "O-1136", "name_data"=> "data 1 warga 3"],
]
]
];
function find_value_from_arr($arr, $findVal){
foreach($arr as $childArr){
if(( $indx = array_search( $findVal, array_column($childArr['data'], 'id_data')) ) !== FALSE){
return $childArr['data'][$indx];
}
}
return false;
}
print_r(find_value_from_arr($arr,'O-1135'));