How to convert a PHP object or array into a string

Source: Internet
Author: User
Tags php database scalar
How PHP object or array is converted to string
There are two functions when doing PHP database queries.
Oci_fetch_object and Oci_fetch_array (), return the return value of object and array type, respectively, I need a string type now, how to convert?


$results = "[";
$row = Oci_fetch_array ($stmt); <-here with Oci_fetch_object can also
$results + =??? <-here if you convert the return value $row to a string type, I tried (string) No
$results = "]";

------Optimal Solution--------------------
while ($row = Oci_fetch_array ($stmt)) {
$result [] = $row;
}
$result = Json_encode ($result);
------Other Solutions--------------------
What you might need is json_encode.

------Other Solutions--------------------
Citation: What
you might need is json_encode.

I tried, no ...
In fact, my purpose is to change the return value of the OCI query into a JSON string.
------Other Solutions--------------------
Look at your format, you want to convert object or array into JSON format.
Upstairs positive solution, converted to JSON is Json_encode,json to array is Json_decode
If you want to be able to splice yourself ah. Array into string, use implode directly
------Other Solutions--------------------
Post the landlord's data
------Other Solutions--------------------
$results = "[";
$usercount = 0;
$row = Oci_fetch_array ($stmt);
if ($row) {
$results + = $row;
$usercount + +;
}
while (1) {
$row = Oci_fetch_array ($stmt);
if (! $row) break;

$results + = "," + $row;
$usercount + +;
}
$results + = "]";

This is my code, the $results is 0 instead of a JSON array, where the query is a number of return values
------Other Solutions--------------------
Reference: while
($row = Oci_fetch_array ($stmt)) {
$result [] = $row;
}
$result = Json_encode ($result);

This changes the prompt cannot use a scalar value as an array in, and then $result= ""
------Other Solutions--------------------
You put your original $results = "["; Erase it!
------Other Solutions--------------------
Citation:
you put your original $results = "["; Erase it!

It's erased, only written.
while ($row = Oci_fetch_array ($stmt)) {
$result [] = $row;
}
$result = Json_encode ($result);
This section, and then back to $result.

$result [] = $row; hint cannot use a scalar value as an array in
------Other Solutions--------------------
Citation:
you put your original $results = "["; Erase it!


Yes, I just found out there's a $result definition at the beginning of the function, and it'll be erased.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.