Answers for "php array to json object"

PHP
10

php convert array to json object

$myArr = array("apple", "banana", "mango", "jackfruit");

$toJSON = json_encode($myArr);

echo $toJSON;
Posted by: Guest on June-18-2020
15

json php

//Json Encode

$person = array( 
    "name" => "KINGASV", 
    "title" => "CTO"
); 
$personJSON=json_encode($person);//returns JSON string

//Json Decode

$personJSON = '{"name":"KINGASV","title":"CTO"}';

$person = json_decode($personJSON);

echo $person->name; // KINGASV
Posted by: Guest on July-16-2020
0

json to array php

//2 ways
  //this is for string from $_REQUEST,$_POST to array
$jsonText = $_REQUEST['myJSON'];
$decodedText = html_entity_decode($jsonText);
$myArray = json_decode($decodedText, true);

//this is for json to array
$assosiative_array = json_decode(json_encode($jsonText),true);
Posted by: Guest on July-26-2021
1

json to array php

<?php

$myArr = '{
  "neworder": {
    "-newfolder": "NO",
    "auth": {
      "-extra": "8",
      "-login": "login",
      "-pass": "pass"
    },
    "order": {
      "-orderno": "111111",
      "barcode": "111111",
      "sender": {
        "company": "Ministry of Internal Affairs",
        "person": "I. I. Ivanov",
        "phone": "123-45-67",
        "town": "Saint-Petersburg",
        "address": "Petrovka Str., 38, room 35",
        "date": "2021-03-22",
        "time_min": "09:00",
        "time_max": "14:00"
      },
      "receiver": {
        "company": "Ministry of Internal Affairs",
        "person": "Tom Wale",
        "phone": "123-45-67",
        "zipcode": "125480",
        "town": {
          "-regioncode": "78",
          "-country": "RU",
          "#text": "Saint-Petersburg"
        },
        "address": "Petrovka Str., 38, room 35",
        "pvz": "124",
        "inn": "1112223335",
        "date": "2021-03-22",
        "time_min": "09:00",
        "time_max": "14:00",
        "deliveryPIN": "1234",
        "coords": {
          "-lat": "55.680327",
          "-lon": "37.604456"
        }
      },
      "return": "NO",
      "weight": "5.1",
      "return_weight": "5.1",
      "quantity": "2",
      "paytype": "CASH",
      "service": "2",
      "return_service": "1",
      "type": "3",
      "return_type": "3",
      "courier": "22",
      "price": "387.5",
      "deliveryprice": {
        "-VATrate": "20",
        "#text": "150"
      },
      "inshprice": "387.5",
      "receiverpays": "NO",
      "discount": "120",
      "enclosure": "Kids toys",
      "instruction": "Check in the presence of the buyer, sign acceptance certificate",
      "department": "Accounting",
      "pickup": "NO",
      "acceptpartially": "NO",
      "costcode": "cc12345",
      "items": {
        "item": [
          {
            "-extcode": "abc123",
            "-quantity": "1",
            "-mass": "0.2",
            "-retprice": "37.5",
            "-VATrate": "0",
            "-barcode": "2345625213125",
            "-textArticle": "1",
            "-article": "1",
            "-volume": "3",
            "-origincountry": "AUT",
            "-GTD": "321546654",
            "-excise": "15.20",
            "-suppcompany": "LLC \"Miller and Partners\"",
            "-suppphone": "79161234567",
            "-suppINN": "1112223334",
            "-governmentCode": "11223311",
            "#text": "Race car"
          },
          {
            "-extcode": "abc124",
            "-quantity": "2",
            "-mass": "2",
            "-retprice": "100",
            "-inshprice": "100",
            "-VATrate": "10",
            "-barcode": "4645625213138",
            "-article": "2",
            "-length": "10",
            "-width": "20",
            "-height": "30",
            "-origincountry": "004",
            "#text": "Princess castle"
          },
          {
            "-extcode": "abc125",
            "-quantity": "3",
            "-mass": "0.3",
            "-retprice": "50",
            "-inshprice": "50",
            "-barcode": "2345625213126",
            "-itemcode": "44123",
            "-article": "3",
            "-type": "1",
            "#text": "Clay mass"
          }
        ]
      },
      "packages": {
        "package": [
          {
            "-strbarcode": "ORD0000001",
            "-mass": "1",
            "-message": ""
          },
          {
            "-strbarcode": "ORD0000002",
            "-mass": "2.5",
            "-message": "",
            "-length": "10",
            "-width": "20",
            "-height": "30"
          }
        ]
      },
      "deliveryset": {
        "-above_price": "100",
        "-return_price": "1000",
        "-VATrate": "10",
        "below": [
          {
            "-below_sum": "500",
            "-price": "500",
            "-self-closing": "true"
          },
          {
            "-below_sum": "2000",
            "-price": "300",
            "-self-closing": "true"
          }
        ]
      },
     
      "overall_volume": "81",
      "userid": "123",
      "groupid": "456"
    }
  }
}';
    print_r(json_decode($myArr)); ?>
Posted by: Guest on September-15-2021
0

php object to json

function getJsonData(){
    $var = get_object_vars($this);
    foreach ($var as &$value) {
        if (is_object($value) && method_exists($value,'getJsonData')) {
            $value = $value->getJsonData();
        }
    }
    return $var;
}
Posted by: Guest on July-04-2021

Browse Popular Code Answers by Language