while I was searching through some file in a php library, i found some documents like this
a:1:{i:0;a:1:{s:3:"cnt";s:1:"1";}}
This is definitely not JSON. Does any one know what is this? or is it a custom syntax for the guy who wrote the library ?
That is a serialized data and not any programming language syntax.
For your understanding...
<?php
$arr = ['a'=>1,'b'=>2,'cnt'=>5];
echo serialize($arr);
OUTPUT :
a:3:{s:1:"a";i:1;s:1:"b";i:2;s:3:"cnt";i:5;}
Language in Php more info on this link unserialize
This is basically a serailize form of array
of follwoing array
Array
(
[0] => Array
(
[cnt] => 1
)
)
You can get it back by into array
$a = 'a:1:{i:0;a:1:{s:3:"cnt";s:1:"1";}}';
$unserailize_a = (unserialize($a));
To convert an array into string us the Serialize
Related
I am trying to take an array of filenames and output the following...
a:1:{s:4:"docs";a:4:{i:0;a:1:{s:15:"property_imgurl";s:63:"http://wwww.example.com/image1.jpg";}i:1;a:1:{s:15:"property_imgurl";s:63:"http://wwww.example.com/image2.jpg";}i:2;a:1:{s:15:"property_imgurl";s:63:"http://wwww.example.com/image3.jpg";}i:3;a:1:{s:15:"property_imgurl";s:63:"http://wwww.example.com/image4.jpg";}}}
This is what I have so far...
<?php
$serialized_data = serialize(array('http://www.example.com/image1.jpg', 'http://www.example.com/image2.jpg', 'http://www.example.com/image3.jpg', 'http://www.example.com/image4.jpg'));
echo $serialized_data . '<br>';
?>
But this is giving me...
a:4:{i:0;s:34:"http://www.example.com/image1.jpg";i:1;s:34:"http://www.example.com/image2.jpg";i:2;s:34:"http://www.example.com/image3.jpg";i:3;s:34:"http://www.example.com/image4.jpg";}
Where am I going wrong?
There is nothing wrong with the serialized array. You're just not creating the array like you want it to be. PHP can't guess how you really want your array to be, so you have to tell PHP how you want it to be. So what you need to do is to change the input array correctly.
You're giving
array('http://www.example.com/image1.jpg', 'http://www.example.com/image2.jpg', 'http://www.example.com/image3.jpg', 'http://www.example.com/image4.jpg')
and that's completely different from the serialized array how it should be. Your Array needs to look like this
array('docs' => array(array('property_imgurl' => 'http://www.example.com/image1.jpg'), array('property_imgurl' => 'http://www.example.com/image2.jpg'), array('property_imgurl' => 'http://www.example.com/image3.jpg'), array('property_imgurl' => 'http://www.example.com/image4.jpg')))
Look at this eval
You're just missing the array key definitions.
$serialized = array(array('docs' => array(array('property_imgurl' => 'http://www.example.com/image4.jpg'))));
As you can see, each URL has a key of property_imgurl and each of those array is part of a parent array with a key of docs
Here's the eval.in
I not which part I am doing wrong. I couldn't able to fetch this array to display. Can someone please help me with this. I am new to JSON.
Array
(
[0] => [{"id":2,"request_id":2,"message":"wqvewq ewq wq ewq e wqwe qwe ","user_id":1,"created_at":"2014-05-30 16:21:28","updated_at":"2014-05-30 16:21:28"},{"id":3,"request_id":2,"message":"as aS A","user_id":2,"created_at":"2014-05-30 17:18:37","updated_at":"2014-05-30 17:18:37"},{"id":4,"request_id":2,"message":"AS As a","user_id":2,"created_at":"2014-05-30 17:18:43","updated_at":"2014-05-30 17:18:43"}]
[1] => [{"id":1,"request_id":2,"message":"sfsdfds sdfds f ","user_id":2,"created_at":"2014-05-30 17:15:16","updated_at":"2014-05-30 17:15:16"}]
[2] => []
)
The output you have quoted looks like PHP print_r output, and it's certainly not legal JSON.
Perhaps you need the PHP json_encode function, to get real JSON out of your PHP code?
It's not one json string but an array of json strings.
You have to first loop thru the array, parse the json and show the variables that you want in your html with jQuery.
You can find a lot of info on the internet and stackoverflow on this subject.
these are the possibilitys you have
var data = array();
for(var i=0;i<yourArray.length;i++)
data[i] = $.parseJSON(yourArray[i]);
or (untested)
var data = JSON.parse(JSON.stringify({yourArray: yourArray}));
A JSON array has the form:
[[a,b,c],[a,b,c],[a,b,c]]
Is there a better way than split?
No, this is most certainly not the best way to parse JSON. JSON parsers exist for a reason. Use them.
In JavaScript, use JSON.parse:
var input = '[[1,2,3],[1,2,3],[1,2,3]]';
var arrayOfArrays = JSON.parse(input);
In PHP, use json_decode:
$input = '[[1,2,3],[1,2,3],[1,2,3]]';
$arrayOfArrays = json_decode($input);
You do not need to use regular expressions. As has been mentioned, you must first have valid JSON to parse. Then it is a matter of using the tools already available to you.
So, given the valid JSON string [[1,2],[3,4]], we can write the following PHP:
$json = "[[1,2],[3,4]]";
$ar = json_decode($json);
print_r($ar);
Which results in:
Array
(
[0] => Array
(
[0] => 1
[1] => 2
)
[1] => Array
(
[0] => 3
[1] => 4
)
)
If you want to decode it in JavaScript, you have a couple options. First, if your environment is new enough (e.g. this list), then you can use the native JSON.parse function. If not, then you should use a library like json2.js to parse the JSON.
Assuming JSON.parse is available to you:
var inputJSON = "[[1,2],[3,4]]",
parsedJSON = JSON.parse(inputJSON);
alert(parsedJSON[0][0]); // 1
In JavaScript , I think you can use Eval() → eval() method...
please tell me what is below code doing? is it creating array with two values or its creating to string index which will take value later?
var $requiredValues=Array(
'MaxResponses',
'AvailableOnlyIndicator'
);
$requiredValues=Array(
'MaxResponses',
'AvailableOnlyIndicator'
);
It is assigning index 0 to value MaxResponses and 1 to value AvailableOnlyIndicator.
So array with two values.
Note: var keyword won't work in you are under php 5+.
That code isn't valid php. This is:
$requiredValues=Array(
'MaxResponses',
'AvailableOnlyIndicator'
);
That code is creating an array with two elements.
The code should be:
<?php array('MaxResponses', 'AvailableOnlyIndicator'); ?>
And the array would be:
Array
(
[0] => MaxResponses
[1] => AvailableOnlyIndicator
)
It's creating an array with two values, with an automatic numeric index. Syntax is slightly off, but it should work.
I have the following array (in php after executing print_r on the array object):
Array (
[#weight] => 0
[#value] => Some value.
)
Assuming the array object is $arr, how do I print out "value". The following does NOT work:
print $arr->value;
print $val ['value'] ;
print $val [value] ;
So... how do you do it? Any insight into WHY would be greatly appreciated! Thanks!
echo $arr['#value'];
The print_r() appears to be telling you that the array key is the string #value.
After quickly checking the docs, it looks like my comment was correct.
Try this code:
print $arr['#value'];
The reason is that the key to the array is not value, but #value.
You said your array contains this :
Array (
[#weight] => 0
[#value] => Some value.
)
So, what about using the keys given in print_r's output, like this :
echo $arr['#value'];
What print_r gives is the couples of keys/values your array contains ; and to access a value in an array, you use $your_array['the_key']
You might want to take a look at the PHP manual ; here's the page about arrays.
Going through the chapters about the basics of PHP might help you in the future :-)