Get 2D array from API Post request in PHP
P粉057869348
P粉057869348 2023-09-08 10:38:02
0
1
693

I cannot get the two-dimensional array of the API post request. Unable to formulate logic in php to get multiple arrays from JSON format API. The structure of the JSON API request is:

"abc": [{"id": 123, "days": [{"a": 11, "b": 11},{"a":22, "b":22}]} , {"id": 456, "days": [{"a": 33, "b": 33},{"a":44, "b":44}]}

I'm trying this logic for a 2D array to get the ID and the values ​​of A,B which I know are not in the correct format.

foreach ($request->abc as $ids => $id) {
    foreach ($id => array_combine($a, $b)) {
        $value .= $this->helper($id, $a, $b);
    }
}

I have successfully retrieved a single array from the API via this loop:

// 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

reply all(1)
P粉244730625

Through this loop, I called the object of "abc":

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

Then I called the helper function in which I developed a loop for the "days" object:

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

This is the helper2 function where I decode the values ​​of a and b by sending them as arguments:

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

Now I can easily pass the values ​​of a and b as parameters to the helper2 function. Hope someone will find it helpful.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template