Solved

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

Posted on 2016-11-01
5
64 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 108

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 16

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 108

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 51

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
spacing 5 32
php image upload 3 27
Element alignment and word wrapping 9 28
update field on focusout 15 24
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now