0

hi i have a backend with php in cpanel and i have a problem with one of jsons . this is part of my php code :

    ...

    }elseif ($work == "dollardate") {

    $query3 = "SELECT * FROM tabl_dollar_date";

    $result3 = $connect->prepare($query3);

    $result3->execute();

    $out3 = array();

    while ($row3 = $result3->fetch(PDO::FETCH_ASSOC)) {

        $record3 = array();

        $record3["dollar"] = $row3["dollar"];

        $record3["date"] = $row3["date"];



        array_push($out3, $record3);

    }
    echo json_encode($out3);
}

?>

this code show this in json :

[  
   {  
      "dollar":"15000",
      "date":"1397-12-12"
   }
]

how can remove array from json and show the json like this :

  {  
      "dollar":"15000",
      "date":"1397-12-12"
   }
9
  • What's left is still JSON. Why would you need to remove it or make it separate? Commented Mar 18, 2019 at 12:47
  • 2
    From the code, it seem the array could contain multiple object. How do you plan if got more than one object it return Commented Mar 18, 2019 at 12:48
  • 1
    Are you sure you want that? What if your query returns zero or more than one row? How do you want to represent these cases? Using an array is the simplest way of handling all cases equally. Commented Mar 18, 2019 at 12:50
  • i have problem to use array json in swift if it doesnt have array i more comfortable Commented Mar 18, 2019 at 12:53
  • I am also dealing this same problem, will you please try rtrim and ltrim to remove array bracket Commented Mar 18, 2019 at 12:55

2 Answers 2

2

Easiest way (according his code):

change line

echo json_encode($out3);

to

echo json_encode($out3[0]);
Sign up to request clarification or add additional context in comments.

4 Comments

array_shift() will also do the job - secure.php.net/manual/en/function.array-shift.php
yes, but getting the first element by its index is faster than shifting all others after getting it.
With one element it makes no difference. But it is safer for empty array / not array
Yes, it is correct by this perspective. I've thought like this, there will be always a result from the database.
0

One solution is that if you just want the latest value (in case there are multiple records in the table), then change the SELECT to order by date descending also set LIMIT to 1 to only get the 1 record anyway, and remove the loop to fetch the data and just fetch the 1 record...

$query3 = "SELECT `date`, `dollar`
    FROM `tabl_dollar_date`
    ORDER BY `date` desc
    LIMIT 1";
$result3 = $connect->prepare($query3);
$result3->execute();
$row3 = $result3->fetch(PDO::FETCH_ASSOC);
echo json_encode($row3);

As you know which fields you want from the SELECT, it's good to just fetch those fields rather than always using *. This also means that as the result set only contains the fields your after, you can directly json_encode() the result set rather than extracting the fields from one array to another.

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.