I have an array named $paises:
Array
(
[0] => Array
(
[life_sciences] => Array
(
[0] => Array
(
[value] => Life Sciences
)
[1] => Array
(
[value] => 226
)
[2] => Array
(
[value] => 433
)
[3] => Array
(
[value] => 816
)
)
[telecom] => Array
(
[0] => Array
(
[value] => Telecom
)
[1] => Array
(
[value] => 10
)
[2] => Array
(
[value] => 20
)
[3] => Array
(
[value] => 30
)
)
)
)
I need transform in another array with json format exactly like this:
{
"dataset": [
{
"seriesname": "Life Sciences",
"data": [{"value": "226"}, {"value": "433"}, {"value": "816"}]
},
{
"seriesname": "Telecom",
"data": [{"value": "10"}, {"value": "20"}, {"value": "30"}]
},
.
.
.
]
}
What i'm doing returns a wrong array format:
$chart = array();
for ($i=0; $i < count($paises); $i++) {
// here i get the value from index 0
$chart["dataset"][]['seriesname'] = $paises[$i]['life_sciences'][0]['value'];
// Here i remove the index 0 and reaorder the array indexes
unset($paises[$i]['life_sciences'][0]);
array_splice($paises[$i]['life_sciences'], 1, 1);
// Here i get all values
$chart["dataset"][]['data'] = $paises[$i]['life_sciences'];
}
Result:
{
"dataset": [
{
"seriesname":"Life Sciences"
},
{
"data":[{"value":"226"},{"value":"816"},{"value":"1098"}]
},
{
"seriesname":"Telecom"
},
{
"data":[{"value":"10"},{"value":"20"},{"value":"30"}]
},
.
.
.
]
}
Note that the "seriesname" is separated of "data":
{ "seriesname":"Life Sciences" }, { "data":[{"value":"226"},...] }
But what i need is:
{ "seriesname":"Life Sciences" , "data":[{"value":"226"},...] }
How can i do this in correct way?
########## EDIT 1I used:
$chart = array();
for ($i=0; $i < count($paises); $i++) {
$chart["dataset"][$i]['seriesname'] = $paises[$i]['life_sciences'][0]['value'];
$chart["dataset"][$i]['data'] = $paises[$i]['life_sciences'];
$chart["dataset"][$i]['seriesname'] = $paises[$i]['telecom'][0]['value'];
$chart["dataset"][$i]['data'] = $paises[$i]['telecom'];
}
But returns only the "telecom" elements, ignoring the "life_sciences"
Try this for reformatting the array.
<?php
$json = '{
"dataset": [
{
"seriesname": "Life Sciences",
"data": [{"value": "226"}, {"value": "433"}, {"value": "816"}]
}
]
}';
$data = json_decode($json)->dataset;
foreach ($data as $series) {
$tempArr = Array();
foreach ($series->data as $value) {
array_push($tempArr, $value);
}
$series->data = $tempArr;
}
print_r($data);
?>
To access your data:
$data->data
Or speries name:
$data->seriesname
The []
operator creates a new array element every time you use it. So when you do this:
$chart["dataset"][]['seriesname'] = $paises[$i]['life_sciences'][0]['value'];
You're appending a new element, and when you do this:
$chart["dataset"][]['data'] = $paises[$i]['life_sciences'];
You're appending another new element. You can either add them both at the same:
$char["dataset"][] = [
"seriesname" => ...
"data" => ...
];
Or explicitly specify your new index:
$chart["dataset"][$i]['seriesname'] = ...
$chart["dataset"][$i]['data'] = ...
$chart["dataset"][] = array(
"seriesname" => $paises[$i]['life_sciences'][0]['value'],
"data" => array_slice($paises[$i]['life_sciences'], 1)
);
I think this would be appropriate for cycle body
EDIT 1
After your edition I think about applying something like that
$k = array_keys($paises[0]); // get array of keys names
for ($i=0; $i < count($k); $i++) { // iterate key names
$chart["dataset"][] = array(
"seriesname" => $paises[0][$k[$i]][0]['value'],
"data" => array_slice($paises[0][$k[$i]], 1) // array without first element
);
}
Play with snippet to form your solution.
Add one more outside loop to iterate $paises
if needed.
You can use array_shift()
to take the first element of an array (and remove it from the array):
function transformPaisesArray(array $paises): array
{
$transformedPaisesArray = [];
foreach ($paises[0] as $series) {
// Take the first element from the array.
$name = array_shift($series)['value'];
// Add this first element and the rest as a new record to the target array.
$transformedPaisesArray[] = [
'seriesname' => $name,
'data' => $series
];
}
// Wrap everything into the dataset field.
return ['dataset' => $transformedPaisesArray];
}