开发者

How to best separate out an array?

Suppose I have an array such as the one below. If I wanted to separate and divide out the array below into multiple arrays containing records with only one tl_id, how would I go about doing this efficiently?

Example: so notice how there are two tl_id's (there could possibly be more): 275 and 328. I want two separate out that big array into two arrays. array1 would contain all the records that has tl_id's of 275. And array2 would contain all the records with tl_id 328.

I was thinking of a for loop and separating it out that way, but was hoping there's some PHP wizardy function I can use to simplify this action.

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"] => i开发者_开发问答nt(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"
  }
}


I hope this will be of help.

<?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);

This function walks the array and builds a new array where the items have been grouped by id.


PHP does not have anything native that will pluck out certain elements of an array. There are many Set libraries available or part of frameworks that offer this functionality if you really don't want to use a for loop.

Nonetheless, there are a many PHP Array Functions. You may be able to string a few together to achieve what you want. But, IMO, a for loop is going to be the most straighforward.


Depending on how much the size if this main array may change, it could be quite difficult to separate it out into separate arrays. That's because you're going to need a different variable for each individual array. So, if the size of the main array can change, then you aren't going to know how many different variables you need.

May I ask why you need to split this main array into multiple arrays? There may be a better solution to your problem then doing that.


Try something like:

$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 doesn't have any special data parsing libraries that you were hoping for (at least, not for this specific situation). A foreach will probably be your best bet.


You mean something like this?

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