Jump to content

Split array every 3 commas & return as string?


AquariaXI
Go to solution Solved by AquariaXI,

Recommended Posts

Hi! So what I'm trying to do is split a multi dimensional array of strings ( each containing 3 strings inside of 1 array ) so I can write the results to a file.

So say I had a bunch of strings inside 1 array 

class File {
    function WriteFile ( $filename, $content ) {
        $list = [
            $content
        ];
        $delimiters = File :: checkDelimiter (
            $content, 
            true, 
            false
        );
        $fp = fopen (
            $filename, 'w'
        );
        foreach ( $list as $fields ) {
            $fields = File :: SplitFileByDelimiter (
                $fields, 
                ",", 
                3
            );
            file_put_contents (
                $filename, $fields
            );
        }
        fclose ( $fp );
    }
}


$file-> File :: WriteFile (
    $filepath . $filename, 
    "TEST!, TEST!, TEST!, " . 
    "TEST!, TEST!, TEST!, " . 
    "TEST!, TEST!, TEST!"
);
	

Instead of writing

TEST!, TEST!, TEST!, TEST!, TEST!, TEST!, TEST! ,TEST!, TEST!, TEST!
	

to the file in a straight line, it should write it like this

TEST!, TEST!, TEST!, 
TEST!, TEST!, TEST!, 
TEST!, TEST!, TEST!
	

Any help is ABSOLUTELY appreciated!

 

Edited by AquariaXI
Link to comment
Share on other sites

  • AquariaXI changed the title to Split array every 3 commas & return as string?

@requinix 

function checkDelimiter ( $content, $throwExceptionOnNonUnique = true, $expectSingleColumn = false ) {

	// Would be cleaner if you pass the delimiters from outside
	// as also the order matters in the special case you've got something like "a,b;c"
	// & you don't throw the exception - then the first match is prefered
	// But for StackOverflow I put them inside

	$delimiters = [
		"\t", ";", "|", 
		","
	];

	$result = ',';
	$maxCount = 0;

	foreach ( $delimiters as $delimiter ) {

		// Impress your code reviewer by some good regex

		$pattern = "/(?<!\\\)(?:\\\\\\\)*(?!\B\"[^\\\"]*)\\" . 
				   $delimiter . 
				   "(?![^\"]*\\\"\B)/";

		$amount = preg_match_all (
			$pattern, $content
		);

		if ( $maxCount > 0 && $amount > 0 && $throwExceptionOnNonUnique ) {
			$msg = 'Identifier is not clear : "' . 
				   $result . 
				   '" & "' . 
				   $delimiter . 
				   '" are possible';
			throw new Exception ( $msg );
		}

		if ( $amount > $maxCount ) {
			$maxCount = $amount;
			$result = $delimiter;
		}

	}

	// If nothing matches & you don't expect that just the value consists of one single column without a delimeter at the end

	if ( $maxCount === 0 && ! $expectSingleColumn ) {
		throw new Exception (
			'Unknown delimiter'
		);
	}

	return $result;

}

 

function SplitFileByDelimiter ( $content, $delimiter = ",", $delimCount = 3 ) {
    // Turn array into string
    $content = implode (
        $delimiter, 
        $content
    );
    return $content;
}
Edited by AquariaXI
Link to comment
Share on other sites

This:

$file-> File :: WriteFile (
    $filepath . $filename, 
    "TEST!, TEST!, TEST!, " . 
    "TEST!, TEST!, TEST!, " . 
    "TEST!, TEST!, TEST!"
);

Is that what it has to be? Because you act like you want those three "lines" of TESTs to be three separate strings, but if you concatenate them like that then you only have one string, and splitting them apart into multiple strings again leads to this whole mess about splitting delimiters.

You also talk about a multidimensional array, but you don't have that here. You have an array containing one string.
And you have code that uses fopen/fclose but does absolutely nothing with them because you actually use file_put_contents.
And there's the comment which makes it super obvious that you copy/pasted the code from StackOverflow.
So I don't really get the impression that you know what's going on here. And if you don't know then how am I supposed to know?

Is the "TEST" string supposed to represent some different string? CSV string, maybe? Is it written in code or pulled from a database or submitted by a user through a form or what?

Not asking to be annoying. I'm asking because this whole thing doesn't make any sense, and I can only assume that it's supposed to make sense, so clearly I'm missing something important.

Link to comment
Share on other sites

2 hours ago, AquariaXI said:

