Decode sparse json object to php array - php

I can create a sparse php array (or map) using the command:
$myarray = array(10=>'hi','test20'=>'howdy');
I want to serialize/deserialize this as JSON. I can serialize it using the command:
$json = json_encode($myarray);
which results in the string {"10":"hi","test20":"howdy"}. However, when I deserialize this and cast it to an array using the command:
$mynewarray = (array)json_decode($json);
I seem to lose any mappings with keys which were not valid php identifiers. That is, mynewarray has mapping 'test20'=>'howdy', but not 10=>'hi' nor '10'=>'hi'.
Is there a way to preserve the numerical keys in a php map when converting to and back from json using the standard json_encode / json_decode functions?
(I am using PHP Version 5.2.10-2ubuntu6.4.)

json_decode returns an object of type stdClass by default. You access members as properties (i.e., $result->test20). 10 isn't a valid name for a property, which is why you're losing it.
Instead of casting to an array, you can pass true as a second argument to json_decode to make it return an associative array itself:
$mynewarray = json_decode($json, true);
If you do that, $mynewarray[10] will work fine.

What version of PHP? On 5.2 the following program/script
$myarray = array(10=>'hi','test20'=>'howdy');
$json = json_encode($myarray);
$mynewarray = (array) json_decode($json);
var_dump($mynewarray);
Outputs
array(2) {
["10"]=>
string(2) "hi"
["test20"]=>
string(5) "howdy"
}
Which doesn't display the behavior you're describing.
That said, if your version of PHP is miscasting the JSON, try using get_object_vars on the stdClass object that json_decode returns
get_object_vars(json_decode($json))
That might return better results.

The problem is in the conversion from object to array.
$a = (array)json_decode('{"10":"hi","test20":"howdy"}');
var_dump($a);
//outputs
array(2) {
["10"]=>
string(2) "hi"
["test20"]=>
string(5) "howdy"
}
See how this array have index "10"? But in PHP, everything that looks like a number gets converted into a number, especially in array indexes. You can't just get a["10"] because it converts "10" into a number and this array does not have such an index.
However, foreach works.
foreach ($a as $key => $value) {
var_dump($key);
var_dump($value);
}
//outputs
string(2) "10"
string(2) "hi"
string(6) "test20"
string(5) "howdy"
You can also treat result of json_decode as an object. While you won't be able to do $a->10 or $a->"10",
$a = json_decode('{"10":"hi","test20":"howdy"}');
$b = 10;
var_dump($a->$b);
//outputs
string(2) "hi"
works.
But most likely, as Chris said, you just want to pass true as a second argument.
$a = json_decode('{"10":"hi","test20":"howdy"}', true);
var_dump($a[10]);
//outputs
string(2) "hi"

Related

store string with key and value in array

I have a string = "Name":"Susan","Age":"23","Gender":"Male";
How to store them in an array so that I can echo the value for example:
echo $array['Name']
or
echo $array['Age']
Thanks
If your string is already:
"Name":"Susan","Age":"23","Gender":"Male"
That's almost JSON, so you can just enclose it in curly brackets and convert it to turn that into an array:
$decoded = (Array)json_decode('{'.$str.'}');
json_decode() normally outputs an object, but here we're casting it to an array. This is not required, but it changes how you have to access the resulting elements.
This would render the following associative array:
array(3) {
["Name"]=>
string(5) "Susan"
["Age"]=>
string(2) "23"
["Gender"]=>
string(4) "Male"
}
Associative Arrays in PHP are what you need to achieve your task. In PHP array() are actually ordered maps i.e. associates values with a key Here is an example. An associative array is an array where each key has its own specific value. Here's an example.
$values = array("Name"=>"Susan", "Age"=>"23", "Gender"=>"Male");
echo $values['Name'];
echo $values['Age'];
echo $values['Gender'];
You can store string as json
$json = '{"Name":"Susan","Age":"23","Gender":"Male"}';
$array = json_decode($json, true);
var_dump($array);
The manual specifies the second argument of json_decode as:
assoc
When TRUE, returned objects will be converted into associative arrays.
https://stackoverflow.com/a/18576902/5546916
Try below snippet
$string = "Name":"Susan","Age":"23","Gender":"Male";
//explode string with `,` first
$s = explode(",",$string); // $s[0] = "Name":"Susan"....
$array = array();
foreach($s as $data){
$t = array();
$t = explode(":",$data); //explode with `:`
$array[$t[0]] = $t[1];
}
echo $array["name"];

Access array element indexed by numerical string

