1

How can I in an API echo results in a new array?

example:

 echo implode('[]',$resultArray);

result:

$resultArray

requested result:

[$resultArray]

4
  • will array_merge work? (php.net/manual/en/function.array-merge.php) Commented Jun 3, 2016 at 8:32
  • 3
    json_encode? Commented Jun 3, 2016 at 8:33
  • var_dump or print_r is it in php side? Commented Jun 3, 2016 at 8:36
  • What do you mean by echo results in a new array? What are the results? Are you consuming an API? Commented Jun 3, 2016 at 9:11

4 Answers 4

3

Since you're implementing an API the best pracrice will be to output your results in a format that's readable and understandable by multiple programming languages. JSON is the way to go. Read about it here and here

To echo an array, or anything not just an array, in PHP in a JSON format use:

echo json_encode($data);

where $data holds the output

Sign up to request clarification or add additional context in comments.

Comments

1

echo json_encode($resultArray);

Assuming you want to keep the data in the response an array to the requester.

If not, your example will work but I'd suggest something easy to parse on the requester's side like a CSV value using commas: echo implode(',', $resultArray);

if you're looking to output the string '[$resultArray]', it'll be something like echo '[$'.print_var_name($resultArray).']'. Not sure why you'd need it like this, but your question is a little bit vague/confusing on exactly what you're looking for.

Comments

0

You could use:

echo "<pre>"; var_dump($array); echo "</pre>";

This way you will get a nice preformatted result of var_dump.

Comments

0

try this

$array = json_decode(json_encode($resultArray), true);

or

Print_r();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.