如何最好地分离数组?

时间:2011-08-17 17:36:38

标签: php arrays

假设我有一个如下所示的数组。如果我想将下面的数组分离并分成包含只有一个tl_id的记录的多个数组,我将如何有效地执行此操作?

示例:请注意有两个tl_id(可能有更多):275和328.我想将两个大数组分成两个数组。 array1将包含tl_id为275的所有记录。而array2将包含tl_id为328的所有记录。

我正在考虑使用for循环并将其分离出来,但我希望有一些PHP巫师函数可以用来简化此操作。

array(5) {
  [0] => array(14) {
    ["tl_id"] => int(275)
    ["tl_email"] => string(27) "abc@gmail.com"
    ["tl_first_name"] => string(9) "Jane"
    ["tl_last_name"] => string(6) "Doe"
    ["target_id"] => int(354)
    ["target_first_name"] => string(4) "Rudy"
    ["target_last_name"] => string(5) "Smith"
  }
  [1] => array(14) {
    ["tl_id"] => int(275)
    ["tl_email"] => string(27) "abc@gmail.com"
    ["tl_first_name"] => string(9) "Jane"
    ["tl_last_name"] => string(6) "Doe"
    ["target_id"] => int(354)
    ["target_first_name"] => string(4) "Rudy"
    ["target_last_name"] => string(5) "Smith"
  }
  [2] => array(14) {
    ["tl_id"] => int(275)
    ["tl_email"] => string(27) "abc@gmail.com"
    ["tl_first_name"] => string(9) "Jane"
    ["tl_last_name"] => string(6) "Doe"
    ["target_id"] => int(194)
    ["target_first_name"] => string(5) "Katie"
    ["target_last_name"] => string(4) "Smith"
  }
  [3] => array(14) {
    ["tl_id"] => int(328)
    ["tl_email"] => string(20) "qrf@hotmail.com"
    ["tl_first_name"] => string(6) "John"
    ["tl_last_name"] => string(9) "Smith"
    ["target_id"] => int(219)
    ["target_first_name"] => string(6) "Kelly"
    ["target_last_name"] => string(5) "Smith"
  }
  [4] => array(14) {
    ["tl_id"] => int(328)
    ["tl_email"] => string(20) "qrf@hotmail.com"
    ["tl_first_name"] => string(6) "John"
    ["tl_last_name"] => string(9) "Smith"
    ["target_id"] => int(213)
    ["target_first_name"] => string(5) "Chris"
    ["target_last_name"] => string(5) "Jones"
  }
}

5 个答案:

答案 0 :(得分:1)

我希望这会有所帮助。

<?php
$test = array(
  array(
    "tl_id" => 275,
    "tl_email" => "abc@gmail.com",
    "tl_first_name" => "Jane",
    "tl_last_name" => "Doe",
    "target_id" => 354,
    "target_first_name" => "Rudy",
    "target_last_name" => "Smith",
  ),
  array(
    "tl_id" => 275,
    "tl_email" => "abc@gmail.com",
    "tl_first_name" => "Jane",
    "tl_last_name" => "Doe",
    "target_id" => 354,
    "target_first_name" => "Rudy",
    "target_last_name" => "Smith",
  ),
  array(
    "tl_id" => 278,
    "tl_email" => "abc@gmail.com",
    "tl_first_name" => "Jane",
    "tl_last_name" => "Doe",
    "target_id" => 354,
    "target_first_name" => "Rudy",
    "target_last_name" => "Smith",
  ),
);

$result = array_reduce($test,
    function($result, $item)
    {
        $result[$item["tl_id"]][] = $item;
        return $result;
    }
);
print_r($result);

此函数遍历数组并构建一个新数组,其中项目已按ID分组。

答案 1 :(得分:0)

PHP没有任何原生的采摘数组的某些元素。如果您确实不想使用for循环,则可以使用许多 Set 库或部分框架来提供此功能。

尽管如此,还是有很多PHP Array Functions。您可以将一些串联在一起以实现您想要的效果。但是,IMO,for循环将是最直接的。

答案 2 :(得分:0)

根据此主阵列可能更改的大小,可能很难将其分离为单独的阵列。那是因为你需要为每个单独的数组使用不同的变量。因此,如果主阵列的大小可以改变,那么你就不会知道你需要多少个不同的变量。

请问您为什么需要将此主阵列拆分为多个阵列?这样做可能会有更好的解决方案。

答案 3 :(得分:0)

尝试类似:

$collection = array();
foreach($array as $value) {
        $collection[$value['tl_id']][] = $value; //It's a bad practice, but you 
                                                 //don't need to define the array 
                                                 //before pushing values to it
}

PHP没有您希望的任何特殊数据解析库(至少不适用于这种特定情况)。 foreach可能是您最好的选择。

答案 4 :(得分:0)

你的意思是这样的吗?

foreach($tls as $tl){
    $arrayName = "tl_{$tl['tl_id']}";
    ${$arrayName}[] = $tl;
}