Solved

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

Posted on 2016-11-01
5
86 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 109

Accepted Solution

by:
Ray Paseur earned 250 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 125 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 109

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 54

Assisted Solution

by:Julian Hansen
Julian Hansen earned 125 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create animated movies for web page 17 39
Wordpress plugins not working in html 3 25
PHP Syntax Error 4 27
Un-Indent Setting Notepad++ 2 5
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

773 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