Page 1 of 1

[SOLVED!] Problem indexing..(updated)

Posted: Wed Nov 26, 2003 8:55 pm
by infolock
is there a way to merge 2 arrays into 1 array while sorting them? :)

i know how to use array_merge, however it's just putting all the data for my first array in first, and then all the data from my second in afterwards.


for example :

i have 2 arrays : $time and $date.

each of these arrays has 2 values

when i try to use

Code: Select all

<?php

$result = array_merge($time, $date);
print_r($result);

?>
it's coming out like this :

Array([0]20031010 [1]20031111 [2] 110505 [3] 040048)


anyone have a clue to where i can have it input the arrays as date, time, date, time, date, time, etc... ?

Posted: Wed Nov 26, 2003 10:15 pm
by infolock
n/m figured it out...

edit :: guess i could post the answer lol...

was actually too simple..

i didn't end up using merge, but instead used this :

Code: Select all

$result = array();
for($i=0; $i<=$count($date); $i++)
{
   $result[] = $date[$i];
   $result[] = $time[$i];
}
sigh lol...

Posted: Thu Nov 27, 2003 12:06 am
by infolock
ok... so, since we are talking about arrays, let's try another scenario, and bit more complicated one at that...

I need to do this in order to spit the array at a pie chart. since i'm having a lot of dynamic values, i can't specify exactly how many arrays to use since it will vary from time to time.

i define the variable $data ( which the pie chart uses to determine how to draw the chart ) like this :

Code: Select all

$data = ('Hello' => 45, 'Something' => 55);
where hello is var one, and 45 is the actual percentage ( so it knows how big of a slice to cut in the pie ).

In the actual function that this pie chart derrives in, it's calling $data ( that you pass it ) $data_array, and splits it like this :

Code: Select all

foreach($data_array as $var=>$val)
			{
			$data_set[$var][0] = $val;
			$data_set[$var][1] = (360/$data_sum)*$val;

so, here lies my problem. let's say i have $date and $time.

Only this time, i'm wanting to index $date to $time into an array called $data.

so i'm wanting to do :

Code: Select all

$result = array($date => $time);
(with $time being an array of 3 times, and $date being an array of 3 dates )

while hoping to get a result of :

Array(
[20031010] => 110505, [20031111] => 040048 )

HOWEVER, when I do it like this, all I get is a blank Array :

Array ()

with no data in the array. Why is this occuring ? Am I missing something here ?

I mean, I know I can do something like

Code: Select all

for($i=0; $i<=count($date); $i++)
{
  $result[] = array($date[$i] => $time[$i]);
}
but this numbers the array insteads of creating indexes of the values for date.


any help would seriously be appreciated...

Posted: Thu Nov 27, 2003 4:39 am
by infolock
any thoughts on this ?

Posted: Thu Nov 27, 2003 4:45 am
by twigletmac
What do you get if you do:

Code: Select all

for($i=0; $i<=count($date); $i++)
{
  $result[$date[$i]] = $time[$i];
}
Mac

Posted: Thu Nov 27, 2003 4:45 am
by malcolmboston
looks in awe of such complex coding........... :lol:

Posted: Thu Nov 27, 2003 4:49 am
by infolock
MAC!!!!!!!!!!!! LOL!!! I been working for 8 hours now on this ONE freakin headache and you solve it in a matter of seconds! lol, i've tried every known variation i could think of, and could not get it to work, but THAT worked PERFECTLY!

Oh man, if you were here i'd give you the biggest kiss!

Thanks so much. wh00t! i can now finally rewrite all this ugly code and compensate it for great intellect :D

edit : i just woke my parents up from this one !

Posted: Thu Nov 27, 2003 5:01 am
by twigletmac
:lol: glad it's sorted.

Mac