Home > Backend Development > PHP Tutorial > PHP delete first and last element of array

PHP delete first and last element of array

WBOY
Release: 2016-07-29 09:01:29
Original
1013 people have browsed it

1. array_shift() function

 Deletes the first element in the array and returns the value of the element. Returns null if the array is empty.
Example:

<code><span><?php</span><span>$test_arr</span> = <span>array</span> (
    <span>"name"</span> => <span>"jeanphorn"</span>,
    <span>"skill"</span> => <span>"programming"</span>,
    <span>"hobby"</span> => <span>"swimming"</span>,
);

<span>echo</span> array_shift(<span>$test_arr</span>). <span>"\n"</span>;                                                                                                                                                           
print_r(<span>$test_arr</span>);

<span>?></span></span></code>
Copy after login

Result:

jeanphorn
Array (
[skill] => programming
[hobby] => swimming )


Note: If the key name is numeric, all elements will get new key names, starting from 0 and increasing by 1


2. array_pop() function


​Delete the last element in the array. Returns null if the array is empty.
Example:

<code><span><?php</span><span>$test_arr</span> = <span>array</span> (
    <span>"name"</span> => <span>"jeanphorn"</span>,
    <span>"skill"</span> => <span>"programming"</span>,
    <span>"hobby"</span> => <span>"swimming"</span>,
);

<span>echo</span> array_pop(<span>$test_arr</span>). <span>"\n"</span>;                                                                                                                                                           
print_r(<span>$test_arr</span>);

<span>?></span></span></code>
Copy after login
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces PHP to delete the first element and the last element of an array, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template