我有一个数组:-我正在从print_r
函数获得此值。
Array
(
[0] => Array
(
[country] => Austria
[state] => Salzburg
[city] => Salzburg
)
[1] => Array
(
[country] => United States
[state] => California
[city] => San Diego County
)
[2] => Array
(
[country] => United States
[state] => California
[city] => San Francisco City and County
)
[3] => Array
(
[country] => United States
[state] => California
[city] => Santa Clara County
)
[4] => Array
(
[country] => United States
[state] => Texas
[city] => Harris County
)
[5] => Array
(
[country] => United Kingdom
[state] => England
[city] => Greater London
)
[6] => Array
(
[country] => China
[state] =>
[city] => Hong Kong Island
)
[7] => Array
(
[country] => India
[state] => Andhra Pradesh
[city] => Chittoor ,Andhra Pradesh
)
[8] => Array
(
[country] => India
[state] => Andhra Pradesh
[city] => Krishna ,Andhra Pradesh
)
[9] => Array
(
[country] => India
[state] => Andhra Pradesh
[city] => Visakhapatnam ,Andhra Pradesh
)
[10] => Array
(
[country] => India
[state] => Assam
[city] => Kamrup
)
[11] => Array
(
[country] => India
[state] => Bihar
[city] => Aurangabad,Bihar
)
[12] => Array
(
[country] => India
[state] => Goa
[city] => North Goa
)
[13] => Array
(
[country] => India
[state] => Jammu and Kashmir
[city] => Jammu
)
[14] => Array
(
[country] => India
[state] => Karnataka
[city] => Bangalore
)
[15] => Array
(
[country] => India
[state] => Karnataka
[city] => Chamarajanagar ,Karnataka
)
[16] => Array
(
[country] => India
[state] => Karnataka
[city] => Chikkaballapura ,Karnataka
)
[17] => Array
(
[country] => India
[state] => Karnataka
[city] => Chikkamagaluru ,Karnataka
)
[18] => Array
(
[country] => India
[state] => Karnataka
[city] => Hassan ,Karnataka
)
[19] => Array
(
[country] => India
[state] => Karnataka
[city] => Mandya ,Karnataka
)
[20] => Array
(
[country] => India
[state] => Karnataka
[city] => Mysuru ,Karnataka
)
[21] => Array
(
[country] => India
[state] => Karnataka
[city] => Ramanagara ,Karnataka
)
[22] => Array
(
[country] => India
[state] => Karnataka
[city] => Shimoga ,Karnataka
)
[23] => Array
(
[country] => India
[state] => Karnataka
[city] => Udupi ,Karnataka
)
[24] => Array
(
[country] => India
[state] => Karnataka
[city] => Uttara Kannada
)
[25] => Array
(
[country] => India
[state] => Kerala
[city] => Ernakulam
)
[26] => Array
(
[country] => India
[state] => Kerala
[city] => Kollam
)
[27] => Array
(
[country] => India
[state] => Kerala
[city] => Thiruvananthapuram
)
[28] => Array
(
[country] => India
[state] => Kerala
[city] => Wayanad
)
[29] => Array
(
[country] => India
[state] => Maharashtra
[city] => Konkan Division
)
[30] => Array
(
[country] => India
[state] => Maharashtra
[city] => Pune
)
[31] => Array
(
[country] => India
[state] => Punjab
[city] => Amritsar
)
[32] => Array
(
[country] => India
[state] => Sikkim
[city] => South Sikkim ,Sikkim
)
[33] => Array
(
[country] => India
[state] => Sikkim
[city] => West Sikkim ,Sikkim
)
[34] => Array
(
[country] => India
[state] => Tamil Nadu
[city] => Cuddalore ,Tamil Nadu
)
[35] => Array
(
[country] => India
[state] => Tamil Nadu
[city] => Dharmapuri ,Tamil Nadu
)
[36] => Array
(
[country] => India
[state] => Tamil Nadu
[city] => Dindigul ,Tamil Nadu
)
[37] => Array
(
[country] => India
[state] => Tamil Nadu
[city] => Kanchipuram ,Tamil Nadu
)
[38] => Array
(
[country] => India
[state] => Tamil Nadu
[city] => Nagapattinam ,Tamil Nadu
)
[39] => Array
(
[country] => India
[state] => Tamil Nadu
[city] => The Nilgiris ,Tamil Nadu
)
[40] => Array
(
[country] => India
[state] => Tamil Nadu
[city] => Tirunelveli Kattabo
)
[41] => Array
(
[country] => India
[state] => Tamil Nadu
[city] => Viluppuram ,Tamil Nadu
)
[42] => Array
(
[country] => India
[state] => Telangana
[city] => Hyderabad
)
[43] => Array
(
[country] => India
[state] => Telangana
[city] => Rangareddy ,Telangana
)
[44] => Array
(
[country] => India
[state] => Telangana
[city] => Telangana
)
[45] => Array
(
[country] => India
[state] => Telangana
[city] => Yadadri Bhuvanagiri
)
[46] => Array
(
[country] => India
[state] => Uttar Pradesh
[city] => Ghaziabad
)
[47] => Array
(
[country] => India
[state] => Uttar Pradesh
[city] => Meerut
)
[48] => Array
(
[country] => India
[state] => Uttarakhand
[city] => Uttarakhand
)
[49] => Array
(
[country] => India
[state] => West Bengal
[city] => Darjeeling
)
[50] => Array
(
[country] => India
[state] => West Bengal
[city] => Paschim Medinipur
)
[51] => Array
(
[country] => India
[state] => West Bengal
[city] => West Bengal
)
[52] => Array
(
[country] => Thailand
[state] => Bangkok
[city] => Pathum Wan ,Bangkok
)
[53] => Array
(
[country] => Thailand
[state] => Bangkok
[city] => Phasi Charoen ,Bangkok
)
[54] => Array
(
[country] => Myanmar
[state] => Yangon
[city] => Yangon
)
)
问题是我想做的:在下面的输出中请帮忙:-
{
"result": "SUCCESS",
"records": [
{
country : india,
states : [
{
state : karnataka
cities : [bangalore,mysore]
},
{
state : Tamilnadu
cities : [chennai,coimbutore]
}
]
},
{
country : US,
states : [
{
state : ohiao
cities : [city 1,city 2]
},
{
state : california
cities : [city 1,city 2]
}
]
}
]
}
我尝试循环这个。
foreach ($returnData as $key => $value) {
$actual_response[$value['country']][$value['state']][]=$value['city'];
}
答案 0 :(得分:0)
<?php
$returnData = array(array('country'=>'Austria','state' => 'Salzburg','city' => 'Salzburg'),array(
'country' => 'United States',
'state' => 'California',
'city' => 'San Diego County'
),array(
'country' => 'United States',
'state' => 'California',
'city' => 'San Francisco City and County'
),
array
(
'country' => 'United States',
'state' => 'California',
'city' => 'Santa Clara County'
),
array
(
'country' => 'United States',
'state' => 'Texas',
'city' => 'Harris County'
),
array
(
'country' => 'United Kingdom',
'state' => 'England',
'city' => 'Greater London'
),
array
(
'country' => 'China',
'state' =>' ',
'city' => 'Hong Kong Island'
),
array
(
'country' => 'India',
'state' => 'Andhra Pradesh',
'city' => 'Chittoor ,Andhra Pradesh'
),
array
(
'country' => 'India',
'state' => 'Andhra Pradesh',
'city' => 'Krishna ,Andhra Pradesh'
),
array
(
'country' => 'India',
'state' => 'Andhra Pradesh',
'city' => 'Visakhapatnam ,Andhra Pradesh'
),
array
(
'country' => 'India',
'state' => 'Assam',
'city' => 'Kamrup'
),
array
(
'country' => 'India',
'state' => 'Bihar',
'city' => 'Aurangabad,Bihar'
),
array
(
'country' => 'India',
'state' => 'Goa',
'city' => 'North Goa'
),
array
(
'country' => 'India',
'state' => 'Jammu and Kashmir',
'city' => 'Jammu'
),
array
(
'country' => 'India',
'state' => 'Karnataka',
'city' => 'Bangalore'
),
array
(
'country' => 'India',
'state' => 'Karnataka',
'city' => 'Chamarajanagar ,Karnataka'
),
array
(
'country' => 'India',
'state' => 'Karnataka',
'city' => 'Chikkaballapura ,Karnataka'
),
array
(
'country' => 'India',
'state' => 'Karnataka',
'city' => 'Chikkamagaluru ,Karnataka'
),
array
(
'country' => 'India',
'state' => 'Karnataka',
'city' => 'Hassan ,Karnataka'
),
array
(
'country' => 'India',
'state' => 'Karnataka',
'city' => 'Mandya ,Karnataka'
),
array
(
'country' => 'India',
'state' => 'Karnataka',
'city' => 'Mysuru ,Karnataka'
),
array
(
'country' => 'India',
'state' => 'Karnataka',
'city' => 'Ramanagara ,Karnataka'
),
array
(
'country' => 'India',
'state' => 'Karnataka',
'city' => 'Shimoga ,Karnataka'
),
array
(
'country' => 'India',
'state' => 'Karnataka',
'city' => 'Udupi ,Karnataka'
),
array
(
'country' => 'India',
'state' => 'Karnataka',
'city' => 'Uttara Kannada'
),
array
(
'country' => 'India',
'state' => 'Kerala',
'city' => 'Ernakulam'
),
array
(
'country' => 'India',
'state' => 'Kerala',
'city' => 'Kollam'
),
array
(
'country' => 'India',
'state' => 'Kerala',
'city' => 'Thiruvananthapuram'
),
array
(
'country' => 'India',
'state' => 'Kerala',
'city' => 'Wayanad'
),
array
(
'country' => 'India',
'state' => 'Maharashtra',
'city' => 'Konkan Division'
),
array
(
'country' => 'India',
'state' => 'Maharashtra',
'city' => 'Pune'
),
array
(
'country' => 'India',
'state' => 'Punjab',
'city' => 'Amritsar'
),
array
(
'country' => 'India',
'state' => 'Sikkim',
'city' => 'South Sikkim ,Sikkim'
),
array
(
'country' => 'India',
'state' => 'Sikkim',
'city' => 'West Sikkim ,Sikkim'
),
array
(
'country' => 'India',
'state' => 'Tamil Nadu',
'city' => 'Cuddalore ,Tamil Nadu'
),
array
(
'country' => 'India',
'state' => 'Tamil Nadu',
'city' => 'Dharmapuri ,Tamil Nadu'
),
array
(
'country' => 'India',
'state' => 'Tamil Nadu',
'city' => 'Dindigul ,Tamil Nadu'
),
array
(
'country' => 'India',
'state' => 'Tamil Nadu',
'city' => 'Kanchipuram ,Tamil Nadu'
),
array
(
'country' => 'India',
'state' => 'Tamil Nadu',
'city' => 'Nagapattinam ,Tamil Nadu'
),
array
(
'country' => 'India',
'state' => 'Tamil Nadu',
'city' => 'The Nilgiris ,Tamil Nadu'
),
array
(
'country' => 'India',
'state' => 'Tamil Nadu',
'city' => 'Tirunelveli Kattabo'
),
array
(
'country' => 'India',
'state' => 'Tamil Nadu',
'city' => 'Viluppuram ,Tamil Nadu'
),
array
(
'country' => 'India',
'state' => 'Telangana',
'city' => 'Hyderabad'
),
array
(
'country' => 'India',
'state' => 'Telangana',
'city' => 'Rangareddy ,Telangana'
),
array
(
'country' => 'India',
'state' => 'Telangana',
'city' => 'Telangana'
),
array
(
'country' => 'India',
'state' => 'Telangana',
'city' => 'Yadadri Bhuvanagiri'
),
array
(
'country' => 'India',
'state' => 'Uttar Pradesh',
'city' => 'Ghaziabad'
),
array
(
'country' => 'India',
'state' => 'Uttar Pradesh',
'city' => 'Meerut'
),
array
(
'country' => 'India',
'state' => 'Uttarakhand',
'city' => 'Uttarakhand'
),
array
(
'country' => 'India',
'state' => 'West Bengal',
'city' => 'Darjeeling'
),
array
(
'country' => 'India',
'state' => 'West Bengal',
'city' => 'Paschim Medinipur'
),
array
(
'country' => 'India',
'state' => 'West Bengal',
'city' => 'West Bengal'
),
array
(
'country' => 'Thailand',
'state' => 'Bangkok',
'city' => 'Pathum Wan ,Bangkok'
),
array
(
'country' => 'Thailand',
'state' => 'Bangkok',
'city' => 'Phasi Charoen ,Bangkok'
),
array
(
'country' => 'Myanmar',
'state' => 'Yangon',
'city' => 'Yangon'
));
$finalArr = [];
$csK = [];
$ssK = [];
foreach($returnData as $rdk => $rdv){
$csArr = [];
if(!in_array($rdv['country'],$csK)){
$csK[] = $rdv['country'];
$ssK[$rdv['country']][] = $rdv['state'];
$csArr['country'] = $rdv['country'];
$csArr['states'][0]['state'] = $rdv['state'];
$csArr['states'][0]['cities'][0] = $rdv['city'];
$finalArr[$csArr['country']] = $csArr;
}
else{
if(!in_array($rdv['state'],$ssK[$rdv['country']])){
$ssK[$rdv['country']][]=$rdv['state'];
$csArr['state'] = $rdv['state'];
$csArr['cities'][0] = $rdv['city'];
array_push($finalArr[$rdv['country']]['states'],$csArr);
}
else{
foreach($finalArr[$rdv['country']]['states'] as $facsk => $facsv){
if($facsv['state'] == $rdv['state']){
$finalArr[$rdv['country']]['states'][$facsk]['cities'][] = $rdv['city'];
}
}
}
}
}
$finalArr = array_values($finalArr);
echo json_encode(array("result"=>"SUCCESS" ,"records"=>$finalArr) );exit;
?>
您编写的代码不足以得到预期的响应。
答案 1 :(得分:0)
您可以尝试将此代码放在数组下面并运行。
function nested($item)
{
if (count($item) > 1)
{
$key = array_shift($item);
return array($key => nested($item));
}
else
{
return $item;
}
}
$finalArr = array();
foreach($arr as $data)
{
$finalArr = array_merge_recursive($finalArr, nested($data));
}
$finalArr = array_values($finalArr);
echo json_encode(array("result"=>"SUCCESS" ,"records"=>$finalArr) );exit;