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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<?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>';
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.