Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add elements to array in specific format

Tags:

json

arrays

php

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"

like image 635
Eduardorph Avatar asked Jul 03 '26 01:07

Eduardorph


1 Answers

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'] = ...
like image 128
Alex Howansky Avatar answered Jul 05 '26 15:07

Alex Howansky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!