Skip to content
Advertisement

d3.js – PHP Array format

I need to create a d3.js chart using PHP formatted array for JSON. The format is like:

[
{
    "Category": "LV",
    "Percentage": 8 
},
{
    "Category": "CL",
    "Percentage": 20
},
{
    "Category": "Paed",
    "Percentage": 15
}
]

The array code and format that I currently have is below.

The format of the array clearly needs to be changed, but I’m not sure how to do that to suite the above format.

$data = array(
        array('LV' => round($low_vision_percentage, 2)), 
        array('CL' => round($contact_lenses_percentage, 2)), 
        array('Paed' => round($paediatrics_percentage, 2)),
        array('BV' => round($binocular_vision_percentage, 2)), 
        array('VT' => round($vision_therapy_percentage, 2)),  
        array('T' => round($therapeutics_percentage, 2)),
        array('R' => round($research_percentage, 2)), 
        array('CP' => round($clinical_practice_percentage, 2)),
        array('Op' => round($optics_percentage, 2)), 
        array('BVS' => round($broad_vision_sciences, 2)),
        array('Other' => round($other_percentage, 2)));

$json = json_encode($data);

$fp = fopen('categories.json', 'w');
fwrite($fp, $json);
fclose($fp);

Advertisement

Answer

The best approach may be to create your $data array in the desired format from the onset as this would be more efficient and be less code. See below:

$data = [
    [
        "Category"=>"LV",
        "Percentage"=>round($low_vision_percentage, 2),
    ],
    [
        "Category"=>"CL",
        "Percentage"=>round($contact_lenses_percentage, 2),
    ],
    //... continue this pattern for other entries
];

However, I am not sure if you are using $data as is elsewhere. The code below will allow you to transform your current data to your desired d3jsFormattedData using array_keys.

$data = array(
        array('LV' => round($low_vision_percentage, 2)), 
        array('CL' => round($contact_lenses_percentage, 2)), 
        array('Paed' => round($paediatrics_percentage, 2)),
        array('BV' => round($binocular_vision_percentage, 2)), 
        array('VT' => round($vision_therapy_percentage, 2)),  
        array('T' => round($therapeutics_percentage, 2)),
        array('R' => round($research_percentage, 2)), 
        array('CP' => round($clinical_practice_percentage, 2)),
        array('Op' => round($optics_percentage, 2)), 
        array('BVS' => round($broad_vision_sciences, 2)),
        array('Other' => round($other_percentage, 2)));

$d3jsFormattedData = array_map(function($entry){
    //assume valid array entries are always available
    return [
        "Category"=>array_keys($entry)[0], //get first array key
        "Percentage"=>array_values($entry)[0] //get first array value
    ];
},$data);

//or safer approach

$d3jsFormattedData = array_filter(
    array_map(function($entry){
        //ensure non-empty arrays are available as elements
        if(!is_array($entry) || empty($entry)) return null; 
        $category = array_keys($entry)[0]; //still assuming 1 entry in each
        return [
            "Category"=> $category,
            "Percentage"=>$entry[$category]
        ];
    },$data), function($entry){
        return $entry != null; //remove invalid entries
});

NB. [] is shorthand for array() , you may read more here on php arrays

Let me know if this works for you.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement