Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get several pieces of data in an array by php

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article Xiaobian for you to introduce in detail "php how to get several pieces of data in the array", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "php how to get several pieces of data in the array" can help you solve your doubts.

Two methods: 1, use array_slice () to intercept the specified number of data from the specified location, syntax "array_slice (array, start position, number)"; 2, use array_splice () to intercept all data from the specified position, syntax "array_slice (array, start position)".

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

The method of getting one or more pieces of data in an array by php

Use the array_slice () function

Use the array_splice () function

1. Use the array_slice () function

The array_slice () function is a function provided by PHP to intercept an array and extract a fragment from the array. Let's take a look at the array_slice () function:

Array_slice ($array,$start,$length,$preserve)

This function supports two required parameters: $array and $start, and two omitted arguments $length and $preserve.

The parameter $array does not need to be introduced, the parameter $start is used to specify the location to start the interception (subscript), and the parameter $length represents the intercepted length (if omitted, it will be intercepted from the specified subscript to the end of the array).

The output is as follows:

As you can see, the function of the array_slice () function is similar to that of the array_splice () function in intercepting an array, but the array_slice () function does not change the original array!

The parameter $start has three values:

Is 0

If it is a positive number, it is intercepted from the back of the destination.

Is a negative number, it is intercepted from back to front, starting at the distance from the end of $array-start (that is, positioning from the right side of the array to the left by absolute values). For example,-2 means starting with the penultimate element of the array.

The output is as follows:

There are also three values for the parameter $length:

If length is positive, it indicates the number of elements intercepted

If length is negative, the intercepted fragment will terminate at the position of length at the end of the distance array

If omitted, it starts at the start position and goes all the way to the end of the array.

The output is as follows:

The parameter $preserve indicates whether to keep the original key name, and the default value is false, that is, it is not retained; if set to true, the original key name is retained.

The output is as follows:

2. Use the array_splice () function

Yes, the array_splice () function! We know that the array_splice () function can add, delete, and replace array elements, so how can we intercept arrays?

That's because when the array_splice () function deletes some elements of an array, it combines the deleted elements into a new array and then returns the new array; so the array_splice () function can be used to intercept array fragments.

Let's look at the following small example:

The output is as follows:

OK! Using the array_splice ($array,$start,$length) function, we can intercept array fragments of a specified length (the value of $length) based on the array subscript (the value of $start). Remember that the array_splice () function changes the original array!

After reading this, the article "how to get several pieces of data in an array by php" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report