Link to home
Start Free TrialLog in
Avatar of Vipin Kumar
Vipin KumarFlag for India

asked on

Splitting an array and creating custom string

Hi,

I have an array like below:
$name=
Array ( [0] => Array ( [0] => mon_shift [1] => tue_shift [2] => wed_shift [3] => thu_shift [4] => fri_shift [5] => sat_shift [6] => sun_shift ) 
[1] => Array ( [0] => mon_proj [1] => tue_proj [2] => wed_proj [3] => thu_proj [4] => fri_proj [5] => sat_proj [6] => sun_proj ) 
[2] => Array ( [0] => mon_hour [1] => tue_hour [2] => wed_hour [3] => thu_hour [4] => fri_hour [5] => sat_hour [6] => sun_hour ) ) 

Open in new window


I want to get a string in below format:
mon_shift,mon_proj,mon_hour,tue_shift,tue_proj,tue_hour,wed_shift,wed_proj,wed_hour,thu_shift,thu_proj,thu_hour,fri_shift,fri_proj,fri_hour,sat_shift,sat_proj,sat_hour,sun_shift,sun_proj,sun_hour

Open in new window


When I use foreach($name as $value) i get the below output:
mon_shift,tue_shift,wed_shift,thu_shift,fri_shift,sat_shift,sun_shift,mon_proj,tue_proj,wed_proj,thu_proj,fri_proj,sat_proj,sun_proj,mon_hour,tue_hour,wed_hour,thu_hour,fri_hour,sat_hour,sun_hour

Open in new window


Kindly let me know how can I achieve my requirement.

Thanks in advance.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

It looks like what you want is a matrix transformation followed by implode().  I'll try to give you a code sample in a moment.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Vipin Kumar

ASKER

@Ray... Thanks a lot it worked perfectly fine for me.
greetings   leovipin2, , In your FOREACH string output, you do not try to reorder the 7 days list in each sub array.  For me the best way to do this, is to use a string "Section" build for each collection of shift, project, and hour by days. I make an array with seven "Day" strings, for monday through sunday. In my FOREACH I place the several day strings in the sub array Into the correct day "section of my $days array. Then I have to join the $days string together.

See if this code can help you to see a way to use the FOREACH .
I copied your desired output string below my output and they seem to match.
<!DOCTYPE html>
<html lang=en><head>
<title>Multi arrays to Ordered string</title>
</head>
<body>
<h3>Multi arrays to Ordered string</h3>
<?php
error_reporting(E_ALL);

$mutiAry = array(
  array('mon_shift','tue_shift','wed_shift','thu_shift','fri_shift','sat_shift','sun_shift'),
  array('mon_proj', 'tue_proj','wed_proj', 'thu_proj', 'fri_proj', 'sat_proj', 'sun_proj'),
  array('mon_hour', 'tue_hour','wed_hour', 'thu_hour', 'fri_hour', 'sat_hour', 'sun_hour')
);

$days = array('','','','','','','');
foreach ($mutiAry as $ary) {
for ($i = 0; $i < count($ary); ++$i) {
	$days[$i] .= $ary[$i].',';
    }
  } // end of FOREACH
	  
$str1 = implode('', $days);
$str1 = substr($str1,0,-1);
echo 'Here is the comma delimited DAYS string -<br />',$str1,'<br />
mon_shift,mon_proj,mon_hour,tue_shift,tue_proj,tue_hour,wed_shift,wed_proj,wed_hour,thu_shift,thu_proj,thu_hour,fri_shift,fri_proj,fri_hour,sat_shift,sat_proj,sat_hour,sun_shift,sun_proj,sun_hour';
 ?>

</body>
</html>

Open in new window

Sorry I was eatin lunch, while trying to code, I did not know it was accepted answer, happened while I was eatin.