Parse JSON String Contents into PHP Array

Parse JSON string contents into PHP Array

It can be done with json_decode(), be sure to set the second argument to true because you want an array rather than an object.

$array = json_decode($json, true); // decode json

Outputs:

Array
(
[id] => 1
[name] => foo
[email] => foo@test.com
)

Parsing JSON File to array in PHP

<?php
$string = file_get_contents("./PHP/JSON/clicks.json");
$json_a = json_decode($string);
$cx = $json_a->cX;
$cy = $json_a->cY;
?>

I hope this help you.

How to convert JSON string to a PHP array?

json_decode expects a valid JSON string for input. Your input is not a JSON string. Your input is an XML element. When you try to json_decode something that is not a JSON string, the decoding will fail.

$foo = json_decode('<ion:test_images/>');

var_dump($foo); // NULL
var_dump(json_last_error_msg()); // "Syntax error"

See http://eval.in/905948

Ionize apparently evals the tag prior to json_decode getting executed, but since you wrapped the element in single quotes, json_decode will just treat the input as a string. So you'd need to use

$foo = json_decode(<ion:test_images/>);

However, the JSON produced from the tag is invalid, too:

{"name":cat,"pic":"cat.png"}{"name":dog,"pic":"dog.png",}

would need to be

[{"name":"cat","pic":"cat.png"},{"name":"dog","pic":"dog.png"}]

So make sure to pass valid JSON, e.g. fix the code that produces the JSON:

 public static function tag_images(FTL_Binding $tag)
{
$images = array();
self::load_model('page_test_model');
$new_details = self::$ci->page_test_model->get_details();

foreach ($new_details as $image) {
if (is_array($image)){
$images[] = $image;
}
}

return json_encode($images, true);
}

Previously, you'd json_encode the images to string and then json_encode that string again. That obviously won't produce a collection then, but just concatenated single objects or arrays.

How to convert JSON string to arrays

This is what you need, json_decode($json,true);

<?php
$json = '[{"name":"Menu","sub":[{"name":"Menu 2","url":"menu-2.php"}]}]';
$array = json_decode($json,1);
print_r($array[0]);
?>

DEMO: https://3v4l.org/JZQCn

OR use it as a parsable string representation of a variable with var_export()

<?php
$json = '[{"name":"Menu","sub":[{"name":"Menu 2","url":"menu-2.php"}]}]';
$array = var_export(json_decode($json,1)[0]);
print($array);
?>

DEMO: https://3v4l.org/rLA9R

How to convert JSON string data into array using PHP?

Try below code you will get result.

<?php
$data=array("createTransactionRequest" => array(
"merchantAuthentication" => array(
"name" => "2bU77DwM",
"transactionKey" => "92x86d7M7f6NHK98"
),
"refId" => "9898989898",
"transactionRequest" => array(
"transactionType" => "authCaptureTransaction",
"amount" => "25",
"payment" => array(
"creditCard" => array(
"cardNumber" => "5424000000000015",
"expirationDate" => "1220",
"cardCode" => "999"
)
)
)
)
);
$data_string = json_encode($data);

$ch = curl_init('https://apitest.authorize.net/xml/v1/request.api');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);

// below is my code
$final_result = json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $result), true );
echo "<pre>";
print_r($final_result);

?>

you just need to use

$output = json_decode(
preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $result), true );

print_r($output);

i have checked it! its working for me.
Hope this will help!

How to convert json data to array in php?

Your data is serialized as well as json encoded. So you have to use:-

json_decodealong with unserialize like below:-

<?php

$data = 's:59:"[{"item_id":"UTILITY CON CERNIERA","qty":1,"points":"110"}]"';
print_r(json_decode(unserialize($data),true));
?>

And

<?php

$data = 's:109:"[{"item_id":"UTILITY CON CERNIERA","qty":1,"points":"110"},{"item_id":"PESA VALIGIA","qty":1,"points":"120"}]"';
print_r(json_decode(unserialize($data),true));
?>

https://eval.in/590757 And https://eval.in/590758

For more reference:-

http://php.net/manual/en/function.json-decode.php

http://php.net/manual/en/function.unserialize.php

How to parse JSON string with array in php

Just access pid with:

$mydata['pid']

You pass true as a second argument for json_decode

When TRUE, returned objects will be converted into associative arrays.

So you have to treat is as an array, not an object.

How to convert JSON string to PHP object or array creation *code*

<?php

$json = '
[
{
"ID": "1",
"Name": "Test One"
},
{
"ID": "2",
"Name": "Test Two"
}
]';

echo '$output = '.var_export(json_decode($json, true), true).';';

Output:

$output = array (
0 =>
array (
'ID' => '1',
'Name' => 'Test One',
),
1 =>
array (
'ID' => '2',
'Name' => 'Test Two',
),
);


Related Topics



Leave a reply



Submit