3

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 1

I 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"

0

4 Answers 4

1

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'] = ...
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Alex, but did not work for me, i edited the question with the new issue
0

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

Comments

0
$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.

Comments

0

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];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.