Php implode multidimensional array for tabbed lines

I have a multidimensional array $BlockData[]that has 13 dimensions in it and the number of elements is 'n'. I need to insert this array back into one long line, where the elements are separated by a line of lines "\n"and the sizes are separated by tabs "\t".

I tried to use the function array_map()without success and I need help in its implementation. Please, help!

+3
source share
3 answers

Here is the option I suggested yesterday in the chat:

$callback = function($value) { 
    return implode("\t", $value); 
};
echo implode("\n", array_map($callback, $BlockData));

Or if you are using PHP <5.3 (5.2, 5.1, 5.0, etc.)

$callback = create_function('$value', 'return implode("\t", $value);');
echo implode("\n", array_map($callback, $BlockData));
+2
source

This can be done using a recursive function.

<?php

function r_implode( $pieces )
{
  foreach( $pieces as $r_pieces )
  {
    if( is_array( $r_pieces ) )
    {
      $retVal[] = "\t". r_implode( $r_pieces );
    }
    else
    {
      $retVal[] = $r_pieces;
    }
  }
  return implode("\n", $retVal );
}

$test_arr = array( 0, 1, array( 'a', 'b' ), array( array( 'x', 'y'), 'z' ) );
echo r_implode( $test_arr ) . "\n";
$test_arr = array( 0 );
echo r_implode( $test_arr ) . "\n";
?>
+5
source
 $lines = array();
 foreach($BlockData as $data) {
      $lines[] = implode("\t", $data);
 }

 echo implode("\n", $lines);

@Alex , . .

0

All Articles