如何基于组将数组值合并为单个数组

时间:2015-08-17 09:34:54

标签: php arrays

您正在寻找一种基于应用程序组将多个数组值合并到单个数组中的方法。是否有人可以帮我解决这个问题?

我的阵列:

Array{
         [0]=>Application{
          [id]=>1
          [name]=>facebook
          [group]=>mobile_app
         }
         [1]=>Application{
          [id]=>2
          [name]=>youtube
          [group]=>mobile_app
         }
         [2]=>Application{
           [id]=>3
           [name]=>whatsapp
           [group]=>messenger
         }
         [3]=>Application{
           [id]=>4
           [name]=>skype
           [group]=>messenger
         }
     }

请求的输出:

Array{
[0]=>application{
[id]=>1
[app_name_1]=>facebook
[app_name_2]=>youtube
[group]=>mobile_app
}
[1]=>application{
[id]=>2
[app_name_1]=>whatsapp
[app_name_2]=>skype
[group]=>messenger
}
}

2 个答案:

答案 0 :(得分:2)

假设:

$array等于:

Array{
         [0]=>Application{
          [id]=>1
          [name]=>facebook
          [group]=>mobile_app
         }
         [1]=>Application{
          [id]=>2
          [name]=>youtube
          [group]=>mobile_app
         }
         [2]=>Application{
           [id]=>3
           [name]=>whatsapp
           [group]=>messenger
         }
         [3]=>Application{
           [id]=>4
           [name]=>skype
           [group]=>messenger
         }
     }

因此,对于第一个数组,数组中的每个元素都是Application Object的一个实例,它应该是这样的:

class Application {
    public $id;
    public $name;
    public $group;

    public function __construct($id, $name, $group) {
        $this->id = $id;
        $this->name = $name;
        $this->group = $group;
    }
}

并且,该对象的一些实例组成了数组$array

要按照您想要的方式对其进行格式化,您必须先按照以下方式对其进行排序:

foreach($array as $element) {
    $newAppName = $element->name;
    $newArray[$element->group][] = $element->name;
}

要存储它们的对象,您需要设计一个这样的新类:

class ApplicationObjectTwo {
    public $id;
    public $group;

    public function __construct($id, $group) {
        $this->id = $id;
        $this->group = $group;
    }
}

一旦你这样做,你想创建对象的实例并将它们存储在数组中,如下所示:

$counter = 1;
$counterTwo = 1;
$otherArray = [];

foreach($newArray as $group => $data) {
    $otherArray[] = new ApplicationObjectTwo($counter, $group);
    foreach($data as $app) {
        $varName = "app_name_" . $counterTwo;
        $index = $counter - 1;
        $otherArray[$index]->$varName = $app;
        $counterTwo++;
    }

    $counter++;
    $counterTwo = 1;
}

一旦你这样做,你想要print_r($otherArray)

完整代码的Pastebin:http://pastebin.com/S07BMBuV

答案 1 :(得分:1)

由于youtube位于mobile and信使, I just assumed that was a typo. <br> I have made this example for you br&gt;

,因此我不确定您要求的确切内容

我们首先创建您要插入的数组和数组。

$array = array(
    array(
       "id" => 1,
       "name" => "facebook",
       "group" => "mobile"
   ),
   array(
      "id" => 2,
      "name" => "youtube",
      "group" => "mobile"
   ),
   array(
      "id" => 3,
      "name" => "whatsapp",
      "group" => "messenger"
   ),
   array(
      "id" => 4,
      "name" => "skype",
      "group" => "messenger"
   )
);
$req_array = array(
   "mobile" => array(
   ),
   "messenger" => array(
   )
);

然后我们遍历$array变量中的所有子数组。
在这里,我们取出组名称和组应用名称,然后将名称推送到$our req_array中的组中。

foreach($array as $app){
   $group = $app["group"];
   $name = $app["name"];
   array_push($req_array[$group], $name);
}