Printing all elements in an array

In php it may sometimes be important to print all of the elements contained inside of an array. In most instances you need to print them out forwards, starting with the element stored in the first array key, but sometimes you may need to print out the contents backwards. This process can be tricky and hard to understand for a lot of people so I tried to simplify it by making a function that will print out all the elements for you. Here it is:

function printAllArray($name,$action){
if($action == “foreward” || $action == NULL || $action == “”){
$amountIn = count($name);
for($x = 0; $x <= $amountIn;$x++){
print($name[$x]);
}
}
if($action == “backward”){
$amountIn = count($name);
for($x = $amountIn; $x >= 0;$x–){
print($name[$x]);
}
}
}

Here is a basic overview of what is going on. A function called printAllArray is created with the parameters $name, which is the name of the array you want to print the contents of, and $actions which can either be ‘foreward’ or ‘backward’. Then I made two if statements which decide what to do if you want to print out the contents forwards or backwards. In both if statements a variable is created to see how many keys are stored in the array so it knows how much to print, where to start, and when to stop. Then a for loop is created. In the first if statement, the one that prints out the contents forwards, it creates a variable x which is initialized to zero, the loop keeps repeating as long as x is less than or equal to the amount of elements in the array so that it knows when to stop, then every time after it loops it adds one to x so that the for loop terminates eventually. Inside the loop it prints out the contents of the array key that the loop is currently on. The other for loop in the second if statement is almost exactly opposite. If you do not exactly understand this or the explanation is unclear it doesn’t matter because I will show you how to work this without knowing exactly what it does. If you want to know what it does anyways just comment with your questions.

Alright, now to show you how to use it. Well whenever you want to use this function copy it into the beginning of your php code. Then at wherever you want to print the code type the following:

printAllArray($arrayName,’foreward’);

You can replace $arrayName with whatever the name of your array is and you can replace ‘foreward’ with ‘backward’ if you wish.

I hope you all found this useful XD


The voice of the people

  1. Av-

    Uhm, does this not do the exact same things a the function print_r?

  2. Greg

    Pretty much, minus a the fact it can print in reverse too. Haha, to tell you the truth I never heard of print_r() before now. But other than that it is exactly the same.

  3. Mike Lockwood

    nethostspace.com

  4. bfsog

    tbh why? print_r does this, by reading it backwards you get it reversed.

  5. Marcus

    Hmm doesn’t

    do the same thing? only it doesnt do it backwards :)

    Its nice that you do examples though.
    Keep up the work!

    Marcus

  6. Marcus

    Hmm, you know that its possible to run php-code in your comment-section huh? :D

    the code i wrote in my past comment that should be before “do the same thing?” was:

    foreach($name as $printName)
    {
    print $printName;
    }

  7. Greg

    < ?php echo "hello" ?>

  8. I Would Like Some Info On Revitol Reviews - My Experience With Revitol Products

    discount revitol…

    I Would Like Some Info On Revitol Reviews…

  9. Wo kann ich filme downloaden?

    Mit Usenet Anbietern tonnenweise Filme downloaden lll…

    Wo kann ich filme downloaden?…

  10. How to pick up women

    how to pick up on women…

    how to pick up women the right way…

  11. Justin

    Try this bro.

    function printArray($array, $backwards){
    $tmp = $backwards ? array_reverse($array) : $array;
    foreach($tmp as $value){
    print $value;
    }
    }

    I admire your work man. I’m 19 and have my own company that I live modestly off. Your well on your way man. Keep it up.

Leave a Reply

Recently