У меня есть 2 массива, $fileArr
и $noteArr
.
$fileArr
- это список файлов. Файл может быть связан с более чем одной историей. Итак, в этом $fileArr
вы видите все файлы в истории, которые мне нужно перечислить. Обратите внимание, что CAR.jpg ([processId]=>111
) связан с [storyId]=>1
и [storyId]=>2
. Таким образом, CAR.jpg будет указан дважды.
Я хочу взять все заметки из $noteArr
и поместить их в $fileArr
, сопоставив [processId
]. Таким образом, каждый экземпляр CAR.jpg будет иметь 2 примечания, а TRUCK.jpg - нет.
Мой текущий $ fileArr
Array
(
[0] => Array
(
[fileName] => CAR.jpg
[processId] => 111
[storyId] => 1
)
[1] => Array
(
[fileName] => CAR.jpg
[processId] => 111
[storyId] => 2
)
[2] => Array
(
[fileName] => TRUCK.jpg
[processId] => 222
[storyId] => 3
)
)
Мой текущий $ noteArr
Array
(
[0] => Array
(
[noteId] => 50
[note] => this is a note
[processId] => 111
)
[1] => Array
(
[noteId] => 51
[note] => and this is also a note
[processId] => 111
)
)
Мой предполагаемый новый массив с примечаниями, помещенными под файл, совпадая с идентификатором процесса
Array
(
[0] => Array
(
[fileName] => CAR.jpg
[processId] => 111
[storyId] => 1
[notes] => Array
(
[50] => Array
(
[noteId] => 50
[note] => this is a note
[processId] => 111
)
[51] => Array
(
[noteId] => 51
[note] => and this is also a note
[processId] => 111
)
)
)
[1] => Array
(
[fileName] => CAR.jpg
[processId] => 111
[storyId] => 2
[notes] => Array
(
[50] => Array
(
[noteId] => 50
[note] => this is a note
[processId] => 111
)
[51] => Array
(
[noteId] => 51
[note] => and this is also a note
[processId] => 111
)
)
)
[2] => Array
(
[fileName] => TRUCK.jpg
[processId] => 222
[storyId] => 3
)
)
Я могу сделать это с помощью кода, который я написал ниже, но не хочу использовать цикл внутри цикла. Есть ли другой способ, которым я могу достичь этого?
Мой текущий код (цикл внутри цикла)
$newArr = array();
$i = 0;
foreach($fileArr as $file){
$newArr[$i] = $file;
if(count($noteArr)>0){
foreach($noteArr as $note){
if($file['processId']==$note['processId']){
$newArr[$i]['notes'][$note['id']] = $note;
}
}
}
$i++;
}