1

I'm trying to figure out how to implode a json array coming from an axios call, within a php file. I currently have this function where I toggle between dumping out the array and then trying to dump the imploded version but that fails stating that it's an invalid argument for a foreach.

PHP file:

public function getResults(array $num = []){
    dd($num);
    dd(implode(", ", array_map(function($obj) { foreach ($obj as $p => $v) { return $p;} }, $num)));
} 

The dd($num) call shows this in my network console:

array:2 [
  0 => "{"number":"115"}"
  1 => "{"number":"135"}"
]

How can I implode this to look like 115,135?

Geoff_S
  • 4,917
  • 7
  • 43
  • 133
  • What and where is `dd()`'s function definition? – GetSet Mar 09 '22 at 18:51
  • It's a dump and die statement, so the first one just dumps the array in my network console. If I comment it out the second one SHOULD dump the comma separated list in my network consoe but in this case it dumps an error – Geoff_S Mar 09 '22 at 18:52
  • can you provide the **json** as a `string` or `encoded associative array` ? – medilies Mar 09 '22 at 18:53
  • @medilies it's coming from an axios call where the array is set via vue, so I'm just showing how it's coming through currently – Geoff_S Mar 09 '22 at 18:54
  • Does this answer your question? [How implode array elements in php?](https://stackoverflow.com/questions/25874932/how-implode-array-elements-in-php) – gguney Mar 09 '22 at 18:58
  • @gguney I tried that before but it prints: "{"number":"115"}','{"number":"135"}" – Geoff_S Mar 09 '22 at 19:01
  • Can you add $num output in your question? – gguney Mar 09 '22 at 19:03

2 Answers2

1

You should use php's built in function json_decode($data) to decode json.

You Will not get it in the format you have show, but as a stdClass by default.

If you oass a 2nd argument to json_decode (true or false) you will get it as and associative array og you are more comdortable with that.

I would have provided a better usage example, but the editor is not coorporating on my mobile device.

Fizk
  • 1,015
  • 1
  • 7
  • 19
1
<?php

function getResults(array $num = []): string
{
    return implode(", ", array_map(fn ($obj) => json_decode($obj)->number , $num));
}


$json = ['{"number":"115"}','{"number":"135"}'];

echo getResults($json);

Test https://3v4l.org/HUmuM

Issues with your code

  1. PHP receives JSON as a string. So you need to decode that string so it makes sense for your code as a data structure.

  2. You have an array of objects. array_map already iterates through the array and exposes the stringified objects inside its scope. So I see no need for the extra loop.

  3. Even if you did decode the object as an associative array you could just do return $obj['number']. ($obj as $p => $v) { return $p;} would always return "number" which is the key.


Fix for the comment feedback

My code is throwing an error at => (I'm running PHP 7.1)

Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ')' in...

https://www.php.net/manual/en/functions.arrow.php

Arrow functions were introduced in PHP 7.4 as a more concise syntax for anonymous functions.

return implode(", ", array_map(function ($obj) {
    return json_decode($obj)->number;
}, $num));
medilies
  • 1,811
  • 1
  • 8
  • 32
  • 1
    Ok this definitely works in that test but my code is throwing an error at => (I'm running PHP 7.1), do you know how that would be conveted? – Geoff_S Mar 09 '22 at 20:02
  • That is probably caused by the usage of the arrorw function `fn()=>`, replace it with the traditional callback format – medilies Mar 09 '22 at 20:03