我有下面的数组,我需要根据[location]键拆分成更小的数组(所以我想要一个.co.uk数组和一个.com数组).[location]键不限于.co.uk或.com.
任何帮助表示赞赏.
[22] => Array
(
[query] => tttt
[location] => .co.uk
[x] => 1292889600
[y] => 1
[fullurl] => http://www.tttt.com/
)
[20] => Array
(
[query] => tttt
[location] => .co.uk
[x] => 1292976000
[y] => 1
[fullurl] => http://www.tttt.com/
)
[21] => Array
(
[query] => tttt
[location] => .com
[x] => 1292976000
[y] => 1
[fullurl] => http://www.tttt.com/
)
[19] => Array
(
[query] => tttt
[location] => .co.uk
[x] => 1293062400
[y] => 1
[fullurl] => http://www.tttt.com/
)
[18] => Array
(
[query] => tttt
[location] => .com
[x] => 1293062400
[y] => 1
[fullurl] => http://www.tttt.com/
)
[17] => Array
(
[query] => tttt
[location] => .co.uk
[x] => 1293148800
[y] => 1
[fullurl] => http://www.tttt.com/
)
[16] => Array
(
[query] => tttt
[location] => .com
[x] => 1293148800
[y] => 1
[fullurl] => http://www.tttt.com/
)
[14] => Array
(
[query] => tttt
[location] => .com
[x] => 1293235200
[y] => 1
[fullurl] => http://www.tttt.com/
)
Run Code Online (Sandbox Code Playgroud)
你可以这样做:
$byLocation = array();
foreach ($arr as $key => $item) {
if (!isset($byLocation[$item['location']])) {
$byLocation[$item['location']] = array();
}
$byLocation[$item['location']][$key] = $item;
}
Run Code Online (Sandbox Code Playgroud)
然后例如$byLocation['.co.uk'][22]
是原始数组的第一项.如果您不想保留原始密钥,只需省略它并使用[]
.
归档时间: |
|
查看次数: |
1926 次 |
最近记录: |