Reputation: 173
I need to check one user input value is present in between two number type value or not but as per my code its not working. I am explaining my code below.
$post_code=751011
$dataCed=[{"region_id":"4","zip_from":"754346","zip_to":"754346"},{"region_id":"4","zip_from":"754345","zip_to":"754345"},{"region_id":"4","zip_from":"754130","zip_to":"754230"},{"region_id":"3","zip_from":"226012","zip_to":"226025"},{"region_id":"2","zip_from":"751001","zip_to":"751030"},{"region_id":"1","zip_from":"1000","zip_to":"1500"}]
//echo json_encode($dataCed);
$flag=0;
foreach ($dataCed as $key => $value) {
if ($post_code >= (int)$value['zip_from'] && $post_code <= (int)$value['zip_to']) {
$flag=1;
$reg_code=$value['region_id'];
break;
}
}
echo ($flag);exit;
Here I should get the flag value=1
as postcode(i.e-751011
) present within 751001 & 751030
but I am getting the output as 0 which is wrong.So how to fix this code so that I can get the right output.
Upvotes: 0
Views: 48
Reputation: 6529
You can make use of array_filter
to check this as given below.
<?php
$post_code = 751011;
$dataCed = '[{"region_id":"4","zip_from":"754346","zip_to":"754346"},{"region_id":"4","zip_from":"754345","zip_to":"754345"},{"region_id":"4","zip_from":"754130","zip_to":"754230"},{"region_id":"3","zip_from":"226012","zip_to":"226025"},{"region_id":"2","zip_from":"751001","zip_to":"751030"},{"region_id":"1","zip_from":"1000","zip_to":"1500"}]';
$dataCed = json_decode($dataCed);
$result = array_filter($dataCed, function($v1) use ($post_code){
if ((int)$post_code >= (int)$v1->zip_from && (int)$post_code <= (int) $v1->zip_to) {
return $v1;
}
});
$found = count($result) > 0 ? true : false;
var_dump($found); // This gives bool(true)
Upvotes: 0
Reputation: 581
Try the followig code:
$post_code = 751011;
$dataCed = '[{"region_id":"4","zip_from":"754346","zip_to":"754346"},{"region_id":"4","zip_from":"754345","zip_to":"754345"},{"region_id":"4","zip_from":"754130","zip_to":"754230"},{"region_id":"3","zip_from":"226012","zip_to":"226025"},{"region_id":"2","zip_from":"751001","zip_to":"751030"},{"region_id":"1","zip_from":"1000","zip_to":"1500"}]';
$myArray = json_decode($dataCed);
$flag = 0;
foreach ($myArray as $key => $value) {
if ($post_code >= $value->zip_from && $post_code <= $value->zip_to) {
$flag = 1;
$reg_code = $value->region_id;
break;
}
}
echo ($flag);
exit;
Then you should get the exact output. The problem was you need to decode the JSON
array properly.
Upvotes: 0
Reputation: 1118
Something doesn't seem right in the code you provided, but assuming $dataCed
is JSON data, you could do:
<?php
$post_code = 751011;
$dataCed = '[{"region_id":"4","zip_from":"754346","zip_to":"754346"},{"region_id":"4","zip_from":"754345","zip_to":"754345"},{"region_id":"4","zip_from":"754130","zip_to":"754230"},{"region_id":"3","zip_from":"226012","zip_to":"226025"},{"region_id":"2","zip_from":"751001","zip_to":"751030"},{"region_id":"1","zip_from":"1000","zip_to":"1500"}]';
$dataCed = json_decode($dataCed);
$flag = 0;
foreach ($dataCed as $key => $value) {
$value = (array) $value;
if ($post_code >= (int)$value['zip_from'] && $post_code <= (int)$value['zip_to']) {
$flag=1;
$reg_code=$value['region_id'];
break;
}
}
echo ($flag) . "<br>";
echo ($reg_code);
exit;
Upvotes: 1