Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2016-11-01
5
Medium Priority
?
112 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
[X]
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
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 59

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

PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
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…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…

704 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