I have encountered something odd.
I have a php array, indexed with numerical keys.
However it appears impossible to access any of the elements because php automatically treats numerical strings as integers, causing an illegal offset notice.
Under normal circumstances its imposable to create a php array with numerical string indexes, but it can happen with type casting.
To reproduce:
$object = new stdClass();
$object->{'1'} = 'one';
$array = (array) $object;
var_dump($array);
/* produces
array(1) {
["1"]=>
string(3) "one"
}
*/
//none of the following will work
$key = '1';
echo $array[1], $array['1'], $array["1"], $array[(string)1], $array[$key];
Is this just an edge case bug? I only encountered the problem when attempting to improve my answer for another SO question
Live code example: http://codepad.viper-7.com/dFSlH1
Unbelievable but this is normal behavior in php, it was considered as a bug (link) in the year 2008.
But they just pointed out to the manual for the cast with (array):
If an object is converted to an array, the result is an array whose
elements are the object's properties. The keys are the member variable
names, with a few notable exceptions: integer properties are
unaccessible;
You can use get_object_vars() instead:
$object = new stdClass();
$object->{'1'} = 'one';
$array = get_object_vars( $object );
$key = '1';
echo $array[1]."<br>";
echo $array['1']."<br>";
echo $array["1"]."<br>";
echo $array[(string)1]."<br>";
echo $array[$key]."<br>";
Doesn't explain why this happens, but is a solution to avoid the cast problem.
Off topic but I thought maybe it is interesting. Found this in the manual.
To avoid these kind of problems, always use an integer OR a string as index, don't mix it up and don't use integers in a string.
Example of mixed array:
$array = array(
1 => "a",
"1" => "b",//overrides 1
1.5 => "c",//overrides "1"
true => "d",//overrides 1.5
);
var_dump($array);
You can use
$vars = get_object_vars($object);
echo $vars[1];
String keys containing valid integer values would be cast to integer keys automatically in “normal” array creation – but it seems casting from object to array doesn’t apply the same logic.
It can be fixed however, by using
$array = array_combine(array_keys($array), array_values($array));
after your line that creates the array from the object. http://codepad.viper-7.com/v5rGJa
Although, same as Dave already said in his comment, using get_object_vars looks like a “cleaner” solution to me as well.
foreach ($array as $key => $value){
var_dump($key);
var_dump($value);
}
shows
string(1) "1"
string(3) "one"
But echo $array['"1"']; gives
E_NOTICE : type 8 -- Undefined index: "1" -- at line 8
That's strange!

PHP - how to cast multidimensional object?

I am wondering if there is a built-in way in PHP to cast multidimensional objects to arrays?
The problem is when applying a regular casting on an object, the first dimension only is being affected, all other dimensions remind the same.
Note: I am interested in casting only!
Example:
$a = new stdClass();
$a->b = 'qwe';
$a->c = new stdClass();
$a->c->d = 'asd';
var_dump((array)$a); // echoes:
array(2) {
["b"]=>
string(3) "qwe"
["c"]=>
object(stdClass)#2 (1) {
["d"]=>
string(3) "asd"
}
}
As you can see only the first dimension was affected, so how to cast multidimensional objects?
There is no official way to cast a multi-level object to an array but the good news is that there is a hack.
Use json_encode() to get a JSON representation of your object then pass the result to json_decode() and use TRUE as its second argument to get arrays instead of objects.
$a = new stdClass();
$a->b = 'qwe';
$a->c = new stdClass();
$a->c->d = 'asd';
print_r(json_decode(json_encode($a), TRUE));
The output is:
Array
(
[b] => qwe
[c] => Array
(
[d] => asd
)
)
The method has some drawbacks (it cannot handle resources, for example) but they are just minor annoyances.
Since your question is if it's possible using only a single built-in PHP function to recursively cast object and children objects as array, without even any user-made callback function, the answer is no, it can't be done like that.
There are other ways to achieve it, though.

How would i convert a comma seperated list to a square bracket array

I have this variable $csv = '3,6,7,8'; that i need to convert to a square bracketed array of the form $csv_array = [3,6,7,8];
If i explode the csv like $new_array=explode(",",$csv);,it does not give me the array i want.
This is a running example http://3v4l.org/kYC0g
The code
$csv = '3,6,7,8';
$new_csv = '['.$csv.']';
if(is_array($new_csv)){
echo 'true';
}
else{
echo 'false';
//this is false
}
echo '<br/>';
$new_array=explode(",",$csv);
print_r($new_array);
//not what i am looking for
echo '<br/>';
print_r($new_csv);
echo '<br/>';
echo $new_csv;
As stated by a fellow stacker
RichardBernards - The two 'types' of array are exactly the same in PHP. If you are looking for JSON
An example of using JSON to achieve what it is you require:
To encode:
$csv = '3,6,7,8';
$array = explode(",", $csv);
$json = json_encode($array);
echo $json;
To decode $csv into the normal array you provided it:
$decoded = json_decode($json, true);
var_dump($decoded);
And then to return it to its original format:
$csv = implode(',', $decoded);
See json_encode() for more information, and also see it's opposite json_decode()
Keep in mind that JSON is literally a string and is not compatible associatively in PHP until it is decoded using the json_decode() function mentioned above. With that being said, replacing true with false in the example above would create an object array and multi-dimensional arrays would require them to be referenced differently, e.g. $array->result.
It would also be worth bringing to your attention the beauty of the predefined CSV functions within PHP
Adding [ and ] to string does not makes it PHP array.
$csv = '3,6,7,8';
var_dump(explode(',', $csv));
array(4) {
[0]=>
string(1) "3"
[1]=>
string(1) "6"
[2]=>
string(1) "7"
[3]=>
string(1) "8"
}
That is equal to ["3", "6", "7", "8"] as PHP array.
To get JSON array form it, use json_encode(explode(',', $csv)). Or simply $jsonArray = "[{$csv}]" if you need JSON array (not PHP array, because it will be simple string).

