Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How can I split the array as 4 rows per line?

Posted on 2016-11-01
5
Medium Priority
?
116 Views
Last Modified: 2016-11-01
I have comma separated values as beblow in array

$array="IAM-00001-04684.jpg,IAM-00001-04685.jpg,IAM-00001-04686.jpg,IAM-00001-04687.jpg,IAM-00002-04688.jpg,IAM-00002-04689.jpg,IAM-00002-04690.jpg,IAM-00002-04691.jpg,IAM-00003-04692.jpg,IAM-00003-04693.jpg,IAM-00003-04694.jpg,IAM-00003-04695.jpg,IAM-00004-04696.jpg,IAM-00004-04697.jpg,IAM-00004-04698.jpg,IAM-00004-04699.jpg,IAM-00005-04700.jpg,IAM-00005-04701.jpg,IAM-00005-04702.jpg,IAM-00005-04703.jpg";

I want to split & print on the page the above as below (just in a row 4 each like), if possible remove end "," on the 4th value

IAM-00001-04684.jpg,IAM-00001-04685.jpg,IAM-00001-04686.jpg,IAM-00001-04687.jpg
IAM-00002-04688.jpg,IAM-00002-04689.jpg,IAM-00002-04690.jpg,IAM-00002-04691.jpg
IAM-00003-04692.jpg,IAM-00003-04693.jpg,IAM-00003-04694.jpg,IAM-00003-04695.jpg
IAM-00004-04696.jpg,IAM-00004-04697.jpg,IAM-00004-04698.jpg,IAM-00004-04699.jpg
IAM-00005-04700.jpg,IAM-00005-04701.jpg,IAM-00005-04702.jpg,IAM-00005-04703.jpg

Please help.

Thanks
0
Comment
Question by:Loganathan Natarajan
5 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1000 total points
ID: 41868422
<?php // demo/temp_loganathan.php
/**
 * https://www.experts-exchange.com/questions/28980205/How-can-I-split-the-array-as-4-rows-per-line.html
 *
 * http://php.net/manual/en/function.array-chunk.php
 */
error_reporting(E_ALL);
echo '<pre>';

// TEST DATA FROM THE POST AT E-E
$array="IAM-00001-04684.jpg,IAM-00001-04685.jpg,IAM-00001-04686.jpg,IAM-00001-04687.jpg,IAM-00002-04688.jpg,IAM-00002-04689.jpg,IAM-00002-04690.jpg,IAM-00002-04691.jpg,IAM-00003-04692.jpg,IAM-00003-04693.jpg,IAM-00003-04694.jpg,IAM-00003-04695.jpg,IAM-00004-04696.jpg,IAM-00004-04697.jpg,IAM-00004-04698.jpg,IAM-00004-04699.jpg,IAM-00005-04700.jpg,IAM-00005-04701.jpg,IAM-00005-04702.jpg,IAM-00005-04703.jpg";

// ACTIVATE THIS TO SHOW THE TEST DATA (IT IS NOT AN ARRAY - IT IS A STRING)
// print_r($array);

// MAKE THE STRING INTO AN ARRAY
$array = explode(',', $array);

// MAKE A TWO-DIMENSIONAL MATRIX
$matrix = array_chunk($array,4);

// MAKE A PRINTED REPRESENTATION TO MATCH THE QUESTION
$out = NULL;
foreach ($matrix as $arr)
{
    $out .= PHP_EOL . implode(',', $arr);
}
echo $out;

Open in new window

1
 
LVL 17

Assisted Solution

by:Chris Harte
Chris Harte earned 500 total points
ID: 41868530
array_chunk() is a new one on me, so I did it the old fashioned way

echo "<pre>";

$array = "IAM-00001-04684.jpg,IAM-00001-04685.jpg,IAM-00001-04686.jpg,IAM-00001-04687.jpg,IAM-00002-04688.jpg,IAM-00002-04689.jpg,IAM-00002-04690.jpg,IAM-00002-04691.jpg,IAM-00003-04692.jpg,IAM-00003-04693.jpg,IAM-00003-04694.jpg,IAM-00003-04695.jpg,IAM-00004-04696.jpg,IAM-00004-04697.jpg,IAM-00004-04698.jpg,IAM-00004-04699.jpg,IAM-00005-04700.jpg,IAM-00005-04701.jpg,IAM-00005-04702.jpg,IAM-00005-04703.jpg";

$arraylets =  explode (",", $array);

$x = 0;

foreach ($arraylets as $output)
{
    echo $output;
    
    if ($x != 3)
    {
        echo ", ";
        $x++;
    }
    else
    {
        echo "\n";
        $x = 0;
    }        
}

Open in new window

1
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41868883
@Chris: Yes, there are really a lot of PHP functions for dealing with arrays.  I counted 79, which is more than the count of all of the instructions in the first programming language I learned!
1
 
LVL 60

Assisted Solution

by:Julian Hansen
Julian Hansen earned 500 total points
ID: 41869219
Just for kicks - a single pass solution
<?php
$array="IAM-00001-04684.jpg,IAM-00001-04685.jpg,IAM-00001-04686.jpg,IAM-00001-04687.jpg,IAM-00002-04688.jpg,IAM-00002-04689.jpg,IAM-00002-04690.jpg,IAM-00002-04691.jpg,IAM-00003-04692.jpg,IAM-00003-04693.jpg,IAM-00003-04694.jpg,IAM-00003-04695.jpg,IAM-00004-04696.jpg,IAM-00004-04697.jpg,IAM-00004-04698.jpg,IAM-00004-04699.jpg,IAM-00005-04700.jpg,IAM-00005-04701.jpg,IAM-00005-04702.jpg,IAM-00005-04703.jpg";
$count = 0;
for($i = 0; $i < strlen($array); $i++) {
  if ($array[$i] == ',') {
    if (++$count % 4 == 0) {
      $array[$i] = "\n";
    }
  }
}
echo "<pre>" . print_r($array, true) . "</pre>";

Open in new window

1
 
LVL 36

Author Closing Comment

by:Loganathan Natarajan
ID: 41869557
Thanks to all
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question