0

So I'm getting a Undefined index: id error when attempting to add something to a property.

So I have a cURL request that I'm making and grabbing all the data and then outputting it into an array.

Here is the foreach loop:

foreach($offices_array_array as $office_array) {
    print_r2($office_array);
}

Here is the output for that foreach:

Array
(
    [total] => 131
    [per_page] => 500
    [current_page] => 1
    [last_page] => 1
    [prev_page] => 
    [next_page] => 
)
Array
(
    [0] => Array
        (
            [id] => 1

Now when I do this:

foreach($offices_array_array as $office_array) {
    $Office->set_office_id(trim($office_array['id']));
}

I get this error: Notice: Undefined index: id in

Sema
  • 81
  • 6
  • What is `print_r2`? Based on what you said was output the `id` index only exists in the 2nd iteration which is why that error would be thrown on the first iteration. – Dave Oct 07 '19 at 18:11
  • @Dave, how would I be able to target the second iteration? – Sema Oct 07 '19 at 18:12
  • If you change this part of the foreach `as $office_array` to `as $counter => $office_array` the variable `$counter` will tell you how many times you've gone through the foreach. – Dave Oct 07 '19 at 18:17

1 Answers1

1

Seems like the curl response is returning results and metadata about the results as two separate array elements. If this is always the case, all you need to do is replace $offices_array_array with $offices_array_array[1]:

foreach($offices_array_array[1] as $office_array) {
    $Office->set_office_id(trim($office_array['id']));
}
Asrar
  • 401
  • 3
  • 8