Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 根据密钥名称将大数组拆分为小数组_Php - Fatal编程技术网

Php 根据密钥名称将大数组拆分为小数组

Php 根据密钥名称将大数组拆分为小数组,php,Php,我有一个数组,下面我需要根据[location]键将其拆分为更小的数组,所以我想要一个.co.uk数组和一个.com数组。[location]键不限于.co.uk或.com 感谢您的帮助 [22] => Array ( [query] => tttt [location] => .co.uk [x] => 1292889600 [y] => 1 [fullurl] => h

我有一个数组,下面我需要根据[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/
    )
您可以这样做:

$byLocation = array();
foreach ($arr as $key => $item) {
    if (!isset($byLocation[$item['location']])) {
        $byLocation[$item['location']] = array();
    }
    $byLocation[$item['location']][$key] = $item;
}
例如,$byLocation['.co.uk'][22]是原始数组的第一项。如果您不想保留原始密钥,只需省略它并使用[]。

您可以这样做:

$byLocation = array();
foreach ($arr as $key => $item) {
    if (!isset($byLocation[$item['location']])) {
        $byLocation[$item['location']] = array();
    }
    $byLocation[$item['location']][$key] = $item;
}

例如,$byLocation['.co.uk'][22]是原始数组的第一项。如果您不想保留原始密钥,只需省略它并使用[]。

我认为您只需按如下方式迭代数组即可:

检查当前位置是否为“已知数组”\u key\u是否存在于$domains数组中。如果它没有添加它$域[]=数组

将当前数据作为新数组添加到$domains数组中的相关位置键下。数组_push$domains[],数组'query'=>XXX,'x'=>


我认为您只需要按如下方式迭代数组:

检查当前位置是否为“已知数组”\u key\u是否存在于$domains数组中。如果它没有添加它$域[]=数组

将当前数据作为新数组添加到$domains数组中的相关位置键下。数组_push$domains[],数组'query'=>XXX,'x'=>


工作完美。必须更改$byLocation[$item['location'][$key]=$val;至$byLocation[$item['location'][$key]=$item;不过,它工作得很好。必须更改$byLocation[$item['location'][$key]=$val;至$byLocation[$item['location'][$key]=$item;虽然