php convert array to json object
$myArr = array("apple", "banana", "mango", "jackfruit");
$toJSON = json_encode($myArr);
echo $toJSON;
php convert array to json object
$myArr = array("apple", "banana", "mango", "jackfruit");
$toJSON = json_encode($myArr);
echo $toJSON;
string json format to object in php
<?php
// JSON string
$someJSON = '[{"name":"Jonathan Suh","gender":"male"},{"name":"William Philbin","gender":"male"},{"name":"Allison McKinnery","gender":"female"}]';
// Convert JSON string to Array
$someArray = json_decode($someJSON, true);
print_r($someArray); // Dump all data of the Array
echo $someArray[0]["name"]; // Access Array data
// Convert JSON string to Object
$someObject = json_decode($someJSON);
print_r($someObject); // Dump all data of the Object
echo $someObject[0]->name; // Access Object data
?>
php json_decode
$personJSON = '{"name":"Johny Carson","title":"CTO"}';
$person = json_decode($personJSON);
echo $person->name; // Johny Carson
convert object to array php
<?php
class sample {
/* Member variables */
var $var1;
var $var2;
function __construct( $par1, $par2 )
{
$this->var1 = $par1;
$this->var2 = $par2;
}
}
// Creating the object
$myObj = new sample(1000, "second");
echo "Before conversion: \n";
var_dump($myObj);
// Converting object to associative array
$myArray = json_decode(json_encode($myObj), true);
echo "After conversion: \n";
var_dump($myArray);
?>
Output:
Before conversion:
object(sample)#1 (2) {
["var1"]=>
int(1000)
["var2"]=>
string(6) "second"
}
After conversion:
array(2) {
["var1"]=>
int(1000)
["var2"]=>
string(6) "second"
}
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);
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)); ?>
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us