@requinix Well yes, it IS supposed to be CSV, but unless I have to, I don't want to use fOpenCSV ( ) or w/e that function is. I just want to turn the raw arrays into a string then split them at their 3rd comma.

If the data is CSV then you need to treat it like CSV data. Because that's what it is. If you try to pretend it is something else then you'll run into problems.

It's trivial to take an array with a bunch of things and "group" them into 3s - once you get it into that form.

Back to this code:

$file-> File :: WriteFile (
    $filepath . $filename, 
    "TEST!, TEST!, TEST!, " . 
    "TEST!, TEST!, TEST!, " . 
    "TEST!, TEST!, TEST!"
);

I imagine that is not the real code you want to use. What is the real code? The data you're trying to write to the file: what does it actually look like?

Link to comment
Share on other sites

I'll try again but even more explicitly.

1. You need to call WriteFile with certain arguments. The first one is the file path and name, great. Now think about the second one. Is it a variable?
2. Take that variable, put a real "value" into it, and print_r() it. What is the output?
3. Given that particular output, exactly what is it that you want to write to the file?

Link to comment
Share on other sites

Dude, you're really not helping me here. Try answering my questions in a way that does not repeat the same thing you've said before, okay?

I do not want a description of the inputs. I do not want you to talk about "it's a hex color". I don't care whether it's a hex value or a list of animals I can see at my local zoo or how many cups of vanilla extract I have to bake to fill my house with its delicious smell.
What I do want is some literal values. I want you to take that variable you said you have, var_dump() it into a webpage or as console output (I said print_r() before but I've changed my mind), and literally copy-and-paste that into a post.

For example, if your real code is

$file-> File :: WriteFile (
    $filepath . $filename,
    $test
);

and $test had that "TEST" stuff you demonstrated, then I would expect your next post to contain, somewhere inside it,

string(61) "TEST!, TEST!, TEST!, TEST!, TEST!, TEST!, TEST!, TEST!, TEST!"

 

For bonus points, you could even tell me literally what you want to see saved into the file - again, literally what you want, not a description of it but the exact content of the file - in case I don't understand correctly what you're trying to say about the grouping by 3 or the weird trailing commas or whatever.

But anyway, here's the thing. I don't think you have a variable named $test containing that "TEST" stuff. I assume it's either an array or a string, but maybe it isn't, and I assume the array (if it is one) contains multiple values or the string (if it is one) contains multiple comma-separated things inside it (which may or may not constitute a valid CSV line).
My problem is that I don't think it's one thing and I do assume it's something else, and that's all I have to go on. And guessing like that does not help me to help you to put the contents of that variable into the file in the way you want it do be done.

Link to comment
Share on other sites

@requinix Now I think I understand you. If I don't, I apologize in advance. SO.... 1st, here's all the functions used to write to this file ( which is a CSV in this case )

includes/file-mainframe.php : 

	<?php
	    function implodeAll ( $glue, $arr ) {
	        for ( $i = 0; $i < count ( $arr ); $i++ ) {
	            if ( @is_array ( $arr [ $i ] ) ) {
                $arr [ $i ] = CSV :: implodeAll (
                    $glue, $arr [ $i ]
                );
            }
	        }
	    }
	    function SplitFileByDelimiter ( $content, $delimiter = ",", $delimCount = 3 ) {
	        $content = [ $content ];
        
        $content = implode (
            $delimiter, 
            $content
        );
	        return $content;
	    }
	    function checkDelimiter ( $content, $throwExceptionOnNonUnique = true, $expectSingleColumn = false ) {
	        // Would be cleaner if you pass the delimiters from outside
        // as also the order matters in the special case you've got something like "a,b;c"
        // & you don't throw the exception - then the first match is prefered
        // But for StackOverflow I put them inside
	        $delimiters = [
            "\t", ";", "|", 
            ","
        ];
	        $result = ',';
        $maxCount = 0;
	        foreach ( $delimiters as $delimiter ) {
	            // Impress your code reviewer by some good regex 
	            $pattern = "/(?<!\\\)(?:\\\\\\\)*(?!\B\"[^\\\"]*)\\" . 
                       $delimiter .
                       "(?![^\"]*\\\"\B)/";
	            $amount = preg_match_all (
                $pattern, $content
            );
	            if ( $maxCount > 0 && $amount > 0 && $throwExceptionOnNonUnique ) {
	                $msg = 'Identifier is not clear : "' . 
                $result . 
                '" & "' . 
                $delimiter . 
                '" are possible';
	                throw new Exception ( $msg );
	            }
	            if ( $amount > $maxCount ) {
	                $maxCount = $amount;
                $result = $delimiter;
	            }
	        }
	        // If nothing matches & you don't expect that just the CSV just
        // consists of one single column without a delimeter at the end
	        if ( $maxCount === 0 && ! $expectSingleColumn ) {
	            throw new Exception (
                'Unknown delimiter'
            );
	        }
	        return $result;
	    }
	    function WriteFile ( $filename, ...$content ) {
	        if ( gettype ( $content ) === 'array' ) {
            $content = File :: implodeAll ( ', ', $content );
        }
	        $list = [ $content ];
	        $delimiters = File :: checkDelimiter (
            $content, 
            true, 
            false
        );
	        $fp = fopen (
            $filename, 'w'
        );
	        foreach ( $list as $fields ) {
	            $fields = File :: SplitFileByDelimiter (
                $fields, 
                ",", 
                3
            );
            
            file_put_contents (
                $filename, $fields
            );
	        }
	        fclose ( $fp );
	    }
	    function ReadCSV ( $filename ) {
	        // Read a CSV file
	        $handle = fopen (
            $filename, "r"
        );
	        // Optionally, you can keep the number of the 
        // line of the loop it's currently 
        // iterating over
	        $lineNumber = 1;
	        // Add new Data Array to dump all data
	        $myData = [ ];
	        // Iterate over every line of the file
	        while ( ( $raw_string = fgets ( $handle ) ) !== false ) {
	            // Parse the raw csv string: "1, a, b, c"
	            $row = str_getcsv (
                $raw_string
            );
	            $myData = [ $row ];
	            // into an array: ['1', 'a', 'b', 'c']
            // And do what you need to do with every line
	            var_dump (
                $myData [ 0 ]
            );
	            // Increase the current line
	            $lineNumber++;
	        }
	        fclose ( $handle );
	        return $myData [ 0 ];
	    }
	?>
	
<?php
        include_once ( 'includes/file-mainframe.php' );
        $file -> WriteFile ( $filepath . $filename, [
        "ff00ff", "ff00cc", "ff0099", 
        "ff0066", "ff0033", "ff0000", 
        "ff3300", "ff6600", "ff9900", 
        "ffcc00", "ffff00", "ccff00", 
        "99ff00", "66ff00", "33ff00", 
        "00ff00", "00ff33", "00ff66", 
        "00ff99", "00ffcc", "00ffff", 
        "00ccff", "0099ff", "0066ff", 
        "0033ff", "0000ff", "3300ff", 
        "6600ff", "9900ff", "cc00ff", 
        "9900ff", "6600ff", "3300ff", 
        "0000ff", "0033ff", "0066ff", 
        "0099ff", "00ccff", "00ffff", 
        "00ffcc", "00ff99", "00ff66", 
        "00ff33", "00ff00", "33ff00", 
        "66ff00", "99ff00", "ccff00", 
        "ffff00", "ffcc00", "ff9900", 
        "ff6600", "ff3300", "ff0000", 
        "ff0033", "ff0066", "ff0099", 
        "ff00cc", "ff00ff"
    ] );
    $fileData = $file -> ReadCSV (
        $filepath . $filename
    );
    echo var_dump ( $fileData );
    die ( );
?>

So when I use this in var_dump ( ), I get 

array(59) { [0]=> string(6) "ff00ff" [1]=> string(7) " ff00cc" [2]=> string(7) " ff0099" [3]=> string(7) " ff0066" [4]=> string(7) " ff0033" [5]=> string(7) " ff0000" [6]=> string(7) " ff3300" [7]=> string(7) " ff6600" [8]=> string(7) " ff9900" [9]=> string(7) " ffcc00" [10]=> string(7) " ffff00" [11]=> string(7) " ccff00" [12]=> string(7) " 99ff00" [13]=> string(7) " 66ff00" [14]=> string(7) " 33ff00" [15]=> string(7) " 00ff00" [16]=> string(7) " 00ff33" [17]=> string(7) " 00ff66" [18]=> string(7) " 00ff99" [19]=> string(7) " 00ffcc" [20]=> string(7) " 00ffff" [21]=> string(7) " 00ccff" [22]=> string(7) " 0099ff" [23]=> string(7) " 0066ff" [24]=> string(7) " 0033ff" [25]=> string(7) " 0000ff" [26]=> string(7) " 3300ff" [27]=> string(7) " 6600ff" [28]=> string(7) " 9900ff" [29]=> string(7) " cc00ff" [30]=> string(7) " 9900ff" [31]=> string(7) " 6600ff" [32]=> string(7) " 3300ff" [33]=> string(7) " 0000ff" [34]=> string(7) " 0033ff" [35]=> string(7) " 0066ff" [36]=> string(7) " 0099ff" [37]=> string(7) " 00ccff" [38]=> string(7) " 00ffff" [39]=> string(7) " 00ffcc" [40]=> string(7) " 00ff99" [41]=> string(7) " 00ff66" [42]=> string(7) " 00ff33" [43]=> string(7) " 00ff00" [44]=> string(7) " 33ff00" [45]=> string(7) " 66ff00" [46]=> string(7) " 99ff00" [47]=> string(7) " ccff00" [48]=> string(7) " ffff00" [49]=> string(7) " ffcc00" [50]=> string(7) " ff9900" [51]=> string(7) " ff6600" [52]=> string(7) " ff3300" [53]=> string(7) " ff0000" [54]=> string(7) " ff0033" [55]=> string(7) " ff0066" [56]=> string(7) " ff0099" [57]=> string(7) " ff00cc" [58]=> string(7) " ff00ff" } array(59) { [0]=> string(6) "ff00ff" [1]=> string(7) " ff00cc" [2]=> string(7) " ff0099" [3]=> string(7) " ff0066" [4]=> string(7) " ff0033" [5]=> string(7) " ff0000" [6]=> string(7) " ff3300" [7]=> string(7) " ff6600" [8]=> string(7) " ff9900" [9]=> string(7) " ffcc00" [10]=> string(7) " ffff00" [11]=> string(7) " ccff00" [12]=> string(7) " 99ff00" [13]=> string(7) " 66ff00" [14]=> string(7) " 33ff00" [15]=> string(7) " 00ff00" [16]=> string(7) " 00ff33" [17]=> string(7) " 00ff66" [18]=> string(7) " 00ff99" [19]=> string(7) " 00ffcc" [20]=> string(7) " 00ffff" [21]=> string(7) " 00ccff" [22]=> string(7) " 0099ff" [23]=> string(7) " 0066ff" [24]=> string(7) " 0033ff" [25]=> string(7) " 0000ff" [26]=> string(7) " 3300ff" [27]=> string(7) " 6600ff" [28]=> string(7) " 9900ff" [29]=> string(7) " cc00ff" [30]=> string(7) " 9900ff" [31]=> string(7) " 6600ff" [32]=> string(7) " 3300ff" [33]=> string(7) " 0000ff" [34]=> string(7) " 0033ff" [35]=> string(7) " 0066ff" [36]=> string(7) " 0099ff" [37]=> string(7) " 00ccff" [38]=> string(7) " 00ffff" [39]=> string(7) " 00ffcc" [40]=> string(7) " 00ff99" [41]=> string(7) " 00ff66" [42]=> string(7) " 00ff33" [43]=> string(7) " 00ff00" [44]=> string(7) " 33ff00" [45]=> string(7) " 66ff00" [46]=> string(7) " 99ff00" [47]=> string(7) " ccff00" [48]=> string(7) " ffff00" [49]=> string(7) " ffcc00" [50]=> string(7) " ff9900" [51]=> string(7) " ff6600" [52]=> string(7) " ff3300" [53]=> string(7) " ff0000" [54]=> string(7) " ff0033" [55]=> string(7) " ff0066" [56]=> string(7) " ff0099" [57]=> string(7) " ff00cc" [58]=> string(7) " ff00ff" }

OR

array(59) {
	[0]=>
	string(6) "ff00ff"
	[1]=>
	string(7) " ff00cc"
	[2]=>
	string(7) " ff0099"
	[3]=>
	string(7) " ff0066"
	[4]=>
	string(7) " ff0033"
	[5]=>
	string(7) " ff0000"
	[6]=>
	string(7) " ff3300"
	[7]=>
	string(7) " ff6600"
	[8]=>
	string(7) " ff9900"
	[9]=>
	string(7) " ffcc00"
	[10]=>
	string(7) " ffff00"
	[11]=>
	string(7) " ccff00"
	[12]=>
	string(7) " 99ff00"
	[13]=>
	string(7) " 66ff00"
	[14]=>
	string(7) " 33ff00"
	[15]=>
	string(7) " 00ff00"
	[16]=>
	string(7) " 00ff33"
	[17]=>
	string(7) " 00ff66"
	[18]=>
	string(7) " 00ff99"
	[19]=>
	string(7) " 00ffcc"
	[20]=>
	string(7) " 00ffff"
	[21]=>
	string(7) " 00ccff"
	[22]=>
	string(7) " 0099ff"
	[23]=>
	string(7) " 0066ff"
	[24]=>
	string(7) " 0033ff"
	[25]=>
	string(7) " 0000ff"
	[26]=>
	string(7) " 3300ff"
	[27]=>
	string(7) " 6600ff"
	[28]=>
	string(7) " 9900ff"
	[29]=>
	string(7) " cc00ff"
	[30]=>
	string(7) " 9900ff"
	[31]=>
	string(7) " 6600ff"
	[32]=>
	string(7) " 3300ff"
	[33]=>
	string(7) " 0000ff"
	[34]=>
	string(7) " 0033ff"
	[35]=>
	string(7) " 0066ff"
	[36]=>
	string(7) " 0099ff"
	[37]=>
	string(7) " 00ccff"
	[38]=>
	string(7) " 00ffff"
	[39]=>
	string(7) " 00ffcc"
	[40]=>
	string(7) " 00ff99"
	[41]=>
	string(7) " 00ff66"
	[42]=>
	string(7) " 00ff33"
	[43]=>
	string(7) " 00ff00"
	[44]=>
	string(7) " 33ff00"
	[45]=>
	string(7) " 66ff00"
	[46]=>
	string(7) " 99ff00"
	[47]=>
	string(7) " ccff00"
	[48]=>
	string(7) " ffff00"
	[49]=>
	string(7) " ffcc00"
	[50]=>
	string(7) " ff9900"
	[51]=>
	string(7) " ff6600"
	[52]=>
	string(7) " ff3300"
	[53]=>
	string(7) " ff0000"
	[54]=>
	string(7) " ff0033"
	[55]=>
	string(7) " ff0066"
	[56]=>
	string(7) " ff0099"
	[57]=>
	string(7) " ff00cc"
	[58]=>
	string(7) " ff00ff"
	}
	array(59) {
	[0]=>
	string(6) "ff00ff"
	[1]=>
	string(7) " ff00cc"
	[2]=>
	string(7) " ff0099"
	[3]=>
	string(7) " ff0066"
	[4]=>
	string(7) " ff0033"
	[5]=>
	string(7) " ff0000"
	[6]=>
	string(7) " ff3300"
	[7]=>
	string(7) " ff6600"
	[8]=>
	string(7) " ff9900"
	[9]=>
	string(7) " ffcc00"
	[10]=>
	string(7) " ffff00"
	[11]=>
	string(7) " ccff00"
	[12]=>
	string(7) " 99ff00"
	[13]=>
	string(7) " 66ff00"
	[14]=>
	string(7) " 33ff00"
	[15]=>
	string(7) " 00ff00"
	[16]=>
	string(7) " 00ff33"
	[17]=>
	string(7) " 00ff66"
	[18]=>
	string(7) " 00ff99"
	[19]=>
	string(7) " 00ffcc"
	[20]=>
	string(7) " 00ffff"
	[21]=>
	string(7) " 00ccff"
	[22]=>
	string(7) " 0099ff"
	[23]=>
	string(7) " 0066ff"
	[24]=>
	string(7) " 0033ff"
	[25]=>
	string(7) " 0000ff"
	[26]=>
	string(7) " 3300ff"
	[27]=>
	string(7) " 6600ff"
	[28]=>
	string(7) " 9900ff"
	[29]=>
	string(7) " cc00ff"
	[30]=>
	string(7) " 9900ff"
	[31]=>
	string(7) " 6600ff"
	[32]=>
	string(7) " 3300ff"
	[33]=>
	string(7) " 0000ff"
	[34]=>
	string(7) " 0033ff"
	[35]=>
	string(7) " 0066ff"
	[36]=>
	string(7) " 0099ff"
	[37]=>
	string(7) " 00ccff"
	[38]=>
	string(7) " 00ffff"
	[39]=>
	string(7) " 00ffcc"
	[40]=>
	string(7) " 00ff99"
	[41]=>
	string(7) " 00ff66"
	[42]=>
	string(7) " 00ff33"
	[43]=>
	string(7) " 00ff00"
	[44]=>
	string(7) " 33ff00"
	[45]=>
	string(7) " 66ff00"
	[46]=>
	string(7) " 99ff00"
	[47]=>
	string(7) " ccff00"
	[48]=>
	string(7) " ffff00"
	[49]=>
	string(7) " ffcc00"
	[50]=>
	string(7) " ff9900"
	[51]=>
	string(7) " ff6600"
	[52]=>
	string(7) " ff3300"
	[53]=>
	string(7) " ff0000"
	[54]=>
	string(7) " ff0033"
	[55]=>
	string(7) " ff0066"
	[56]=>
	string(7) " ff0099"
	[57]=>
	string(7) " ff00cc"
	[58]=>
	string(7) " ff00ff"
	}
	

I hope this helps!

Edited by AquariaXI
Link to comment
Share on other sites

  • Solution

Basically, 

"ff00ff", "ff00cc", "ff0099", 
"ff0066", "ff0033", "ff0000", 
"ff3300", "ff6600", "ff9900", 
"ffcc00", "ffff00", "ccff00", 
"99ff00", "66ff00", "33ff00", 
"00ff00", "00ff33", "00ff66", 
"00ff99", "00ffcc", "00ffff", 
"00ccff", "0099ff", "0066ff", 
"0033ff", "0000ff", "3300ff", 
"6600ff", "9900ff", "cc00ff", 
"9900ff", "6600ff", "3300ff", 
"0000ff", "0033ff", "0066ff", 
"0099ff", "00ccff", "00ffff", 
"00ffcc", "00ff99", "00ff66", 
"00ff33", "00ff00", "33ff00", 
"66ff00", "99ff00", "ccff00", 
"ffff00", "ffcc00", "ff9900", 
"ff6600", "ff3300", "ff0000", 
"ff0033", "ff0066", "ff0099", 
"ff00cc", "ff00ff"

is what I want to see in the file ( YES, including the double quotes ) & YES, in the format seen above. After every 3rd comma, starts a new line.

 

Edited by AquariaXI
Link to comment
Share on other sites

Okay... So basically, that stuff you wrote into the code, you want it to appear just like that in the file?

Have you tried forgetting complicated logic about splitting arrays into groups of three and simply writing a string to a file?

Link to comment
Share on other sites

One significant problem with your request is you started out by saying it was a multi-dimensional array when it is , in fact, a flat array. But, your responses have been far from illustrative. Understand that you are very close to the problem and KNOW what is in your head. So, while you think a statement is perfectly obvious, to others it is greek.

In any case, if you have an array that you want to "split" into "chunks" of an arbitrary size, PHP has a built-in function to do that: array_chunk()

//Input data
$outputFile = "output.csv";
$itemsPerLine = 3;

$hexCodes = array("ff00ff", "ff00cc", "ff0099", 
        "ff0066", "ff0033", "ff0000", "ff3300", "ff6600", "ff9900", 
        "ffcc00", "ffff00", "ccff00", "99ff00", "66ff00", "33ff00", 
        "00ff00", "00ff33", "00ff66", "00ff99", "00ffcc", "00ffff", 
        "00ccff", "0099ff", "0066ff", "0033ff", "0000ff", "3300ff", 
        "6600ff", "9900ff", "cc00ff", "9900ff", "6600ff", "3300ff", 
        "0000ff", "0033ff", "0066ff", "0099ff", "00ccff", "00ffff", 
        "00ffcc", "00ff99", "00ff66", "00ff33", "00ff00", "33ff00", 
        "66ff00", "99ff00", "ccff00", "ffff00", "ffcc00", "ff9900", 
        "ff6600", "ff3300", "ff0000", "ff0033", "ff0066", "ff0099", 
        "ff00cc", "ff00ff");

//Open file for writing
$fileHandle = fopen($outputFile, 'w');

//Split the array into chunks and write each chunk to the file
//with a comma separator and a line feed character
foreach (array_chunk($hexCodes, $itemsPerLine) as $line) {
    fwrite($fileHandle, implode(',', $line) . "\n");
}

//Close the file
fclose($fileHandle);

Here is the output

ff00ff,ff00cc,ff0099
ff0066,ff0033,ff0000
ff3300,ff6600,ff9900
ffcc00,ffff00,ccff00
99ff00,66ff00,33ff00
00ff00,00ff33,00ff66
00ff99,00ffcc,00ffff
00ccff,0099ff,0066ff
0033ff,0000ff,3300ff
6600ff,9900ff,cc00ff
9900ff,6600ff,3300ff
0000ff,0033ff,0066ff
0099ff,00ccff,00ffff
00ffcc,00ff99,00ff66
00ff33,00ff00,33ff00
66ff00,99ff00,ccff00
ffff00,ffcc00,ff9900
ff6600,ff3300,ff0000
ff0033,ff0066,ff0099
ff00cc,ff00ff

EDIT: Or if you want the quote marks and spacing, replace with this line

    fwrite($fileHandle, '"' . implode('", "', $line) . "\"\n");

 

Edited by Psycho
Link to comment
Share on other sites

If you need to write other hex codes to the file, what are you going to do? Add them to your list in the code?

Basically, what I'm saying is, if you have this code now

<?php
        include_once ( 'includes/file-mainframe.php' );
        $file -> WriteFile ( $filepath . $filename, [
        "ff00ff", "ff00cc", "ff0099", 
        "ff0066", "ff0033", "ff0000", 
        "ff3300", "ff6600", "ff9900", 
        "ffcc00", "ffff00", "ccff00", 
        "99ff00", "66ff00", "33ff00", 
        "00ff00", "00ff33", "00ff66", 
        "00ff99", "00ffcc", "00ffff", 
        "00ccff", "0099ff", "0066ff", 
        "0033ff", "0000ff", "3300ff", 
        "6600ff", "9900ff", "cc00ff", 
        "9900ff", "6600ff", "3300ff", 
        "0000ff", "0033ff", "0066ff", 
        "0099ff", "00ccff", "00ffff", 
        "00ffcc", "00ff99", "00ff66", 
        "00ff33", "00ff00", "33ff00", 
        "66ff00", "99ff00", "ccff00", 
        "ffff00", "ffcc00", "ff9900", 
        "ff6600", "ff3300", "ff0000", 
        "ff0033", "ff0066", "ff0099", 
        "ff00cc", "ff00ff"
    ] );

then why not simply turn it into

<?php
        include_once ( 'includes/file-mainframe.php' );
        file_put_contents ( $filepath . $filename, <<<STRING
"ff00ff", "ff00cc", "ff0099", 
"ff0066", "ff0033", "ff0000", 
"ff3300", "ff6600", "ff9900", 
"ffcc00", "ffff00", "ccff00", 
"99ff00", "66ff00", "33ff00", 
"00ff00", "00ff33", "00ff66", 
"00ff99", "00ffcc", "00ffff", 
"00ccff", "0099ff", "0066ff", 
"0033ff", "0000ff", "3300ff", 
"6600ff", "9900ff", "cc00ff", 
"9900ff", "6600ff", "3300ff", 
"0000ff", "0033ff", "0066ff", 
"0099ff", "00ccff", "00ffff", 
"00ffcc", "00ff99", "00ff66", 
"00ff33", "00ff00", "33ff00", 
"66ff00", "99ff00", "ccff00", 
"ffff00", "ffcc00", "ff9900", 
"ff6600", "ff3300", "ff0000", 
"ff0033", "ff0066", "ff0099", 
"ff00cc", "ff00ff"
STRING
	);

and there you go: written to the file exactly the way you want by virtue of the fact that you told PHP to write it to the file exactly the way you want.

Link to comment
Share on other sites

Here's what that wrote to the file

ff00ff, ff00cc, ff0099, ff0066, ff0033, ff0000, ff3300, ff6600, ff9900, ffcc00, ffff00, ccff00, 99ff00, 66ff00, 33ff00, 00ff00, 00ff33, 00ff66, 00ff99, 00ffcc, 00ffff, 00ccff, 0099ff, 0066ff, 0033ff, 0000ff, 3300ff, 6600ff, 9900ff, cc00ff, 9900ff, 6600ff, 3300ff, 0000ff, 0033ff, 0066ff, 0099ff, 00ccff, 00ffff, 00ffcc, 00ff99, 00ff66, 00ff33, 00ff00, 33ff00, 66ff00, 99ff00, ccff00, ffff00, ffcc00, ff9900, ff6600, ff3300, ff0000, ff0033, ff0066, ff0099, ff00cc, ff00ff
	

It is all one line. It needs to look exactly like the hex I wrote in the code above.

Edited by AquariaXI
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.