Home Backend Development PHP Problem How to convert JSON array to string in php

How to convert JSON array to string in php

Apr 20, 2023 pm 01:49 PM

In PHP programming, JSON format data is often used, and converting JSON arrays to strings is a common problem. Below we will introduce how to convert JSON array to string in PHP.

PHP provides a very convenient function json_encode(), which can convert a PHP array into a JSON format string. However, when the PHP array contains multi-dimensional arrays, the json_encode() function cannot satisfy us. Therefore, we need to use a homemade function.

The following is a function that converts a JSON array to a string:

function json_arr2string($arr, $indent=0){
  $str = "";
  $indstr = "";
  for ($i=0; $i<$indent; $i++){
    $indstr .= "    ";
  }
  foreach ($arr as $key => $val){
    if (is_array($val)){
      $str .= $indstr . '"' . $key . '": {' . "\n" . json_arr2string($val, $indent+1) . $indstr . "},\n";
    } else{
      $str .= $indstr . '"' . $key . '": "' . $val . '",' . "\n";
    }
  }
  return "{" . "\n" . substr($str, 0, -2) . "\n" . $indstr . "}";
}
Copy after login

The function has two parameters. The first parameter $arr is the JSON array to be converted to a string. The second parameter has a default value of 0 and is used to process multi-dimensional arrays. The

function first creates an empty string variable $str and a string variable used for indentation $indstr, and then checks each element in the array. Process multidimensional arrays by calling itself recursively until all elements have been processed.

Finally, delete the last two characters of the string variable $str (that is, delete the last comma and the spaces adjacent to it), and add curly braces and newlines to return the final String.

The following is an example that demonstrates how to use the above function to convert a JSON array containing a multidimensional array into a string:

$json_arr = array(
    "name" => "PHP",
    "version" => "7.3.5",
    "extension" => array(
        "json" => "enabled",
        "curl" => "enabled",
        "pdo" => "enabled"
    ),
    "framework" => array(
        "name" => "Laravel",
        "version" => "5.8.18"
    )
);

$json_str = json_arr2string($json_arr);

echo $json_str;
Copy after login

The above code will output the following result:

{
    "name": "PHP",
    "version": "7.3.5",
    "extension": {
        "json": "enabled",
        "curl": "enabled",
        "pdo": "enabled"
    },
    "framework": {
        "name": "Laravel",
        "version": "5.8.18"
    }
}
Copy after login

This Is a string conforming to JSON format, which converts the original JSON array into a string.

Summary: Using the above self-made function json_arr2string() can easily convert JSON arrays containing multi-dimensional arrays into strings. If you have not encountered multi-dimensional arrays in PHP programming, the json_encode() function is sufficient.

The above is the detailed content of How to convert JSON array to string in php. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1262
29
C# Tutorial
1235
24