Iam writing my code in php and my array looks like this
我在php中编写代码,我的数组看起来像这样
$arr=Array
(
[0] => Array
(
[Business_name] => 1847(Jumeirah)
[Business_id] => 1422
[Business_Locality] => Jumeirah
[Locality_id] => 2
)
[1] => Array
(
[Business_name] => 1847 Mens Salon(Trade Centre)
[Business_id] => 42
[Business_Locality] => Trade Centre
[Locality_id] => 4
)
[2] => Array
(
[Business_name] => 1847 Mens Salon(Mirdif)
[Business_id] => 1565
[Business_Locality] => Mirdif
[Locality_id] => 28
)
[3] => Array
(
[Business_name] => 1847 Mens Salon(City Walk)
[Business_id] => 494
[Business_Locality] => City Walk
[Locality_id] => 77
)
[4] => Array
(
[Business_name] => 1847 Mens Salon(Dubai Marina)
[Business_id] => 44
[Business_Locality] => Dubai Marina
[Locality_id] => 3
))
Now if i want to get data who have Locality_id=2,but if i want to search for data with locality_id=2 and locality_id=3.So when the result set comes back it should only have data for locality_id=2 and locality_id=3. How do i do that.
现在,如果我想获取Locality_id = 2的数据,但是如果我想搜索locality_id = 2和locality_id = 3的数据。那么当结果集返回时,它应该只有locality_id = 2和locality_id = 3的数据。我怎么做。
This is the code i have written till now
这是我到目前为止编写的代码
function search($array, $key, $value)
{
$results = array();
if (is_array($array)) {
if (isset($array[$key]) && $array[$key] == $value) {
$results[] = $array;
}
foreach ($array as $subarray) {
$results = array_merge($results, search($subarray, $key, $value));
}
}
return $results;
}
print_r(search($arr, 'Locality_id', '2'));
2 个解决方案
#1
This'll help you to gain your result
这将帮助您获得结果
$check = array(2,3);
$result_array = array();
$i = 0;
foreach ($arr as $itemKey => $itemValue) {
if (in_array($itemValue['Locality_id'],$check)) {
$result_array[$i]['Business_name'] = $itemValue['Business_name'];
$result_array[$i]['Business_id'] = $itemValue['Business_id'];
$result_array[$i]['Business_Locality'] = $itemValue['Business_Locality'];
$result_array[$i]['Locality_id'] = $itemValue['Locality_id'];
}else{
continue;
}
$i++;
}
print_r($result_array);
Output:
Array ( [0] => Array ( [Business_name] => 1847(Jumeirah) [Business_id] => 1422 [Business_Locality] => Jumeirah [Locality_id] => 2 ) [1] => Array ( [Business_name] => 1847 Mens Salon(Dubai Marina) [Business_id] => 44 [Business_Locality] => Dubai Marina [Locality_id] => 3 ) )
#2
Smells like homework. But whatever. If i understood your question correctly, this should do it:
闻起来像家庭作业。但是无所谓。如果我理解你的问题,应该这样做:
$lookingFor = array(2,3);
foreach($thisArray as $key => $value) {
foreach($lookingFor as $looking) {
if(in_array($looking, $value['Locality_id'])) {
$output[] = $value;
continue;
}
}
}
print_r($output);
I didn't test this yet, my dev is down atm, but it should work or be at least the correct direction. You should be able to adjust the code yourself.
我还没有测试过这个问题,我的开发人员已经失败了,但它应该起作用或至少是正确的方向。您应该能够自己调整代码。
#1
This'll help you to gain your result
这将帮助您获得结果
$check = array(2,3);
$result_array = array();
$i = 0;
foreach ($arr as $itemKey => $itemValue) {
if (in_array($itemValue['Locality_id'],$check)) {
$result_array[$i]['Business_name'] = $itemValue['Business_name'];
$result_array[$i]['Business_id'] = $itemValue['Business_id'];
$result_array[$i]['Business_Locality'] = $itemValue['Business_Locality'];
$result_array[$i]['Locality_id'] = $itemValue['Locality_id'];
}else{
continue;
}
$i++;
}
print_r($result_array);
Output:
Array ( [0] => Array ( [Business_name] => 1847(Jumeirah) [Business_id] => 1422 [Business_Locality] => Jumeirah [Locality_id] => 2 ) [1] => Array ( [Business_name] => 1847 Mens Salon(Dubai Marina) [Business_id] => 44 [Business_Locality] => Dubai Marina [Locality_id] => 3 ) )
#2
Smells like homework. But whatever. If i understood your question correctly, this should do it:
闻起来像家庭作业。但是无所谓。如果我理解你的问题,应该这样做:
$lookingFor = array(2,3);
foreach($thisArray as $key => $value) {
foreach($lookingFor as $looking) {
if(in_array($looking, $value['Locality_id'])) {
$output[] = $value;
continue;
}
}
}
print_r($output);
I didn't test this yet, my dev is down atm, but it should work or be at least the correct direction. You should be able to adjust the code yourself.
我还没有测试过这个问题,我的开发人员已经失败了,但它应该起作用或至少是正确的方向。您应该能够自己调整代码。