按相同值php

时间:2019-09-08 07:56:04

标签: php arrays laravel

我需要从嵌套数组中创建一个新数组,其中最小的一个子数组具有相同的值或被重复。

我尝试在foreach内部检查以比较子数组ID(如果找到了以前的子数组ID,但似乎无法正常工作)。


$finalJobs = array();

       foreach ($CompleteJobs as $index => $jobs) {
            foreach ($jobs['jobs'] as $job) {

                if (!empty($finalJobs)) {
                    foreach ($finalJobs as &$finalJob) {
                        foreach ($finalJob['jobs'] as $Fjob) {
                           if (!empty(array_intersect($Fjob, $job))) {
                             //here is my problem
                            }
                        }
                    }
                } else {
                    $finalJobs[$job['id']] = $job;
                }

            }
        }

这是我的原始数组,我需要在子数组jobs中将id加倍或重复。

array:4 [▼
  0 => array:4 [▼
    "id" => 9
    "position" => 3
    "jobs" => array:2 [▼
      0 => array:21 [▼
        "id" => 2
        "name" => "9987C"
        "plan_id" => null
      ]
      1 => array:21 [▼
        "id" => 3
        "name" => "1009V"
        "plan_id" => null
      ]
    ]
  ]
  1 => array:4 [▼
    "id" => 8
    "position" => 2
    "jobs" => array:1 [▼
      0 => array:21 [▼
        "id" => 2
        "name" => "10bb"
        "plan_id" => null
      ]
      1 => array:21 [▼
        "id" => 4
        "name" => "CC09"
        "plan_id" => null
      ]
    ]
  ]
  ...

我期望的数组必须看起来像:

array:4 [▼
   8 => array:4 [▼
     "id" => 8
     "position" => 2
   ]
    "id" => 9
    "position" => 3
    "jobs" => array:3 [▼
      0 => array:21 [▼
        "id" => 2
        "name" => "01"
        "plan_id" => null
      ]
      1 => array:21 [▼
        "id" => 3
        "name" => "09BB"
        "plan_id" => null
      ]
      3 => array:21 [▼
        "id" => 4
        "name" => "10CB"
        "plan_id" => null
      ]
    ]
  ]

我需要具有某种结合,在这里我检查是否找到相同的id值,并将数组重写为新形式。

0 个答案:

没有答案