How to add and delete array elements in php

Source: Internet
Author: User
How to add and delete array elements in php

  1. $ Fruits = array ("apple", "banana ");
  2. Array_unshift ($ fruits, "orange", "pear ")
  3. // $ Fruits = array ("orange", "pear", "apple", "banana ");

Add elements at the end of the array

The return value of the array_push () function is int type, which is the number of elements in the array after data is pressed. for this function, multiple variables can be passed as parameters and multiple variables can be pushed to the array at the same time. The format is: (array, mixed variable [, mixed variable...])

The following example adds two more fruits to the $ fruits array:

  1. $ Fruits = array ("apple", "banana ");
  2. Array_push ($ fruits, "orange", "pear ")
  3. // $ Fruits = array ("apple", "banana", "orange", "pear ")

Delete value from array header

The array_shift () function deletes and returns the elements found in the array. The result is that if a numeric value is used, all corresponding values are moved down, and the array using the correlated key is not affected. The format is mixed array_shift (array)

The following example deletes the first element apple in the $ fruits array:

  1. $ Fruits = array ("apple", "banana", "orange", "pear ");
  2. $ Fruit = array_shift ($ fruits );
  3. // $ Fruits = array ("banana", "orange", "pear ")
  4. // $ Fruit = "apple ";

Deletes an element from the end of an array.

The array_pop () function deletes the array and returns the last element of the array. The format is mixed array_pop (aray target_array );

The following example deletes the last state from the $ states array:

  1. $ Fruits = array ("apple", "banana", "orange", "pear ");
  2. $ Fruit = array_pop ($ fruits );
  3. // $ Fruits = array ("apple", "banana", "orange ");
  4. // $ Fruit = "pear ";

Note: PHP provides some functions for extending and downgrading arrays. For programmers who want to simulate various queue implementations (FIFO and LIFO), these functions can be convenient. As the name suggests, the function names (push, pop, shift, and unshift) of these functions clearly reflect their functions.

A traditional queue is a data structure. Deleting elements in the same order as adding elements is called FIFO or FIFO. On the contrary, the stack is another data structure, in which the order in which elements are deleted is the opposite of the order when they are added, which is either backward-forward, FIFO, or LIFO.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.