I have an array of file names which gets split into 3's using array_chunk, then using array_push "X" is added to each array chunk. I now want to to reassemble the array chunks into one "2D" array. I have been playing around with array_merge, but I can only get the last array chunk to merge over and over. So...

I have an array ($filesOUT) that looks like this:

Array ( 
   [0] => DSCF2361A1.jpg [1] => DSCF2373.JPG [2] => DSCF2383.JPG [3] => X 
) 
Array ( 
   [3] => DSCF2347.JPG [4] => DSCF2335.JPG [5] => DSCF2366.JPG [6] => X 
) 
Array ( 
   [6] => IMG_3864.JPG [7] => DSCF2375.JPG [8] => DSCF2336.JPG [9] => X 
) 
Array ( 
   [9] => DSCF2369.JPG [10] => DSCF2346.JPG [11] => DSCF2378.JPG [12] => X 
) 
Array ( 
   [12] => IMG_3861.JPG [13] => DSCF2368.JPG [14] => IMG_3859.JPG [15] => X 
) 
Array ( 
   [15] => IMG_3867.JPG [16] => DSCF2395.JPG [17] => DSCF2357.JPG [18] => X 
) 
Array (
   [19] => IMG_3880.JPG [20] => X 
)

But I would like to merge the multidimensional array ($filesOUT) so that it becomes a single "2D array" like so:

Array ( 
   [0] => DSCF2361A1.jpg [1] => DSCF2373.JPG [2] => DSCF2383.JPG [3] => X [4] =>   DSCF2347.JPG [5] => DSCF2335.JPG [6] => DSCF2366.JPG [7] => X [8] => IMG_3864.JPG [9] => DSCF2375.JPG [10] => DSCF2336.JPG [11] => X [12] => DSCF2369.JPG [13] => DSCF2346.JPG [14] => DSCF2378.JPG [15] => X [16] => IMG_3861.JPG [17] => DSCF2368.JPG [18] => IMG_3859.JPG [19] => X [20] => IMG_3867.JPG [21] => DSCF2395.JPG [22] => DSCF2357.JPG [23] => X [24] => IMG_3880.JPG [25] => X 
)

Is this possible? (baring in mind that the array_push does not amend the key number, and causes key duplicates. SEE RED)

Failing that is there any other way I can use array_push to add "X" after every 3rd item in the array?...

If anyone can shed some light on the situation it would be greatly appreciated. Thanks :)

Instead of array_push you can just do: $yourarray[$index] = $value; If the $index already exists, it will overwrite, but you can check for that if you don't want that to happen.

$yourarray[$index] = $value;

Surely that will complicate things hugely though, as I will need various calculations to specify [$index] for each value I want to add.

I think I favour the "unchunk" method if anyone knows how it can be done?...

If you loop through your values, why not just do $newarray[] = $value; (just like array_push). I really don't see why you'd get key duplicates, as it just renumbers the new array from zero upwards (based on your sample).

I dont think im following, sorry i don't know a lot about PHP but im slowly learning :)

What I have so far is...

function array_chunk_fixed($input, $num, $preserve_keys = FALSE) {
    $count = count($input) ;
    if($count)
        $input = array_chunk($input, ceil($count/$num), $preserve_keys) ;
    $input = array_pad($input, $num, array()) ;
    return $input ;
}

print_r($files);
print "<br/>";
$fileIN=(array_chunk($files, 3, TRUE)) ;

foreach($fileIN as $fileIN)
{
array_push($fileIN, "X");
//print_r($fileIN);
//print "<br/>";
}

$fileOUT=(array_merge($fileIN));
print_r($fileOUT);
print "<br/>";

The bit in RED is the problem, cannot get the array chunks to merge into a single array.

You are overwriting your data by using two identical variables

foreach($fileIN as $fileIN) { ... }

This should be:

foreach($fileIN as $value) { array_push($value, "X"); }

Then I guess the array_merge will work just fine.

You are overwriting your data by using two identical variables

foreach($fileIN as $fileIN) { ... }

This should be:

foreach($fileIN as $value) { array_push($value, "X"); }

Then I guess the array_merge will work just fine.

Ive given it ago but now its not adding X..

function array_chunk_fixed($input, $num, $preserve_keys = FALSE) {
    $count = count($input) ;
    if($count)
        $input = array_chunk($input, ceil($count/$num), $preserve_keys) ;
    $input = array_pad($input, $num, array()) ;
    return $input ;
}

print_r($files);
print "<br/>";
$fileIN=(array_chunk($files, 3, TRUE)) ;
//print_r($fileOUT);
//print "<br/>";

foreach($fileIN as $value)
{
array_push($value, "X");
}
$fileOUT=(array_merge($fileIN));
print_r($fileOUT);
print "<br/>";

It returns:

Array ( 
 [0] => Array (
  [0] => DSCF2361A1.jpg [1] => DSCF2373.JPG [2] => DSCF2383.JPG ) 
 [1] => Array (
  [3] => DSCF2347.JPG [4] => DSCF2335.JPG [5] => DSCF2366.JPG )
 [2] => Array (
  [6] => IMG_3864.JPG [7] => DSCF2375.JPG [8] => DSCF2336.JPG ) 
 [3] => Array (
  [9] => DSCF2369.JPG [10] => DSCF2346.JPG [11] => DSCF2378.JPG )
 [4] => Array (
  [12] => IMG_3861.JPG [13] => DSCF2368.JPG [14] => IMG_3859.JPG )
 [5] => Array (
  [15] => IMG_3867.JPG [16] => DSCF2395.JPG [17] => DSCF2357.JPG ) 
)

what is $value being used for here? surely i need to state what $value equals to somewhere? Thanks.

I think this should work:

$fileIN = array_chunk($files, 3, true);
$fileOUT = array ();
foreach ($fileIN as $chunk)
{
  foreach ($chunk as $value)
  {
    $fileOUT[] = $value;
  }
  $fileOUT[] = "X";
}
print_r($fileOUT);

Wow that worked perfectly. I have been struggling with that for days! Thanks a lot. My photo galleries are now display thumbnails in nice straight rows. :)

The final code is:

function array_chunk_fixed($input, $num, $preserve_keys = FALSE) {
    $count = count($input) ;
    if($count)
        $input = array_chunk($input, ceil($count/$num), $preserve_keys) ;
    $input = array_pad($input, $num, array()) ;
    return $input ;
}

$fileIN = array_chunk($files, 3, true);
$fileOUT = array ();
foreach ($fileIN as $chunk)
{
  foreach ($chunk as $value)
  {
    $fileOUT[] = $value;
  }
  $fileOUT[] = "LINE";
}
//print_r($fileOUT);

foreach ($fileOUT as $file)
{
  if ($file=="LINE")
    {
      echo "<div class=\"spacer\"></div>";
    }
      else if ($file!=="LINE")
	{
	  echo "<div class=\"picbox\">";
          echo "<a href=\"".$big.$file ."\" title=\"".$galltitle."\"><img src=\"".$images.$file."\" />";
          echo "</div>";
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.