PHP How to add backslash inside array of strings?

PHPServer Side ProgrammingProgramming

To add backslash inside array of strings, use json_encode().

Let’s say the following is our array −

$value = [
   "ADD", "REMOVE","SELECT","MODIFY"
];

We want the output with backslash inside array of strings i.e −

"[\"ADD\",\"REMOVE\",\"SELECT\",\"MODIFY\"]"

Example

The PHP code is as follows −

 Live Demo

<!DOCTYPE html>
<html>
<body>
<?php
   $value = [
      "ADD", "REMOVE","SELECT","MODIFY"
   ];
   echo json_encode(json_encode($value)) . "
"; ?> </body> </html>

Output

This will produce the following output

"[\"ADD\",\"REMOVE\",\"SELECT\",\"MODIFY\"]"
raja
Updated on 13-Oct-2020 06:52:24

Advertisements