Page 1 of 1

array_chunk doesn't return arrays inside arrays

Posted: Wed Nov 17, 2010 9:06 am
by scuar
Hello i was writing a simple test for my script:

$argv[6] it's the name of a file inside the same folder of the script
$argv[9] it's a number

Code: Select all

			
$Total = array_chunk((file($argv[6], FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)),$argv[9]); 

if(is_array($Total[1]))
{
	fwrite(fopen("testArray.txt",'w+'),"it's an array");die;
}
else
{
	fwrite(fopen("testArray.txt",'w+'),"it's not an array");die;
}
But why $Total[1] it's not an array? array_chunk should put several arrays into $Total.

Re: array_chunk doesn't return arrays inside arrays

Posted: Wed Nov 17, 2010 10:04 am
by AbraCadaver
Would need to see:

Code: Select all

echo $argv[9];
//and
var_export( file($argv[6], FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) );

Re: array_chunk doesn't return arrays inside arrays

Posted: Wed Nov 17, 2010 10:50 am
by scuar
echo $argv[9]; prints "2", that's ok. But i use:

Code: Select all

fwrite(fopen("testArray.txt",'w+'),var_export( file($argv[6], FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) ));die;
And i got squat, the file was empty.

Also tried

Code: Select all

fwrite(fopen("testArray.txt",'w+'),file($argv[6], FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) );die;
But the test file was empty too.

PS: I can't just print it since i'm executing a web script to execute a cli script (wich is the same one).

Re: array_chunk doesn't return arrays inside arrays

Posted: Wed Nov 17, 2010 11:00 am
by AbraCadaver
I didn't say to do all that, just:

Code: Select all

var_export( file($argv[6], FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) );
Also, make sure you have this at the top:

Code: Select all

error_reporting(E_ALL);
ini_set('display_errors', '1');
If the file you are reading is empty then what you are experiencing makes sense.

Re: array_chunk doesn't return arrays inside arrays

Posted: Wed Nov 17, 2010 12:05 pm
by scuar
Hey mate, thanks for the answer. I'm sure the file has content, plus i will not be able to see any kind of error or warning while executing(i need to dump my output into a file).

Besides that i figure out that file() isn't working for me, but file_get_contents() it is (it's returning the file as a string and i need it as an Array tho)
Any clue why this happens?

Re: array_chunk doesn't return arrays inside arrays

Posted: Wed Nov 17, 2010 2:48 pm
by AbraCadaver
scuar wrote:Hey mate, thanks for the answer. I'm sure the file has content, plus i will not be able to see any kind of error or warning while executing(i need to dump my output into a file).

Besides that i figure out that file() isn't working for me, but file_get_contents() it is (it's returning the file as a string and i need it as an Array tho)
Any clue why this happens?
Are you sure that there are newlines in your file?

Re: array_chunk doesn't return arrays inside arrays

Posted: Wed Nov 17, 2010 5:31 pm
by scuar