It seems that your form submits a json object to the server so use json_decode with second parameter set to true to convert it to an array.

$php_array=  json_decode($_POST["questionandanswers"],true)
Answer from Osama on Stack Overflow
๐ŸŒ
Wtools
wtools.io โ€บ convert-js-object-to-php-array
Convert Javascript object to PHP array Online | WTOOLS
Free tool for online converting Javascript literal object into PHP array, generate array from JS object quickly.
๐ŸŒ
Jsontophp
jsontophp.com
Convert JSON Object to PHP Array Online
JSON to PHP array converter online - Convert the given JSON object or Array into beautified PHP array that can be used instantly into your PHP file as a PHP array
๐ŸŒ
ConvertSimple
convertsimple.com โ€บ home โ€บ convert javascript object to php
Convert Javascript Object to PHP - ConvertSimple.com
June 18, 2022 - Use this Javascript Object to PHP converter tool by pasting or uploading Javascript Object in the left box below.
People also ask

How to use the Convert JSON Array to PHP Array Online for free?
Upload your JSON file, paste data, or extract from web pages using our free online table converter. Convert JSON to PHP instantly with real-time preview and advanced editing. This JSON to PHP converter lets you copy or download your PHP output right away.
๐ŸŒ
tableconvert.com
tableconvert.com โ€บ home โ€บ json array to php array converter online - free tool
JSON Array to PHP Array Converter Online - Free Tool
What is PHP Array format?
PHP is a popular server-side scripting language, with arrays being its core data structure, widely used in web development and data processing.
๐ŸŒ
tableconvert.com
tableconvert.com โ€บ home โ€บ json array to php array converter online - free tool
JSON Array to PHP Array Converter Online - Free Tool
How to convert JSON to PHP online?
To convert JSON to PHP, paste your JSON data or upload a file, then review the table in the editor. The JSON to PHP converter updates the result as you edit, so you can convert JSON to PHP and copy or download the final PHP output.
๐ŸŒ
tableconvert.com
tableconvert.com โ€บ home โ€บ json array to php array converter online - free tool
JSON Array to PHP Array Converter Online - Free Tool
๐ŸŒ
Wtools
wtools.io โ€บ convert-php-array-to-js-object
Convert PHP array to Javascript object Online - wtools.io
Free tool for online converting Javascript literal object into PHP array, generate array from JS object quickly.
๐ŸŒ
Code Beautify
codebeautify.org โ€บ json-to-php-array-converter
JSON to PHP Array Converter to convert JSON to PHP Array Variable
This JSON to PHP Array converter utility can convert JSON in JavaScript Object Notation format to PHP arrays. This is an online tool and does not require any installation or plugin.
๐ŸŒ
HTML Code Generator
html-code-generator.com โ€บ tools โ€บ json-to-php-array
JSON To PHP Array Converter Online - HTML Code Generator
Convert JSON or JavaScript object code into a clean, ready-to-use PHP associative array instantly.
Find elsewhere
๐ŸŒ
Table Convert
tableconvert.com โ€บ home โ€บ json array to php array converter online - free tool
JSON Array to PHP Array Converter Online - Free Tool
July 28, 2025 - Before exporting PHP Array, review the options when the result will be used in an API, database, spreadsheet, document, or static site. ... TableConvert runs table editing and standard format conversion in your browser. You can inspect the converted data before copying or downloading the final file, without installing desktop software.
Top answer
1 of 2
1

