-1
下面是我的JSON結果時,我不更換頻道的順序按時序:PHP - array_search附加與json_encode函數解析問題鍵
"channels": [
{
"id": "1",
"name": "LBC دراما "
},
{
"id": "2",
"name": "KTV Arabe"
},
{
"id": "3",
"name": "KTV Plus"
}
]
現在,當我與返回的值取代數組鍵array_search功能,它帶來與陣列響應的關鍵是問題對我來說:
"channels": {
"0": {
"id": "2",
"name": "KTV Arabe"
},
"1": {
"id": "1",
"name": "LBC دراما "
},
"3": {
"id": "3",
"name": "KTV Plus"
}
}
代碼:
$newChannelsArr['channels'][array_search($channelsArr['channels'][$a]['id'], $data)] = ($channelsArr['channels'][$a]);
我怎樣才能克服從鍵被附加在我的JSON數組?
我的代碼段:
$data values:
Array
(
[0] => 2
[1] => 1
[3] => 3
)
$newChannelsArr = array();
if(isset($data)){
for($a = 0; $a < count($data); $a++){
$kv = array_search($channelsArr['channels'][$a]['id'], $data);
$newChannelsArr['channels'][(int)$kv] = ($channelsArr['channels'][$a]);
}
}
解決方案:
ksort($newChannelsArr['channels']); // sort an array
$arr = array_map('array_values', $arr); // regenerate their keys
這將是一個快速的補丁。
好的,所以在我的代碼中的任何建議的更改? –