Processing Multi-Dimensional array into existing object
Posted: Thu Feb 14, 2013 10:57 pm
Hi All,
Have another interesting conundrum which involves JSON, arrays and objects.
I am making use of a PHP library which helps to render JavaScript graphs.
This library you can set a large number of settings for the graph itself by means of the following format:
The available variables to set are quite large in number, so i was thinking about storing all the information required to set up one of these graphs using JSON potentially storing everything as an array which parses nicely into a JSON format.
The problem though, is retrieving and setting all of this information without having to explicitly define everything after the JSON has been retrieved.
Now i did experiment with converting the original array into an object after being retrieved, however for ease, i decided not to go down this path due to issues accessing arrays at the end of the tree which were being converted into objects. Followed by the issue that if i did manage to keep the *actual* arrays stored correctly, they were difficult to access where you had to access them via $obj->myvariable->{1} which wasn't ideal.
So in the end i opted to just keep everything as an array as i thought it would be easier in the long term to handle.
So the problem i am facing is, How do you recursively add array data into an object as object variables?
If we take the example above:
The equivalent array structure would be:
Initially i thought simply looping through the array would be good enough to start setting variables until you got to the second level of variables.
If you try to palm $value off to a method that goes and does the same sort of thing as the above code, i'm not sure you would ever get the structure of
Not only that, but you cant simply do something like:
Because the contents of $chart->chart is not an array, so it cant be assigned as such.
So my question is, what would be the easiest method of assigning multiple levels of variables in this case?
My feeling is that there may be a way using stdClass or something.
PS Here is the full PHP array and the full list of possible assignments that i know of.
PHP Equivalent Array:
Assignments:
cheers
Weiry.
Have another interesting conundrum which involves JSON, arrays and objects.
I am making use of a PHP library which helps to render JavaScript graphs.
This library you can set a large number of settings for the graph itself by means of the following format:
Code: Select all
$chart->chart->renderTo = 'linechart';
$chart->title->text = 'Example chart';
$chart->yAxis->title->text = 'Total';
$chart->addSeries( $series1 );The problem though, is retrieving and setting all of this information without having to explicitly define everything after the JSON has been retrieved.
Now i did experiment with converting the original array into an object after being retrieved, however for ease, i decided not to go down this path due to issues accessing arrays at the end of the tree which were being converted into objects. Followed by the issue that if i did manage to keep the *actual* arrays stored correctly, they were difficult to access where you had to access them via $obj->myvariable->{1} which wasn't ideal.
Code: Select all
function arrayToObject( $array )
{
if ( !is_array( $array ) ) {
return $array;
}
$object = new stdClass();
if ( is_array( $array ) && count( $array ) > 0 ) {
foreach ( $array as $name => $value ) {
$name = strtolower( trim( $name ) );
if ( !empty( $name ) ) {
if(($name+0) > 0){
$object->$name = $value;
}else{
$object->$name = arrayToObject( $value );
}
}
}
return $object;
} else {
return FALSE;
}
}So the problem i am facing is, How do you recursively add array data into an object as object variables?
If we take the example above:
Code: Select all
$chart->chart->renderTo = 'graphDiv';
$chart->title->text = 'Example chart';
$chart->yAxis->title->text = 'Total';Code: Select all
$chart = array(
'chart' => array(
'renderTo ' => 'graphDiv'
),
'title' => array(
'text' => 'Example Chart'
),
'yAxis' => array(
'title' => 'Total'
)
);Code: Select all
foreach ( $settings as $name => $value ){
$chart->$name = ??
}Code: Select all
$chart->$tier1->$tier2->$tier3 //...Code: Select all
$chart->chart = $settings['chart'];So my question is, what would be the easiest method of assigning multiple levels of variables in this case?
My feeling is that there may be a way using stdClass or something.
PS Here is the full PHP array and the full list of possible assignments that i know of.
PHP Equivalent Array:
Code: Select all
$settings = array(
'title' => new Settings(
'title', array(
'text' => 'Most Popular Topics',
'align' => 'left',
'floating' => true,
'style' => array(
'font' => '18px Metrophobic, Arial, sans-serif',
'color' => '#0099ff'
),
'x' => 20,
'y' => 20
)
),
'chart' => array(
"renderTo" => "linechart",
"width" => 500,
"height" => 300,
"marginTop" => 60,
"marginLeft" => 90,
"marginRight" => 30,
"marginBottom" => 110,
"spacingRight" => 10,
"spacingBottom" => 15,
"spacingLeft" => 0,
"backgroundColor" => array(
"linearGradient" => array(0,0,0,300),
"stops" => array(
array(0,'rgb(217, 217, 217)'),array(1,'rgb(255, 255, 255)')
)
),
"alignTicks" => false
),
"legend" => array(
"enabled" => true,
"layout" => 'horizontal',
"align" => 'center',
"verticalAlign" => 'bottom',
"itemStyle" => array(
'color' => '#222'
),
"backgroundColor" => array(
)
),
"tooltip" => array(
"formatter" => 'Formatter', //class reference
"backgroundColor" => array(
"linearGradient" => array(0,0,0,50),
"stops" => array(array(0,'rgb(217, 217, 217)'),array(1,'rgb(255, 255, 255)'))
)
),
"plotOptions" => array(
"line" => array(
"pointStart" => (strtotime('-30 day') * 1000),
"pointInterval" => (24 * 3600 * 1000)
)
),
"xAxis" => array(
"type" => 'datetime',
"tickInterval" => 'plotOptions.line.pointInterval', //settings reference
"startOnTick" => true,
"tickmarkPlacement" => 'on',
"tickLength" => 10,
"minorTickLength" => 5,
"labels" => array(
"align" => 'right',
"step" => 2,
"rotation" => -35,
"x" => 5,
"y" => 20
),
"dataLabels" => array(
"formatter" => 'Formatter' //class reference
)
),
"yAxis" => array(
"labels" => array(
"formatter" => 'Formatter' //class reference
),
"min" => 0,
"maxPadding" => 0.2,
"endOnTick" => true,
"minorGridLineWidth" => 0,
"minorTickInterval" => 'auto',
"minorTickLength" => 1,
"tickLength" => 2,
"minorTickWidth" => 1,
"title" => array(
"text" => 'Pageviews',
"align" => 'high',
"style" => array(
"font" => '14px Metrophobic, Arial, sans-serif'
),
"rotation" => 0,
"x" => 60,
"y" => -10,
),
"plotLines" => array(
array('color' => '#808080', 'width' => 1, 'value' => 0 )
)
)
);Code: Select all
$linechart->title->text = "Most Popular Topics";
$linechart->title->align = "left";
$linechart->title->floating = true;
$linechart->title->style->font = '18px Metrophobic, Arial, sans-serif';
$linechart->title->style->color = '#0099ff';
$linechart->title->x = 20;
$linechart->title->y = 20;
$linechart->chart->renderTo = 'linechart';
$linechart->chart->width = 500;
$linechart->chart->height = 300;
$linechart->chart->marginTop = 60;
$linechart->chart->marginLeft = 90;
$linechart->chart->marginRight = 30;
$linechart->chart->marginBottom = 110;
$linechart->chart->spacingRight = 10;
$linechart->chart->spacingBottom = 15;
$linechart->chart->spacingLeft = 0;
$linechart->chart->backgroundColor->linearGradient = array(0,0,0,300);
$linechart->chart->backgroundColor->stops = array(array(0,'rgb(217, 217, 217)'),array(1,'rgb(255, 255, 255)'));
$linechart->chart->alignTicks = false;
$linechart->legend->enabled = true;
$linechart->legend->layout = 'horizontal';
$linechart->legend->align = 'center';
$linechart->legend->verticalAlign = 'bottom';
$linechart->legend->itemStyle = array('color' => '#222');
$linechart->legend->backgroundColor->linearGradient = array(0,0,0,25);
$linechart->legend->backgroundColor->stops = array(array(0,'rgb(217, 217, 217)'),array(1,'rgb(255, 255, 255)'));
$linechart->tooltip->formatter = new HighRollerFormatter(); // TOOLTIP FORMATTER
$linechart->tooltip->backgroundColor->linearGradient = array(0,0,0,50);
$linechart->tooltip->backgroundColor->stops = array(array(0,'rgb(217, 217, 217)'),array(1,'rgb(255, 255, 255)'));
$linechart->plotOptions->line->pointStart = strtotime('-30 day') * 1000;
$linechart->plotOptions->line->pointInterval = 24 * 3600 * 1000; // one day
$linechart->xAxis->type = 'datetime';
$linechart->xAxis->tickInterval = $linechart->plotOptions->line->pointInterval;
$linechart->xAxis->startOnTick = true;
$linechart->xAxis->tickmarkPlacement = 'on';
$linechart->xAxis->tickLength = 10;
$linechart->xAxis->minorTickLength = 5;
$linechart->xAxis->labels->align = 'right';
$linechart->xAxis->labels->step = 2;
$linechart->xAxis->labels->rotation = -35;
$linechart->xAxis->labels->x = 5;
$linechart->xAxis->labels->y = 20;
$linechart->xAxis->dataLabels->formatter = new Formatter();
$linechart->yAxis->labels->formatter = new Formatter();
$linechart->yAxis->min = 0;
$linechart->yAxis->maxPadding = 0.2;
$linechart->yAxis->endOnTick = true;
$linechart->yAxis->minorGridLineWidth = 0;
$linechart->yAxis->minorTickInterval = 'auto';
$linechart->yAxis->minorTickLength = 1;
$linechart->yAxis->tickLength = 2;
$linechart->yAxis->minorTickWidth = 1;
$linechart->yAxis->title->text = 'Pageviews';
$linechart->yAxis->title->align = 'high';
$linechart->yAxis->title->style->font = '14px Metrophobic, Arial, sans-serif';
$linechart->yAxis->title->rotation = 0;
$linechart->yAxis->title->x = 60 ;
$linechart->yAxis->title->y = -10;
$linechart->yAxis->plotLines = array( array('color' => '#808080', 'width' => 1, 'value' => 0 ));
Weiry.