...

View Full Version : styling php arrays?



seco
01-13-2010, 03: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

)

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

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

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


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

Something like that?

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

Fou-Lu
01-13-2010, 03: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);
var_dump($s);

But that functionality will disappear as of PHP 6.

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


print_r(Charactersheet::getSkillInTraining($charsheet));


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>

<result>

<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>

<trainingTypeID>3380</trainingTypeID>

<trainingStartSP>45255</trainingStartSP>

<trainingDestinationSP>256000</trainingDestinationSP>

<trainingToLevel>5</trainingToLevel>

<skillInTraining>1</skillInTraining>

</result>

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

</eveapi>

Fou-Lu
01-13-2010, 12: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');

seco
01-13-2010, 07: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!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum