PHP Tutorial #09 Array Methods (Array_Walk, Current, Next, Prev, End and Reset )

Image Source

Repository

https://github.com/php/php-src  

What Will I Learn?

I will learn the array methods (Array_Walk, Current, Next, Prev, End and Reset ) with examples.

  • What's the array_walk method and how to use it.
  • How to get the current , next and previous element using " current, next , prev methods ".
  • The concept of " end and reset " methods and their uses.

Requirements

  • Server support PHP , Xampp or Wamp for example
  • An IDE like Sublime text.
  • Browser (Chrome for example) 

Difficulty

  • Basic

Description 

1 – Array_Walk  

The array_walk method applies the callback function defined by the user to each element of the array. This method is not affected by the internal pointer of the array. it will be applied to the entire array regardless of the pointer location. 

It has 2 parameters the first is " the array ",  the second is " the callback function " defined by the user typically takes two parameters. The first is the array parameter, the second is the key or index.  And the third parameter for the callback function is the userdata If the optional userdata parameter is available, it will be passed as a third parameter to it. 

The array_walk  returns the TRUE value when it succeeds or FALSE when it fails to execute.  

To use the array_walk we need to pass the array and the function as parameters.

array_walk($array, function);

I have an array of numbers and a function with one parameter, the function will print the result of the number passed as parameter multiplied by 2

$review = array(20,    30,    10,    14,    21,    34 );      

function multiplication($num){    

        echo "The number" . $num . " Multiplied by 2 = " . $num * 2 . "<br>";   

}       

array_walk($review, "multiplication"); 

The array review has 6 numbers , using the array_walk the method will pass each value to the multiplication function as parameter and it will return the value * 2 .

2 – Current : 

Each array contains an internal pointer that points to the "current" element, which initially refers to the first element in the array.

It has " the array " as parameter. The current method simply returns the value of the array element to which the internal pointer is currently pointing. The cursor function does not move in any way. If the internal cursor points beyond the end of the list of elements, or if the array is empty, it returns the FALSE value. 

This method may return the logical value FALSE, but may also return an illogical value that evaluates with the same FALSE value.  

To use the current method we pass the array as parameter

current($array);

I have an array contains 6 persons , I will use the current method to get the current element

 $team = array( "person1",    "person2",    "person3",    "person4",    "person5",    "person6");      

 echo current($team) . "<br>"; 

Firstly the current method will return the first value in the array because the pointer points on the first value

3- Next :  

The next method behaves in the same way as the current method with one difference. The internal array index advances one place forward before returning the value of the element. This means that it returns the value of the next element of the array and moves the internal array pointer by one place forward. 

It returns the value of the array element at the next location indicated by the internal array pointer, or FALSE if there are no other elements.  

To use the next method we need to pass the array as parameter

next($array);

I will continue the last example I will use the next method to get the next element

 echo next($team) . "<br>"; 

The first element is the person1 when we applie the next method we will get the person2

4 – Prev :  

This method returns the internal array pointer back. It behaves in the same way as the next () method, but it delays the internal array pointer one place back instead of advancing it. 

The method returns the value of the array element at the previous position indicated by the internal array pointer, or FALSE if there are no other elements. It has just the array as parameter.

To use the prev method we need to pass the array as parameter.

prev($array);

I will continue the last example and I want to get the previous element 

echo prev($team) . " <br> ";

To get the next element we use the " next " method and to get the previous element we use the " prev " method

 

5 – End :  

The end method moves the internal pointer of the array to the last element in it and returns its value.It has " the array " as parameter. 

This array passes the reference because the function modifies it. Which requires you to pass a real variable, not a function that returns an array, because only the actual variables can be passed by reference. This method returns the value of the last item, or the FALSE value if the array is empty.  

To use the end method we need to pass the array as parameter

end($array);

I will continue the last example I will use the end method to get the last item in the array

end($team) . " <br> ";

The method will return the last element in the array which is the person6

 

6 – Reset :  

The reset method moves the internal pointer of the array to its beginning. In which the value of the first element is restored. 

It has " the array " as parameter and it returns the value of the first array element, or FALSE if the array is empty. 

This method may return the logical value FALSE, but may also return an illogical value that evaluates with the same FALSE value.   

To use the reset method we need to pass the array as parameter

reset($array);

I will continue the last example , I want to get the first element using the reset method

echo reset($team); 

The reset method will change the pointer to point to the first item in the array and this is the result

Video Tutorial

Curriculum

Proof of Work Done

https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/current.php

Sort:  

Hello @alexendre-maxim

Thank you for your contribution.

I see improvement in this video tutorial and that you had made note of the areas to improve in the presentation in this video tutorial. Good work.

Your overall presentation has gone up and I am very pleased to see that.

The graphic lines and arrows can be better.

I can see that you are being more deliberate with your words and sentences as you give your instructions in the tutorial. You can work on the diction and enunciation of the words. This takes practice. For those whose English is their second language, we tend to speak very fast. Partly it could be because we are nervous. So what you can do is to take a deep breath, count 1, 2, 3 and then start speaking with confidence.

Keep up the good work!

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Hey @rosatravels
Here's a tip for your valuable feedback! @Utopian-io loves and incentivises informative comments.

Contributing on Utopian
Learn how to contribute on our website.

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Thank you @rosatravels , I will try harder to make something better.

Hi @alexendre-maxim!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @alexendre-maxim!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.29
TRX 0.11
JST 0.033
BTC 63458.69
ETH 3084.37
USDT 1.00
SBD 3.99