0

I need to send data to the client side in JSON format.

So far, it worked just fine with json_encode as follows:

$sql = "SELECT `card`.`CardID`,`card`.`Text`,... WHERE (`card`.`CardID`= :ItemId)";

$stmt = $dbh->prepare($sql);
$stmt->bindParam(':ItemId', $itemid);   
$stmt->execute();

while ($row = $stmt->fetchObject()) {
    $posts[]=$row;
}

...
...

$res["rows"] = $posts;
$res["maptel"] = $maptelindicator;

echo json_encode($res,JSON_UNESCAPED_UNICODE); 

But now I have a problem. I have a new field (Videofiles) in the DB that is already JSON formatted- stored as the ouput of a SDK function. If I encode this JSON encoded field once more, I get data which is unreadable on the client side..

How can I json_encode all the other data, but skip this specific field, Videofiles?

Here is the output of a sample data structure using print_r:

Array
(
    [rows] => Array
        (
            [0] => stdClass Object
                (
                    [Name] => Company13
                    [CID] => 26
                    [CardID] => 26-000002
                    [Text] => Kleopatra Deluxe Hotel reservations
                    [ImageLink] => 
                    [VideoFiles] => [{"quality":"mobile","type":"video/mp4","width":480,"height":270....}]
                    [ClickDate] => 2015-11-03
                )

        )

    [maptel] => 0
)

Thanks...

1
  • Rather than decoding all but Videofiles it may be easier to iterate over your result and decode Videofiles, then encode the entire array Commented Nov 3, 2015 at 14:14

2 Answers 2

2

You could use json_decode to decode the already encoded field and then add the result in the $res array.

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

2 Comments

Thanks.. I will look for ways to add the fields to the $posts variable one by one and run json_decode before adding this specific field.
For anyone who is interested, following code did the trick: $row=$stmt->fetch(PDO::FETCH_ASSOC)); $posts[]['CardID']=$row["CardID"]; $posts[0]['Text']=$row["Text"]; $posts[0]['VideoFiles']=json_decode($row["VideoFiles"]);
0

Best way to do it delete this section in SQL!

But if you want to delete in php it will have a cost for you and you can use unset for this situation;

foreach ($posts as $post){
    unset($posts['VideoFiles']);
}

$res["rows"] = $posts;
$res["maptel"] = $maptelindicator;

echo json_encode($res,JSON_UNESCAPED_UNICODE); 

1 Comment

In the end, I will need to add this to the json_encoded data structure. I still need to send it accross.. Thanks anyway.

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.