Holen Sie sich ein 2D-Array von der API-Post-Anfrage in PHP
P粉057869348
P粉057869348 2023-09-08 10:38:02
0
1
512

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);
}

P粉057869348
P粉057869348

Antworte allen(1)
P粉244730625

通过这个循环,我调用了“abc”的对象:

foreach ($request->abc as $abc) {
    $value .= $this->helper($abc['id'], $abc['days']);
}

然后我调用了辅助函数,在其中为“days”对象开发了一个循环:

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 函数,我通过将 a 和 b 的值作为参数发送来解码它们:

public function helper2($a, $b)
{
    return 'this is a:' . $a . ', this is b:' . $b;
}

现在我可以轻松地将 a 和 b 的值作为参数传递给 helper2 函数。 希望有人会觉得它有帮助。

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!