从数组中获取ID,在对象数组中找到它并将值推送到对象数组

时间:2017-05-23 15:18:13

标签: php arrays

我有一个名为$formAnswers的数组和一个名为$defaultData的对象数组。

我需要从[Id]获取$formAnswers并在$defaultData中找到以获取特定内容块。然后检查内容块中[InputKeyName]的{​​{1}},并将$formAnswers[Value]推送到$formAnswers

  • $defaultData中可以多次[Id]次。
  • $formAnswers$defaultData只会出现一次。

直到[Id]循环,一切都按预期工作,但从那里它不起作用。我该如何解决这个问题?

这是我的功能

for

以下是public function pushFormAnswersToDefaultData(array $defaultData, array $formAnswers) { foreach ($defaultData["info"] as $defaultDataContent) { foreach ($defaultDataContent as $defaultDataProfile) { if (is_array($defaultDataProfile)) { foreach ($defaultDataProfile as $defaultKey => $defaultDataProfileContent) { //Content Block for($c=0;$c<count($formAnswers);$c++) { if($defaultDataProfileContent->Id == $formAnswers[$c]['Id']) { foreach ($defaultDataProfile[$defaultKey] as $nameKey => $profileDataContentInside) { if ($nameKey == $formAnswers[$c]['InputKeyName']) { $defaultDataProfileContent->$formAnswers[$c]['InputKeyName'] = $formAnswers[$c]['Value']; } } } } } } } } return $defaultData; }

$formAnswers

以下是 $formAnswers = Array ( [0] => Array ( [Id] => 0142643741 [InputKeyName] => FromDate [Value] => 28.03.2017 ) [1] => Array ( [Id] => 5338820142 [InputKeyName] => Menu [Value] => Green Salad ) [2] => Array ( [Id] => 7351265992 [InputKeyName] => Area [Value] => Mc Cully Green ) [3] => Array ( [Id] => 02643029354 [InputKeyName] => Menu [Value] => Fried Rice ) [4] => Array ( [Id] => 9808392996 [InputKeyName] => Description [Value] => Bolognese is super easy and a true classic Italian pasta ) [5] => Array ( [Id] => 02643029354 [InputKeyName] => FromDate [Value] => 04.01.2017 ) )

$defaultData

0 个答案:

没有答案