Combine two arrays of points into one

At the moment I am receiving two arrays from some javascript that I am writing to a text file.

The array contains:

"Array
(
    [points1] => 107,132,177,283,303,193
    [points2] => 130,137,184,254,416,129
)
"

The text file contains:


107,132
177,283
303,193

130,137
184,254
416,129

And the php code to do the conversion is:


$points = explode(',', $_POST['points1']);
$chunks = array_chunk($points, 2);
$chunks = array_map(function($arr) { return implode(',', $arr); }, $chunks);
$result1 = implode(PHP_EOL, $chunks);

$points = explode(',', $_POST['points2']);
$chunks = array_chunk($points, 2);
$chunks = array_map(function($arr) { return implode(',', $arr); }, $chunks);
$result2 = implode(PHP_EOL, $chunks);

$write = $result1 . PHP_EOL . PHP_EOL . $result2;

// Write to a text file
$file = 'test_output.txt';
$fp = fopen($file, "w");
fwrite($fp, $write);
fclose($fp);

I need the out put in a different format, the first set of points1 with the first set of points2 then a line break and then the second set of points1 with the second set of points2


107,132,130,137
177,283,184,254
303,193,416,129

Please can somebody help me out with this.

I have something working with the help of Google!
Output from the consul:


Array
(
    [points1] => 138,123,145,250,247,179
    [points2] => 160,77,296,77,226,232
)
<br>String =138,123,160,77,145,250,296,77,247,179,226,232"

php code - seems a lot of code to do this!



print_r($_POST);
unlink ('test_output.txt');

// Function to join the two arrays on every second set of points
function array_merge_alternating($chunks1, $chunks2) {
    if(count($chunks1) != count($chunks2)) {
        return false; // Arrays must be the same length
    }

    $mergedArray = array();

    while(count($chunks1) > 0) {
        $mergedArray[] = array_shift($chunks1);
        $mergedArray[] = array_shift($chunks2);
    }
    return $mergedArray;
}
// Function to convert the multidimensional array from first function into a string
function convert_multi_array($array) {
  $out = implode(",",array_map(function($a) {return implode(",",$a);},$array));
//  print_r($out);
  return $out;
}

// Split up the points from the two arrays
$points1 = explode(',', $_POST['points1']);
$chunks1 = array_chunk($points1, 2);

$points2 = explode(',', $_POST['points2']);
$chunks2 = array_chunk($points2, 2);

// Join the arrays into one
$combined = array_merge_alternating($chunks1, $chunks2);

//print_r($combined );
// convert the array to a string
$string = convert_multi_array($combined);
echo '<br>String ='.$string;

// Format the data ready for the text file
$points = explode(',', $string);
$chunks = array_chunk($points, 4);
$chunks = array_map(function($arr) { return implode(',', $arr); }, $chunks);
$write = implode(PHP_EOL, $chunks);

// Write to a text file
$file = 'test_output.txt';
$fp = fopen($file, "w");
fwrite($fp, $write);
fclose($fp);

Text file contents:


138,123,160,77
145,250,296,77
247,179,226,232

Try this:


<?php 

$input=array
(
  'points1' => array(107, 132, 177, 283, 303, 193),
  'points2' => array(130, 137, 184, 254, 416, 129),
);
    
$desired = <<<START
DESIRED:
    107,132,130,137
    177,283,184,254
    303,193,416,129
START;


$file   ='test_output.txt';

$output = reformat( $input );
writetoFile( $output, $file );

if(10) // DEBUG 
{
    echo '<hr /><pre>';
        echo '<br /> $input => '; print_r($input);
        echo '<br /> $desired => '. $desired .'<br />';
        echo '<br />File: '.$file;
        highlight_file($file);
    echo '</pre>';
}


//===============================
//===============================
function reformat($input=array())
{
    $result = array();

    $tmp = array_merge($input['points1'], $input['points2']);
    foreach($tmp as $index => $line)
    {
        if($index<6)
        {
          if( 0 === $index % 2 )
          {
                $result[] = $line .',' 
                             . $tmp[1+$index] .',' 
                             . $tmp[6+$index] .',' 
                             . $tmp[7+$index] ;
            }
        }
    }

    return $result;
}


//===============================
// Write $output to a text file
//===============================
function writetoFile($output=array(), $file='test_output.txt')
{
    $fp = fopen($file, "w");
    foreach($output as $line)
    {
        fwrite($fp, "\	");
      fwrite($fp, $line );
        fwrite($fp, "\
");
    }
    fclose($fp);  
}


Thanks for the code John and I have given it a test and with your data it works; now I need to implement it into my code. BUT I have side tracked off onto another little project and will come back to it soon.

So much to do and so little time!