View Full Version : styling php arrays?

01-13-2010, 04:07 AM
How would i go about breaking an array up into divs, here is an example of the array im working with.

Array (

[currentTQTime] => 2010-01-13 03:05:28
[trainingEndTime] => 2010-01-19 04:43:51
[trainingStartTime] => 2010-01-13 02:22:49
[trainingTypeID] => 3380
[trainingStartSP] => 45255
[trainingDestinationSP] => 256000
[trainingToLevel] => 5
[skillInTraining] => 1


01-13-2010, 04:22 AM
What do you want you're div's to look like?

01-13-2010, 04:42 AM
i just want to get them into divs, i can style them from there

01-13-2010, 04:43 AM
No, I mean what kind of structure do you want for them?

<div name="currentTQTime">2010-01-13 03:05:28</div>
<div name="trainingEndTime">2010-01-19 04:43:51</div>

Something like that?

01-13-2010, 04:46 AM
oh yes sorry, yes that is pretty much how i want them

01-13-2010, 04:54 AM
Then I'd say an array_walk is the best approach:

function arrayToDivs($val, $key)
sprintf('<div name="%s">%s</div>' . PHP_EOL, $key, $val);
array_walk($yourArray, 'arrayToDivs');

Now, if you want to capture the result, this won't work. Sometime between php 4.2 and 5.0, they deprecated the call-time pass-by-reference, so you would need to globalize (or choose a similar approach):

function arrayToDivs($val, $key, $var)
$GLOBALS[$var] .= sprintf('<div name="%s">%s</div>' . PHP_EOL, $key, $val);

array_walk($yourArray, 'arrayToDivs', 's');
print $s;

The only reason why I chose the $GLOBALS approach is because I hate globalizing period. You could global $s and drop the third argument, or even global $$var and keep the third param. I'd keep the third param myself, since at least this way you can control the variable name at runtime, so thats good.

BTW, I should mention that this would still work (its also enabled by default):

function arrayToDivs($val, $key, $var)
$var .= sprintf('<div name="%s">%s</div>' . PHP_EOL, $key, $val);

$s = '';
array_walk($a, 'arrayToDivs', &$s);

But that functionality will disappear as of PHP 6.

01-13-2010, 06:59 AM
hmm, ok well this is how the array is printed from the api im using


im not to familiar with this way of doing things, but it is pulling the data from the xml file it creates, maybe i should just edit the xml style?

<?xml version='1.0' encoding='UTF-8'?>

<eveapi version="2">

<currentTime>2010-01-13 05:55:04</currentTime>


<currentTQTime offset="1">2010-01-13 05:55:05</currentTQTime>

<trainingEndTime>2010-01-19 04:43:51</trainingEndTime>

<trainingStartTime>2010-01-13 02:22:49</trainingStartTime>







<cachedUntil>2010-01-13 06:55:04</cachedUntil>


01-13-2010, 01:37 PM
Thats an option as well.
Otherwise, the Charactersheet::getSkillInTraining($charsheet) itself is what retrieves it as an array:

$a = Charactersheet::getSkillInTraining($charsheet);
array_walk($a, 'arrayToDiv');

01-13-2010, 08:23 PM
thank you for the help, i created a seperate php file with the generated array and used jquery to retrieve and and create the divs for each one. thanks!