0

I'm trying to implode an array coming back from an axios call and I'm hitting an array to string conversion error.

When I hit the axios call, the payload shows the array like this:

categories: [{id: "1"},{id: "3"}]

then it comes into my PHP file where the function tries to implode:

public function implodeArray(array $categories=null){
   $categoriesTest = empty($categories) ? '' : implode(','$categories);
}

but I'm hitting an array to string conversion error. If I dump the array right before implosion it's in this format:

array:2 [
  0 => array:1 [
    "id" => "1"
  ]
  1 => array:1 [
    "id" => "3"
  ]
]

I'm assuming it can't implode now because the indeces aren't comma separated?

Geoff_S
  • 4,917
  • 7
  • 43
  • 133
  • 2
    It can't implode because it is __multidimensional array__. – u_mulder Feb 03 '22 at 15:27
  • Ah, so it needs to be a single-level array of comma separated values? – Geoff_S Feb 03 '22 at 15:28
  • Comma-separated values can be in string. Array is a collection of values without any commas and separations. – u_mulder Feb 03 '22 at 15:29
  • Try to check if it isn't already an array before exploding it. Apparently, it is a multidimensional array (field set). – Manuel Guzman Feb 03 '22 at 15:30
  • @u_mulder gotcha, so in order to use the implode as is would I just want to make it a single dimensional array then? – Geoff_S Feb 03 '22 at 15:31
  • Yes, just a list of IDs would be easier for you to process. The extra structure isn't adding anything useful because each object only has one property anyway – ADyson Feb 03 '22 at 15:35

0 Answers0