如何最好地分离出一个数组


How to best separate out an array?

假设我有一个如下所示的数组。如果我想将下面的数组分开并划分为多个数组,其中包含只有一个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"
  }
}

希望对大家有所帮助。

<?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分组。

PHP没有任何原生的东西可以从数组中取出特定的元素。如果你真的不想使用for循环,有很多Set库或框架的一部分提供了这个功能。

尽管如此,还是有很多PHP数组函数。你可能会把一些人联系在一起来实现你想要的。但是,在我看来,for循环是最直接的。

根据主数组的大小变化,将其划分为单独的数组可能相当困难。这是因为每个数组都需要一个不同的变量。所以,如果主数组的大小可以改变,那么你就不知道你需要多少个不同的变量。

我可以问一下为什么你需要把这个主数组分割成多个数组吗?也许有一个更好的办法来解决你的问题。

试试这样写:

$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可能是你最好的选择。

你是说这样吗?

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