Jump to content

Array help - explode 1st and copy other keys?!


kev@num

Recommended Posts

Hi, I'm not sure if this is a wierd one, I have lots of arrays i'm looping through like this to see them:-

 

for($i=0; $i <count($confirmed); $i++){//loop through
     print_r_html($confirmed[$i]);
}

 

Which shows the following :-

 

Array
(
    [0] => AAAAAA-BBBBBB
    [1] => 2
    [2] => 2010-09-29
)
Array
(
    [0] => AAAAAA-BBBBBB
    [1] => 2
    [2] => 2010-09-28
)
Array
(
    [0] => CCCCCC-DDDDDD-EEEEEE
    [1] => 3
    [2] => 2010-09-29
)

 

Key 0 is a Groupconcat split by "-"

Key 1 shows how many fields are in 0

Key 2 is just the date

 

What I would like to do is split the first array into 2 arrays, the second in 2, and the third into 3 (therefore ungrouping the groupconcat if you will)

as such:-

 

First into these two:-
Array
(
    [0] => AAAAAA
    [1] => 2
    [2] => 2010-09-29
)
Array
(
    [0] => BBBBBB
    [1] => 2
    [2] => 2010-09-29
)
Second into these two:-
Array
(
    [0] => AAAAAA
    [1] => 2
    [2] => 2010-09-28
)
Array
(
    [0] => BBBBBB
    [1] => 2
    [2] => 2010-09-28
)
Third into these two:-
Array
(
    [0] => CCCCCC
    [1] => 3
    [2] => 2010-09-29
)
Array
(
    [0] => DDDDDD
    [1] => 3
    [2] => 2010-09-29
)
Array
(
    [0] => EEEEEE
    [1] => 3
    [2] => 2010-09-29
)
)

 

I know I can get out the info from the first of the keys in each array by putting this into the loop:-

 

for($i=0; $i <count($confirmed); $i++){//loop through
     if($i==0){
             $seperated[] = explode("-",$confirmed[$i]);//split by "-"
     } 
}
    print_r_html($seperated);

 

 

but i'm not sure how to copy the other keys at the same time? If anyone has got any ideas that would be great!

thanks in advance :)

Kev.

What I would like to do is split the first array into 2 arrays, the second in 2, and the third into 3 (therefore ungrouping the groupconcat if you will)

 

^^^ Then why did you use GROUP BY in the first place?

 

What end result are you trying to achieve, because combining data only to undo it later wastes a lot of processing time. I suspect that you just need to ORDER BY the correct column(s) and iterate over the data in your presentation logic.

Hi thanks for your reply, sorry I probably didn't explain correctly,  the data is unfortunately already grouped when I receive it so I can't change this :(

 

I need to split the values @ AAAAAA, BBBBBB, CCCCCC etc so that I can manipulate this data separately. - It just shouldn't be grouped in the first place, hence my problem!

any more help would be greatly appreciated :)

<?php
$confirmed[] = Array('AAAAAA-BBBBBB',2,'2010-09-29');
$confirmed[] = Array('AAAAAA-BBBBBB',2,'2010-09-28');
$confirmed[] = Array('CCCCCC-DDDDDD-EEEEEE',3,'2010-09-29');

$seperated = array();
foreach($confirmed as $arr){
$parts = explode('-',$arr[0]);
foreach($parts as $part){
	$seperated[] = array($part,$arr[1],$arr[2]);
}
}

echo '<pre>',print_r($seperated,true),'</pre>';
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.