954,604 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Merging a multidimensional array into 2D array...

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 :)

NuGG
Junior Poster
111 posts since Feb 2005
Reputation Points: 10
Solved Threads: 1
 

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.

pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 
$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?...

NuGG
Junior Poster
111 posts since Feb 2005
Reputation Points: 10
Solved Threads: 1
 

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).

pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

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 inRED is the problem, cannot get the array chunks to merge into a single array.

NuGG
Junior Poster
111 posts since Feb 2005
Reputation Points: 10
Solved Threads: 1
 

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.

pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

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.

NuGG
Junior Poster
111 posts since Feb 2005
Reputation Points: 10
Solved Threads: 1
 

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);
pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

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>";
	}
}
NuGG
Junior Poster
111 posts since Feb 2005
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: