print r alternative

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
sampaloc
Forum Newbie
Posts: 3
Joined: Fri Apr 25, 2008 6:22 am

print r alternative

Post by sampaloc »

hi! heres the code to filter odd and even numbers. the problem is, "Array ( [1]...." appears... i think because of print_r.. is there any way to display the odd and even numbers without "Array ( [1]...." I want to display pure odd and even numbers.. thank you.

function odd($var)
{
return($var & 1);
}

function even($var)
{
return(!($var & 1));
}

$array1 = array($a,$b,$c,$d,$e,$f,$g,$h,$i,$j);
$array2 = array($a,$b,$c,$d,$e,$f,$g,$h,$i,$j);

echo "<BR>Odd :\n";
print_r(array_filter($array1, "odd"));
echo "<BR>Even:\n";
print_r(array_filter($array2, "even"));
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: print r alternative

Post by John Cartwright »

use a foreach() -- or other type of loop -- to output each array element.

You could also simply do the follow if all you need a delimited list.

Code: Select all

echo implode(',' $youarray);
Post Reply