I had to reformat your JSON which was not valid (checked on https://jsonlint.com/).

I voluntarily used multiple str_replace() so you better understand the process, however you can optimize the code below by making multiple replacements at the same time within the same str_replace().

This works:

<?php

$to_decode = "var BASE_DATA = {
userInfo: {
  id: 0,
  userName: 'no-needed',
  avatarUrl: 'no-needed',
  isPgc: false,
  isOwner: false
},
headerInfo: {
  id: 0,
  isPgc: false,
  userName: 'no-needed',
  avatarUrl: 'no-needed',
  isHomePage: false,
  crumbTag: 'no-needed',
  hasBar: true
},
articleInfo: 
{
  title: 'needed',
  content: 'needed',
  groupId: 'needed',
  itemId: 'needed',
  type: 1,
  subInfo: {
    isOriginal: false,
    source: 'needed',
    time: 'needed'
  },
  tagInfo: {
    tags: [{\"name\":\"no-needed 1\"},{\"name\":\"no-needed 2\"},{\"name\":\"no-needed 3\"}],
    groupId: 'no-needed',
    itemId: 'no-needed',
    repin: 0,
  },
  has_extern_link: 0,
  coverImg: 'no-needed'
},
commentInfo:
{
  groupId: 'no-needed',
  itemId: 'no-needed',
  comments_count: 151,
  ban_comment: 0
},};";

/* Clean JSON and encapsulate in brackets */
$to_decode = str_replace('var BASE_DATA = {', '', $to_decode);
$to_decode = '{'.substr($to_decode, 0, -3).'}';

/* Remove spaces, tabs, new lines, etc. */
$to_decode = str_replace(' ', '', $to_decode);
$to_decode = str_replace("\n", '', $to_decode);
$to_decode = str_replace("\t", '', $to_decode);
$to_decode = str_replace("\r", '', $to_decode);

/* Encapsulate keys with quotes */
$to_decode = preg_replace('/([a-z_]+)\:/ui', '"{$1}":', $to_decode);
$to_decode = str_replace('"{', '"', $to_decode);
$to_decode = str_replace('}"', '"', $to_decode);
$to_decode = str_replace('\'', '"', $to_decode);

/* Remove unecessary trailing commas */
$to_decode = str_replace(',}', '}', $to_decode);

echo '<pre>';
var_dump(json_decode($to_decode));

Result using print_r :

(I added true/false for clarity, these will only show using var_dump() otherwise)

stdClass Object
(
    [userInfo] => stdClass Object
        (
            [id] => 0
            [userName] => no-needed
            [avatarUrl] => no-needed
            [isPgc] => false
            [isOwner] => false
        )

    [headerInfo] => stdClass Object
        (
            [id] => 0
            [isPgc] => false
            [userName] => no-needed
            [avatarUrl] => no-needed
            [isHomePage] => false
            [crumbTag] => no-needed
            [hasBar] => true
        )

    [articleInfo] => stdClass Object
        (
            [title] => needed
            [content] => needed
            [groupId] => needed
            [itemId] => needed
            [type] => 1
            [subInfo] => stdClass Object
                (
                    [isOriginal] => false
                    [source] => needed
                    [time] => needed
                )

            [tagInfo] => stdClass Object
                (
                    [tags] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [name] => no-needed1
                                )

                            [1] => stdClass Object
                                (
                                    [name] => no-needed2
                                )

                            [2] => stdClass Object
                                (
                                    [name] => no-needed3
                                )

                        )

                    [groupId] => no-needed
                    [itemId] => no-needed
                    [repin] => 0
                )

            [has_extern_link] => 0
            [coverImg] => no-needed
        )

    [commentInfo] => stdClass Object
        (
            [groupId] => no-needed
            [itemId] => no-needed
            [comments_count] => 151
            [ban_comment] => 0
        )

)
2 of 2
0

thank @Bruno Leveque for your idea.

I edited your code like below so that it run well:

  1. I changed $to_decode = str_replace(' ', '', $to_decode); to $to_decode = preg_replace('/[\n| |\s]{2,}/',' ',$to_decode);, that means all 1+ space will be changed to 1 space. because sometimes we need space, like: content: '

  2. I added $to_decode = str_replace("'", '"', $to_decode); before your comment code /* Encapsulate keys with quotes */

  3. changed $to_decode = preg_replace('/([a-z_]+)\:/ui', '"{$1}":', $to_decode); to $to_decode = preg_replace('/([a-z_]+)\: /ui', '"$1":', $to_decode); (one more space there); and commented //$to_decode = str_replace('"{', '"', $to_decode); and //$to_decode = str_replace('}"', '"', $to_decode);

  4. added one more code:$to_decode = str_replace(", }", '}', $to_decode);

so my final code is :

because @Bruno Leveque does not know the exactly content of "needed" and "no-needed", so thank you for idea.

seems no perfect way....

๐ŸŒ
SitePoint
sitepoint.com โ€บ php
Convert Posted Javascript Array into PHP Array - PHP - SitePoint Forums | Web Development & Design Community
January 16, 2012 - I need to be able to pass the javascript arrays through the POST method so i can then use them on the server. Is this possible? ... When you post an array from your JS code the PHP core should automatically understand it, if you use var_dump and exit the script the response should be a dump of the sent array with all itโ€™s keys and values intact.
๐ŸŒ
Jonathan Suh
jonsuh.com โ€บ blog โ€บ convert-loop-through-json-php-javascript-arrays-objects
Convert and Loop through JSON with PHP and JavaScript Arrays/Objects โ€” Jonathan Suh
September 30, 2014 - The second parameter accepts a boolean that when set as true, tells it to return the objects as associative arrays. You can learn more about the json_decode function from PHPโ€™s documentation.
๐ŸŒ
X
x.com โ€บ wtools_io
Web Tools (@wtools_io) / Posts / X
Convert JS object to PHP array #convert #converter #PHP #php8 #javascript #js #transformation #web #tool #wtools #developers ... Free tool for online converting Javascript literal object into PHP array, generate array from JS object quickly.
๐ŸŒ
Minifier
minifier.org โ€บ json-to-php
Convert JSON to PHP Array Online
Minify JS and CSS online, or include the minifier in your project for on-the-fly compression.
๐ŸŒ
YouTube
youtube.com โ€บ watch
Javascript Object to a PHP Object - YouTube
I show you a function I made that lets you use your javascript object as a php object. Basically it takes your JS object and turns into a JSON string that P...
Published: January 16, 2010
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 35902396 โ€บ how-can-i-convert-a-javascript-object-to-an-array
php - How can I convert a JavaScript object to an array - Stack Overflow
I have the following javascript object, in an array. I am using PHP's json_encode to get this value [ { January=100}, { February=100} ] I am trying to convert the following objects to ["January...