0

I am trying to create a chart based on some php data i got in an array, if I run the following

<?php     
    $out = array_values($periodi);  
    echo json_encode($out, JSON_PRETTY_PRINT); 
?>

I get this json format

   [
    "Francia",
    "Italia",
    "Italia",
    "Germania",
    "Afghanistan",
    "Italia"
]

While what I am looking for is to get a format like this. Regardless of different values how do I add a propriety name in roder to get something along those lines?

var data = [{
    "name": "Tokyo",
    "data": 3.0
}, {
    "name": "NewYork",
    "data": 2.0
}, {
    "name": "Berlin",
    "data": 3.5
}, {
    "name": "London",
    "data": 1.5
}];

print_r($periodi);

Array ( [0] => Moderno [1] => Contemporaneo [2] => Contemporaneo [3] => Contemporaneo [4] => Contemporaneo )

I have another array with different data but still the format is wrong:

Array ( [francese] => Array ( [maschio] => Array ( [0] => 1 ) [femmina] => Array ( [0] => 1 ) ) [chimica] => Array ( [maschio] => Array ( [0] => 1 [1] => 1 ) ) [fisica] => Array ( [maschio] => Array ( [0] => 1 [1] => 1 ) [femmina] => Array ( [0] => 1 ) ) [scienze] => Array ( [maschio] => Array ( [0] => 1 ) ) [inglese] => Array ( [maschio] => Array ( [0] => 1 ) ) [spagnolo] => Array ( [maschio] => Array ( [0] => 1 ) ) [italiano] => Array ( [femmina] => Array ( [0] => 1 ) ) )

 {
        "maschio": [
            true
        ],
        "femmina": [
            true
        ]
    },
    {
        "maschio": [
            true,
            true
        ]
    },
    {
        "maschio": [
            true,
            true
        ],
        "femmina": [
            true
        ]
    },
    {
        "maschio": [
            true
        ]
    },
    {
        "maschio": [
            true
        ]
    },
    {
        "maschio": [
            true
        ]
    },
    {
        "femmina": [
            true
        ]
    }
]
4
  • update your post with print_r($periodi) Commented Apr 13, 2017 at 5:27
  • @SahilGulati updated, its in the question Commented Apr 13, 2017 at 5:29
  • Is this a sample array or actual one? Your question is incomplete Commented Apr 13, 2017 at 5:30
  • @SahilGulati that is the actual array Commented Apr 13, 2017 at 5:32

3 Answers 3

2
    $jsonArr = array();
    foreach($periodi as $key => $val){
        $jsonArr[$key]['name'] = $val;
        $jsonArr[$key]['data'] = $key; //there is no source for priority 
          //in your array so i just put $key as priority variable
    }

    json_encode($jsonArr);
Sign up to request clarification or add additional context in comments.

Comments

1

You should make your array like this ...

Array
(
    [0] => Array
        (
            ['name'] => 'Tokyo'
            ['data'] => 3
        )

    [1] => Array
        (
            ['name'] => 'NewYork'
            ['data'] => 2
        )
)

How to make the array ?

$final_array = array();
foreach($data as $key=>$value)
{
    $final_array[]=array(
                "name"=>$key,
                "data"=>$value
               );
}

Then you just need to json_encode the $final_array.

4 Comments

I understand it's an associative array, I have update the quetsion with a second array i go, yet how do I insert [name] = > etc?
Does it help? Also regarding the one more array you posted ... What should it be formatted like?
the same as per the other one
So the "data" will contain the true and false ?
0

PHP code demo

<?php
$array=
    [
    "Moderno",
    "Contemporaneo",
    "Contemporaneo",
    "Contemporaneo",
    "Contemporaneo"
];

$array=array_count_values($array);
$result=array();
foreach($array as $key => $value)
{
    $result[]=array("name"=>$key,"data"=>$value);
}
print_r(json_encode($result));

2 Comments

any idea why if i then set this var data = <?php echo json_encode($result); ?> I get NULL? I can see the output if i take that php out of js
Reason can be.. Your $result is not an array. This will be the only reason i hope so.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.