PHP json_encode - Strange behaviour - php

Using json_encode to encode an array of dates, it sometimes does one thing, sometimes does another.
For example, if I'm trying to encode something like:
array(6) {
[0]=>
string(6) "Jun-24"
[1]=>
string(6) "Jun-25"
[2]=>
string(6) "Jun-28"
[3]=>
string(11) "Training-24"
[4]=>
string(6) "Jun-29"
[5]=>
string(6) "Jun-30"
}
It will output
["Jun-24","Jun-25","Jun-28","Training-24","Jun-29","Jun-30"]
However, when I try to encode something like:
array(17) {
[0]=>
string(6) "Jun-23"
[1]=>
string(6) "Jun-24"
[2]=>
string(6) "Jun-28"
[3]=>
string(11) "Training-24"
[4]=>
string(6) "Jun-29"
[5]=>
string(6) "Jun-30"
[6]=>
string(6) "Jul-06"
[7]=>
string(6) "Jul-07"
[9]=>
string(6) "Jul-09"
[10]=>
string(6) "Jul-16"
[11]=>
string(6) "Jul-17"
[12]=>
string(6) "Jul-20"
[13]=>
string(6) "Jul-23"
[14]=>
string(6) "Jul-24"
[15]=>
string(6) "Jul-30"
[16]=>
string(6) "Aug-01"
[17]=>
string(6) "Aug-05"
}
It will output
{"0":"Jun-23","1":"Jun-24","2":"Jun-28","3":"Training-24","4":"Jun-29","5":"Jun-30","6":"Jul-06","7":"Jul-07","9":"Jul-09","10":"Jul-16","11":"Jul-17","12":"Jul-20","13":"Jul-23","14":"Jul-24","15":"Jul-30","16":"Aug-01","17":"Aug-05"}
(Sorry, couldn't find a smaller example where it fails)
Point being, why does it do this? The options are the same, the array is structured the same, what's the issue?

Your PHP array is missing entry 8, so is a mapping (object) and not a list (array).

You don't have key [8] set in your second example. According to the documentation a sequential array with an unset key will be encoded as a JSON object and not a JSON array.

In your first example the array is numbered sequentially from zero. PHP treats this as a conventional array and encodes it accordingly.
In your second example element 8 is missing. PHP treats this as an associative array and encodes the keys accordingly.

It is because of the indexing issue,
when your index is not proper it will behave like this.
the best way to resolve is reindexing.
$array = array_values($array);
Do like this just before converting to JSON.

Just when you encode the array, do this which will reindex array values:
$encoded = json_encode(array_values($myArray));

Related

Removing duplicates out of multiple arrays, php

I've got multiple arrays, and want to remove the duplicates. So I only got the unique items.
array(4) {
[0]=>
string(14) "Bergen op Zoom"
[1]=>
string(9) "jan steen"
[2]=>
string(7) "culture"
[3]=>
string(11) "Netherlands"
}
array(8) {
[0]=>
string(14) "fasion"
[1]=>
string(9) "conceptial"
[2]=>
string(7) "industrial"
[3]=>
string(11) "Netherlands"
}
I want to print all the strings out of the array except for the last Netherlands because it's already printed.
I've tried it with array_unique() but it only does that if there are duplicates in the array itself.
no clue how to get this thing working..
Use array_diff. It will stay in arr1 only items that not in arr2
array_diff($arr1, $arr2);
Try this
array_unique( array_merge($array1, $array2) );

PHP_XLSwriter array output

I'm working with a bit of php code called PHP_XLSWriter. It accepts an input, then outputs it to an Excel sheet.
Their example.php page does exactly what I want, and in it, there's a bit of code that determines what specifically gets exported:
$data1 = array(
array('2003','1','-50.5','2010-01-01 23:00:00'),
array('2003','=B2', '23.5','2010-01-01 00:00:00'),
);
I haven't written arrays like this exactly in the past, but I've passed my own array into this page through AJAX from a separate page. I'm not sure if it's important how I did this, but I'm happy to paste in that code if it is.
When I try to execute the page using MY array, the page fails and my console is full of intelligible text.
I can't figure out why this is as the var_dump of our two arrays are exactly the same.
the original var_dump($data1);
array(2) {
[0]=>
array(4) {
[0]=>
string(4) "2003"
[1]=>
string(1) "1"
[2]=>
string(5) "-50.5"
[3]=>
string(19) "2010-01-01 23:00:00"
}
[1]=>
array(4) {
[0]=>
string(4) "2003"
[1]=>
string(3) "=B2"
[2]=>
string(4) "23.5"
[3]=>
string(19) "2010-01-01 00:00:00"
}
}
the var_dump() when I set $data1 equal to the array I've imported:
array(2) {
[0]=>
array(4) {
[0]=>
string(6) "blah"
[1]=>
string(12) "two"
[2]=>
string(6) "0.0160"
[3]=>
string(6) "0.0173"
}
[1]=>
array(4) {
[0]=>
string(3) "blah"
[1]=>
string(14) "three"
[2]=>
string(6) "0.0085"
[3]=>
string(6) "0.0095"
}
}
and echo gettype($data1); outputs array for both of the strings.
What might I be doing to cause this to break? I'm positive it has something to do with my array, as all else held equal, the code works.

Can't sort array items by value ASC

I get all keywords separated by commas from database and create arrays from them then I merge all arrays. From the resulted array I keep only unique items array_unique. The problem is that I can't sort resulted array by value.
Here is my code:
$select_all_keywords = mysqli_query($db_connect, "SELECT `keywords` FROM `bookmarks`") or die(mysqli_error());
$keywords_array = array();
while($keywords = mysqli_fetch_assoc($select_all_keywords))
{
$explode_keywords = explode(", ", $keywords['keywords']);
$keywords_array = array_merge($keywords_array, $explode_keywords);
}
$unique_keywords = array_unique($keywords_array);
sort($unique_keywords);
$unique_keywords = array_values($unique_keywords);
print_array($unique_keywords);
The printed array:
array(23) {
[0]=>
string(10) "Awolnation"
[1]=>
string(7) "Belgium"
[2]=>
string(7) "Gravity"
[3]=>
string(21) "Nervo (Musical Group)"
[4]=>
string(5) "R3..."
[5]=>
string(22) "R3hab (Musical Artist)"
[6]=>
string(5) "Remix"
[7]=>
string(4) "Sail"
[8]=>
string(30) "Tomorrowland (Recurring Event)"
[9]=>
string(21) "Tomorrowland Festival"
[10]=>
string(9) "Unlimited"
[11]=>
string(6) "dasdas"
[12]=>
string(10) "freshbooks"
[13]=>
string(11) "gdsfgdsfgds"
[14]=>
string(6) "mockup"
[15]=>
string(3) "php"
[16]=>
string(11) "programming"
[17]=>
string(10) "revolution"
[18]=>
string(17) "tomorrowland 2013"
[19]=>
string(11) "ummet ozcan"
[20]=>
string(10) "web design"
[21]=>
string(7) "wikihow"
[22]=>
string(3) "xml"
}
I tryed almost all sorting array functions. None of them helped me sort the array by value ASC.
I believe you might be looking for asort(), it will sort the array by value. But still keep the keys the same.
Look at the documentation here.
http://php.net/manual/en/function.asort.php

Trying to split words in string by space to become elements in array

I am trying to turn the words in a string into elements in an array by splitting the string with space (" ") as the delimiter.
I am trying to use explode, but after I var_dump() the array that explode is supposed to make, I see that the result is an array, but with weird values.
Here is my code:
/*
var_dump() of $matches is array(1) { [0]=> array(5) { [0]=> string(72) "business bossiness busyness bushiness fussiness" [1]=> string(1) "0" [2]=> string(1) "9" [3]=> string(1) "1" [4]=> string(47) "business bossiness busyness bushiness fussiness" } }
*/
$suggestionsString = $matches[0][0];
$suggestionsArray = explode(" ", $suggestionsString);
var_dump($suggestionsArray);
But the output I get from the var_dump() looks like:
array(1) {
[0]=>
array(5) {
[0]=>
string(72) "<c o="0" l="9" s="1">business bossiness busyness bushiness fussiness</c>"
[1]=>
string(1) "0"
[2]=>
string(1) "9"
[3]=>
string(1) "1"
[4]=>
string(47) "business bossiness busyness bushiness fussiness"
}
}
Why am I getting this output and how can I split the words in my string so each word is a word in an array?
What I am trying to get is just those 5 words in an array.
Once we saw the original input array $matches in its entirety, it becomes much clearer what your issue is, and it stems from attempting to debug via var_dump() with a web browser rather than viewing the direct output PHP sends (visible in the page source).
Your input array $matches looks to be the result of a preg_match() call, and contains HTML or XML markup. (Hopefully you are not attempting to process XML via regular expressions - better to use a proper XML parsing library)
// Input array ($matches)
array(1) {
[0]=>
array(5) {
[0]=>
string(72) "<c o="0" l="9" s="1">business bossiness busyness bushiness fussiness</c>"
[1]=>
string(1) "0"
[2]=>
string(1) "9"
[3]=>
string(1) "1"
[4]=>
string(47) "business bossiness busyness bushiness fussiness"
}
}
The first array element returned by preg_match() is the full matched string, not its inner matches from () capture groups, and that is what you attempted to match originally. Instead, the value you need is $matches[0][4] which contains the correct inner match.
Since your word list is separated by multiple space characters rather than single spaces, you cannot use explode(). Instead you need to use preg_split() with an expression like \s+ to match one or more whitespace characters:
$suggestionsString = $matches[0][4]; // string(47) "business bossiness busyness bushiness fussiness"
$suggestionsArray = preg_split('/\s+/', $suggestionsString);
var_dump($suggestionsArray);
// Prints:
array(5) {
[0] =>
string(8) "business"
[1] =>
string(9) "bossiness"
[2] =>
string(8) "busyness"
[3] =>
string(9) "bushiness"
[4] =>
string(9) "fussiness"
}
When I run the following script:
<?php
$suggestionsString = "business bossiness busyness bushiness fussiness";
$suggestionsArray = explode(" ", $suggestionsString);
var_dump($suggestionsArray);
?>
I get
array(5) {
[0]=>
string(8) "business"
[1]=>
string(9) "bossiness"
[2]=>
string(8) "busyness"
[3]=>
string(9) "bushiness"
[4]=>
string(9) "fussiness"
}
exactly as expected. Somehow you are not doing what you think you are doing...
there is no way that what you have posted can be really happening unless the post is a misrepresentation of your code
heres the deal --
<?php
$suggestionsString = "business bossiness busyness bushiness fussiness";
$suggestionsArray = explode(" ", $suggestionsString);
var_dump($suggestionsArray);
?>
I ran this as is (no modification)
RESULT
array(5) { [0]=> string(8) "business" [1]=> string(9) "bossiness" [2]=> string(8) "busyness" [3]=> string(9) "bushiness" [4]=> string(9) "fussiness" }
the [] Tags represent the index of the arrays that are being returned.
array(4) { [0]=> string(2) " string(5) "o="0"" [2]=> string(5) "l="9"" [3]=> string(57) "s="1">business bossiness busyness bushiness fussiness" }
Your array states that there are 4 indexes available but your result is only showing [0] [2] [3] ??? wheres [1]
What you wrote works
not sure why u get this broken dump ...
Use print_r instead of var_dump if you only want the values and not the information about the values.

Sort times array and keep unique values

I have a $times array, which contains:
array(8) {
[0]=>
string(5) "10:00"
[1]=>
string(5) "13:00"
[2]=>
string(5) "10:00"
[3]=>
string(5) "11:00"
[4]=>
string(5) "12:00"
[5]=>
string(5) "13:00"
[6]=>
string(5) "14:00"
[7]=>
string(5) "15:00"
}
How can I a) sort it so it starts with lowest value b) only have one entry of each time? (no duplicates, currently theres two 10:00s and 13:00s etc)
Why not just use PHP's inbuilt functions:
$input = array_unique($input);
sort($input);
print_r($input);
Well, you could always use the php-shipped standard functions for arrays:
Array Unique
and sort()
$array = array_unique(sort($a));

Categories