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.
LVL 1
Vipin KumarSr. Network EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
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.
0
Ray PaseurCommented:
See if this does the trick.
<?php // demo/temp_leovipin2.php

/**
 * http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28679761.html
 */
error_reporting(E_ALL);

$name=
Array (
Array ( '0' => 'mon_shift', '1' => 'tue_shift', '2' => 'wed_shift', '3' => 'thu_shift', '4' => 'fri_shift', '5' => 'sat_shift', '6' => 'sun_shift' ),
Array ( '0' => 'mon_proj',  '1' => 'tue_proj',  '2' => 'wed_proj',  '3' => 'thu_proj',  '4' => 'fri_proj',  '5' => 'sat_proj',  '6' => 'sun_proj' ),
Array ( '0' => 'mon_hour',  '1' => 'tue_hour',  '2' => 'wed_hour',  '3' => 'thu_hour',  '4' => 'fri_hour',  '5' => 'sat_hour',  '6' => 'sun_hour' ),
)
;
// print_r($name);

// SEPARATE THE ARRAYS
$s = $name[0];
$p = $name[1];
$h = $name[2];

// COUNT OF ELEMENTS
$n = count($s);

// CATCH THE OUTPUT HERE
$str = NULL;

for ($i=0; $i < $n; $i++)
{
    $str .= $s[$i] . ',' . $p[$i] . ',' . $h[$i] . ',';
}

$str = rtrim($str, ',');
var_dump($str);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vipin KumarSr. Network EngineerAuthor Commented:
@Ray... Thanks a lot it worked perfectly fine for me.
0
Slick812Commented:
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

0
Slick812Commented:
Sorry I was eatin lunch, while trying to code, I did not know it was accepted answer, happened while I was eatin.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.