Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2016-11-01
5
91 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 55

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
This video teaches users how to migrate an existing Wordpress website to a new domain.
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).

828 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