array_chunk doesn't return arrays inside arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scuar
Forum Newbie
Posts: 9
Joined: Wed Nov 03, 2010 10:27 pm

array_chunk doesn't return arrays inside arrays

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: array_chunk doesn't return arrays inside arrays

Post 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) );
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
scuar
Forum Newbie
Posts: 9
Joined: Wed Nov 03, 2010 10:27 pm

Re: array_chunk doesn't return arrays inside arrays

Post 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).
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: array_chunk doesn't return arrays inside arrays

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
scuar
Forum Newbie
Posts: 9
Joined: Wed Nov 03, 2010 10:27 pm

Re: array_chunk doesn't return arrays inside arrays

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: array_chunk doesn't return arrays inside arrays

Post 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?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
scuar
Forum Newbie
Posts: 9
Joined: Wed Nov 03, 2010 10:27 pm

Re: array_chunk doesn't return arrays inside arrays

Post by scuar »

Post Reply