How can I force PHP to use strings for array keys? [duplicate]

This question already has answers here:
A numeric string as array key in PHP
(11 answers)
Closed 2 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
I've come across an old app that uses an id to name type array, for example...
array(1) {
[280]=>
string(3) "abc"
}
Now I need to reorder these, and a var_dump() would make it appear that that isn't going to happen while the keys are integers.
If I add an a to every index, var_dump() will show double quotes around the key, my guess to show it is now a string...
array(1) {
["280a"]=>
string(3) "abc"
}
This would let me easily reorder them, without having to touch more code.
This does not work.
$newArray = array();
foreach($array as $key => $value) {
$newArray[(string) $key] = $value;
}
A var_dump() still shows them as integer array indexes.
Is there a way to force the keys to be strings, so I can reorder them without ruining the array?
YOU CAN'T!!
Strings containing valid integers will be cast to the integer type. E.g. the key "8" will actually be stored under 8. On the other hand "08" will not be cast, as it isn't a valid decimal integer.
Edit:
ACTUALLY YOU CAN!!
Cast sequential array to associative array
$obj = new stdClass;
foreach($array as $key => $value){
$obj->{$key} = $value;
}
$array = (array) $obj;
In most cases, the following quote is true:
Strings containing valid integers will be cast to the integer type. E.g. the key "8" will actually be stored under 8. On the other hand "08" will not be cast, as it isn't a valid decimal integer.
This examples from the PHP Docs
<?php
$array = array(
1 => "a",
"1" => "b",
1.5 => "c",
true => "d",
);
var_dump($array);
?>
The above example will output:
array(1) {
[1]=> string(1) "d"
}
So even if you were to create an array with numbered keys they would just get casted back to integers.
Unfortunately for me I was not aware of this until recently but I thought I would share my failed attempts.
Failed attempts
$arr = array_​change_​key_​case($arr); // worth a try.
Returns an array with all keys from array lowercased or uppercased. Numbered indices are left as is.
My next attempts was to create a new array by array_combineing the old values the new (string)keys.
I tried several ways of making the $keys array contain numeric values of type string.
range("A", "Z" ) works for the alphabet so I though I would try it with a numeric string.
$keys = range("0", (string) count($arr) ); // integers
This resulted in an array full of keys but were all of int type.
Here's a couple of successful attempts of creating an array with the values of type string.
$keys = explode(',', implode(",", array_keys($arr))); // values strings
$keys = array_map('strval', array_keys($arr)); // values strings
Now just to combine the two.
$arr = array_combine( $keys, $arr);
This is when I discovered numeric strings are casted to integers.
$arr = array_combine( $keys, $arr); // int strings
//assert($arr === array_values($arr)) // true.
The only way to change the keys to strings and maintain their literal values would be to prefix the key with a suffix it with a decimal point "00","01","02" or "0.","1.","2.".
You can achieve this like so.
$keys = explode(',', implode(".,", array_keys($arr)) . '.'); // added decimal point
$arr = array_combine($keys, $arr);
Of course this is less than ideal as you will need to target array elements like this.
$arr["280."]
I've created a little function which will target the correct array element even if you only enter the integer and not the new string.
function array_value($array, $key){
if(array_key_exists($key, $array)){
return $array[ $key ];
}
if(is_numeric($key) && array_key_exists('.' . $key, $array)){
return $array[ '.' . $key ];
}
return null;
}
Usage
echo array_value($array, "208"); // "abc"
Edit:
ACTUALLY YOU CAN!!
Cast sequential array to associative array
All that for nothing
You can append the null character "\0" to the end of the array key. This makes it so PHP can't interpret the string as an integer. All of the array functions (like array_merge()) work on it. Also not even var_dump() will show anything extra after the string of integers.
Example:
$numbers1 = array();
$numbers2 = array();
$numbers = array();
$pool1 = array(111, 222, 333, 444);
$pool2 = array(555, 666, 777, 888);
foreach($pool1 as $p1)
{
$numbers1[$p1 . "\0"] = $p1;
}
foreach($pool2 as $p2)
{
$numbers2[$p2 . "\0"] = $p2;
}
$numbers = array_merge($numbers1, $numbers2);
var_dump($numbers);
The resulting output will be:
array(8) {
["111"] => string(3) "111"
["222"] => string(3) "222"
["333"] => string(3) "333"
["444"] => string(3) "444"
["555"] => string(3) "555"
["666"] => string(3) "666"
["777"] => string(3) "777"
["888"] => string(3) "888"
}
Without the . "\0" part the resulting array would be:
array(8) {
[0] => string(3) "111"
[1] => string(3) "222"
[2] => string(3) "333"
[3] => string(3) "444"
[4] => string(3) "555"
[5] => string(3) "666"
[6] => string(3) "777"
[7] => string(3) "888"
}
Also ksort() will also ignore the null character meaning $numbers[111] and $numbers["111\0"] will both have the same weight in the sorting algorithm.
The only downside to this method is that to access, for example $numbers["444"], you would actually have to access it via $numbers["444\0"] and since not even var_dump() will show you there's a null character at the end, there's no clue as to why you get "Undefined offset". So only use this hack if iterating via a foreach() or whoever ends up maintaining your code will hate you.
Use an object instead of an array $object = (object)$array;
EDIT:
I assumed that if they are integers, I
can't reorder them without changing
the key (which is significant in this
example). However, if they were
strings, I can reorder them how they
like as the index shouldn't be
interpreted to have any special
meaning. Anyway, see my question
update for how I did it (I went down a
different route).
Actually they dont have to be in numeric order...
array(208=>'a', 0=> 'b', 99=>'c');
Is perfectly valid if youre assigning them manually. Though i agree the integer keys might be misinterpreted as having a sequential meaning by someone although you would think if they were in a non-numeric order it would be evident they werent. That said i think since you had the leeway to change the code as you updated that is the better approach.
Probably not the most efficient way but easy as pie:
$keys = array_keys($data);
$values = array_values($data);
$stringKeys = array_map('strval', $keys);
$data = array_combine($stringKeys, $values);
//sort your data
I was able to get this to work by adding '.0' onto the end of each key, as such:
$options = [];
for ($i = 1; $i <= 4; $i++) {
$options[$i.'.0'] = $i;
}
Will return:
array("1.0" => 1, "2.0" => 2, "3.0" => 3, "4.0" => 4)
It may not be completely optimal but it does allow you to sort the array and extract (an equivalent of) the original key without having to truncate anything.
Edit:
This should work
foreach($array as $key => $value) {
$newkey = sprintf('%s',$key);
$newArray["'$newkey'"] = $value;
}
Hi we can make the index of the array a string using the following way. If we convert an array to xml then indexes like [0] may create issue so convert to string like [sample_0]
$newArray = array();
foreach($array as $key => $value) {
$newArray["sample_".$key] = $value;
}
All other answers thus far are hacks that either use fragile workarounds that could break between major PHP versions, create unnecessary gotchas by deliberately corrupting keys, or just slow down your code for no benefit. The various functions to sort arrays yet maintain the crucial key associations have existed since PHP 4.
It is pointless stop PHP from using integer keys, it only does so when the integer representation is exactly the same as the string, thus casting an integer key back to string when reading from the array is guaranteed to return the original data. PHP's internal representation of your data is completely irrelevant as long as you avoid the functions that rewrite integer keys. The docs clearly state which array functions will do that.
An example of sorting, without any hacks, that demonstrates how data remains uncorrupted:
<?php
# use string keys to define as populating from a db, etc. would,
# even though PHP will convert the keys to integers
$in = array(
'347' => 'ghi',
'176' => 'def',
'280' => 'abc',
);
# sort by key
ksort($in);
echo "K:\n";
$i = 1;
foreach ($in as $k => $v) {
echo $i++, "\n";
$k = (string) $k; # convert back to original
var_dump($k, $v);
}
# sort by value
asort($in, SORT_STRING);
echo "\nV:\n";
$i = 1;
foreach ($in as $k => $v) {
echo $i++, "\n";
$k = (string) $k;
var_dump($k, $v);
}
# unnecessary to cast as object unless keys could be sequential, gapless, and start with 0
if (function_exists('json_encode')) {
echo "\nJSON:\n", json_encode($in);
}
The output it produces hasn't changed since v5.2 (with only the JSON missing prior to that):
K:
1
string(3) "176"
string(3) "def"
2
string(3) "280"
string(3) "abc"
3
string(3) "347"
string(3) "ghi"
V:
1
string(3) "280"
string(3) "abc"
2
string(3) "176"
string(3) "def"
3
string(3) "347"
string(3) "ghi"
JSON:
{"280":"abc","176":"def","347":"ghi"}

Categories