[SOLVED!] Problem indexing..(updated)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

[SOLVED!] Problem indexing..(updated)

Post 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... ?
Last edited by infolock on Thu Nov 27, 2003 4:50 am, edited 2 times in total.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post 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...
Last edited by infolock on Thu Nov 27, 2003 12:35 am, edited 1 time in total.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post 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...
Last edited by infolock on Thu Nov 27, 2003 1:21 am, edited 2 times in total.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

any thoughts on this ?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

looks in awe of such complex coding........... :lol:
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post 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 !
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

:lol: glad it's sorted.

Mac
Post Reply