Jump to content

PhCyber Tunnel Available!

Get it on Playstore
Download Now!

Ad Placement For Rent

Ad Space For Rent. Get Your Product up here and let our users see them first when they visit our Forum. We have thousands of page views everyday!
More Info
Info
  • Welcome to PhCyber
  • Explore and Enjoy Browsing!
  • Lots of useful topics
  • Stay active and receive prizes
Sign in to follow this  
Cyber

Php Arrays

Recommended Posts

Simple arrays
Arrays are a special type of variable that can contain many variables, and hold them in a list.

For example, let's say we want to create a list of all the odd numbers between 1 and 10. Once we create the list, we can assign new variables that will refer to a variable in the array, using the index of the variable.

To use the first variable in the list (in this case the number 1), we will need to give the first index, which is 0, since PHP uses zero based indices, like almost all programming languages today.

For Members Only. Register here https://phcyber.com/register


We can now add new variables using an index. To add an item to the end of the list, we can assign the array with index 5 (the 6th variable):

For Members Only. Register here https://phcyber.com/register


Arrays can contain different types of variables according to your needs, and can even contain other arrays or objects as members.

To delete an item from an array, use the unset function on the member itself. For example:

For Members Only. Register here https://phcyber.com/register


Useful functions
The count function returns the number of members an array has.

For Members Only. Register here https://phcyber.com/register


We can also use the index syntax to get the first member of the array, as follows:

For Members Only. Register here https://phcyber.com/register


The end function gets the last member of the array.

For Members Only. Register here https://phcyber.com/register


We can also use the count function to get the number of elements in the list, and then use it to refer to the last variable in the array. Note that we subtract one from the last index because indices are zero based in PHP, so we need to fix the fact that we don't count variable number zero.

For Members Only. Register here https://phcyber.com/register


Stack and queue functions
Arrays can be used as stacks and queues as well.

To push a member to the end of an array, use the array_push function:

For Members Only. Register here https://phcyber.com/register


To pop a member from the end of an array, use the array_pop function:

For Members Only. Register here https://phcyber.com/register


To push a member to the beginning of an array, use the array_unshift function:

For Members Only. Register here https://phcyber.com/register


To pop a member from the beginning of an array, use the array_shift function:

For Members Only. Register here https://phcyber.com/register


Concatenating arrays
We can use the array_merge to concatenate between two arrays:

For Members Only. Register here https://phcyber.com/register


Sorting arrays
We can use the sort function to sort arrays. The rsort function sorts arrays in reverse. Notice that sorting is done on the input array and does not return a new array.

For Members Only. Register here https://phcyber.com/register


Advanced array functions
The array_slice function returns a new array that contains a certain part of a specific array from an offset. For example, if we want to discard the first 3 elements of an array, we can do the following:

For Members Only. Register here https://phcyber.com/register


We can also decide to take a slice of a specific length. For example, if we want to take only two items, we can add another argument to the function:

For Members Only. Register here https://phcyber.com/register


The array_splice function does exactly the same, however it will also remove the slice returned from the original array (in this case, the numbers variable).

For Members Only. Register here https://phcyber.com/register

 


Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...