Reputation: 89
I'm running into a strange problem when indexing the array numerically with the function array_values, and than encoding it into JSON. Here is a quick explanation how my script operates. Firstly, it fetches data via query from the database and prints the following out:
Eg:
Array
(
[0] => Array
(
[label] => Direct
[value] => 1445
)
[1] => Array
(
[label] => Internal
[value] => 2
)
[2] => Array
(
[label] => Internal
[value] => 2
)
[3] => Array
(
[label] => Internal
[value] => 3
)
[4] => Array
(
[label] => Internal
[value] => 1
)
[5] => Array
(
[label] => Internal
[value] => 1
)
[6] => Array
(
[label] => External
[value] => 1
)
)
Than with this function I remove any duplicate label's and sum up the value.
$sources = array();
foreach($data as $key => $values)
{
if(array_key_exists($values['label'], $sources))
{
$sources[$values['label']]['value'] += $values['value'];
$sources[$values['label']]['label'] = $values['label'];
}
else
{
$sources[$values['label']] = $values;
}
}
Returning this:
Array
(
[Direct] => Array
(
[label] => Direct
[value] => 1445
)
[Internal] => Array
(
[label] => Internal
[value] => 9
)
[External] => Array
(
[label] => External
[value] => 1
)
)
Note: I can't use SQL to SUM up the total value as the labels direct, internal, and external are assigned after the data is pulled; so please don't suggest it.
Than, I apply array_values() to the returned filtered array, giving me this:
Array
(
[0] => Array
(
[label] => Direct
[value] => 1445
)
[1] => Array
(
[label] => Internal
[value] => 9
)
[2] => Array
(
[label] => External
[value] => 1
)
)
So far everything is good. However, when I turn the array into JSON with json_encode it returns the following string:
[{"label":"Direct","value":"1445"},{"label":"Internal","value":9},{"label":"External","value":"1"}]
On first glance it looks perfectly fine. However, if you look carefully the value 9 is not in quotes, while the values 1445 and 1 are. I need value 9 to be also in quotes.
What I did notice is the problem most likely lies in the function the removes duplicates and sums the values up, as Internal is the only one with duplicates in the orginal array.
Full Code:
//Add up values of duplicate labels
$sources = array();
foreach($data as $key => $values)
{
if(array_key_exists($values['label'], $sources))
{
$sources[$values['label']]['value'] += $values['value'];
$sources[$values['label']]['label'] = $values['label'];
}
else
{
$sources[$values['label']] = $values;
}
}
//Assign numeric values as main key
$sources = array_values($sources);
//Print JSON
echo json_encode($sources);
Upvotes: 2
Views: 953
Reputation: 227240
What I did notice is the problem most likely lies in the function the removes duplicates and sums the values up, as Internal is the only one with duplicates in the original array.
Yes. That is the issue. When you get values from a database, they are usually strings. So, your value
s are all strings. (NOTE: Use var_dump
for debugging, it'll show you the types of your values.)
When you do the addition, they are converted to ints ('1'+'2' = 3
). So, that's why 9
is not in quotes, but the others are.
Upvotes: 1