0

I have array and i need return format: [1,2,3]

$a = [1,2,3];
print_r( $a );
// result: Array ( [0] => 1 [1] => 2 [2] => 3 )

I had used json_encode but it only return "[1,2,3]" . And I need return $a = [1,2,3];

So how to process return that format [1,2,3]

HUng
  • 19
  • 5
  • It looks like the array is already in the desired format. Are you misinterpreting the `print_r` formatting? Do you mean you want to print it like `echo "$a = [" . implode(', ', $a) . "]";`? Or do you want the variable name `$a` in the output? – ggorlen Aug 24 '18 at 03:20
  • I had used json_encode but it only return "[1,2,3]" . And I need return $a = [1,2,3]; – HUng Aug 24 '18 at 03:23
  • See also : https://stackoverflow.com/questions/255312/how-to-get-a-variable-name-as-a-string-in-php – ggorlen Aug 24 '18 at 03:25
  • But i have more issue. Data origin is "1,2,3" how to convert to [1,2,3] – HUng Aug 24 '18 at 03:33
  • `explode(",", $data);` – ggorlen Aug 24 '18 at 03:37
  • I tried your way and it returns [1,2,3]. But when I create an object: $ newObj = new stdclass; $newObj -> attr = $a; It return "[1,2,3]" – HUng Aug 24 '18 at 03:46
  • It seems like you keep moving the goalpost. You'll need to edit your question to clearly state what your expected input and output is and the transformation you're trying to perform, and where you're stuck. Otherwise, I'm pretty confused about what you're trying to accomplish. – ggorlen Aug 24 '18 at 04:11
  • Yeah, also, give more info, WHY do you need it this way, where are you gonna use this array is not clear what you need neither what you need it for. – Erubiel Aug 24 '18 at 04:12

0 Answers0