Ich kann das 2D-Array für die API-Post-Anfrage nicht abrufen. Es kann keine Logik in PHP formuliert werden, um mehrere Arrays von der JSON-Format-API abzurufen. Die Struktur der JSON-API-Anfrage ist:
"abc": [{"id": 123, "days": [{"a": 11, "b": 11},{"a":22, "b":22}]} , {"id": 456, "days": [{"a": 33, "b": 33},{"a":44, "b":44}]}
Ich versuche diese Logik für ein 2D-Array, um die ID und die Werte von A, B zu erhalten, und ich wei?, dass die Werte nicht im richtigen Format sind.
foreach ($request->abc as $ids => $id) { foreach ($id => array_combine($a, $b)) { $value .= $this->helper($id, $a, $b); } }
Ich habe über diese Schleife erfolgreich ein einzelnes Array von der API abgerufen:
// single array structure from post request "abc": {"single_array_val":[11,12,13,14,15]} foreach ($request->abc as $single_arrays => $single_array) { $value .= $this->helper($single_arrays, $single_array); }
通過(guò)這個(gè)循環(huán),我調(diào)用了“abc”的對(duì)象:
foreach ($request->abc as $abc) { $value .= $this->helper($abc['id'], $abc['days']); }
然后我調(diào)用了輔助函數(shù),在其中為“days”對(duì)象開(kāi)發(fā)了一個(gè)循環(huán):
public function helper($id, $days) { $days_value = ""; foreach ($days as $day) { $days_value .= $this->helper2($day['a'], $day['b']); } return echo 'the id is: '. $id .' and this have days' . $days_value; }
這是 helper2 函數(shù),我通過(guò)將 a 和 b 的值作為參數(shù)發(fā)送來(lái)解碼它們:
public function helper2($a, $b) { return 'this is a:' . $a . ', this is b:' . $b; }
現(xiàn)在我可以輕松地將 a 和 b 的值作為參數(shù)傳遞給 helper2 函數(shù)。 希望有人會(huì)覺(jué)得它有幫助。