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 find several strings in an array by php

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces php how to find several strings in the array of related knowledge, the content is detailed and easy to understand, the operation is simple and fast, with a certain reference value, I believe that after reading this php how to find several strings in the array article will have a harvest, let's take a look.

Methods: 1, define a counter variable and assign a value of 0 arr as 2, use "foreach ($val $val)" to traverse the array; 3, in the loop, use "counter ($val)" to determine whether the array element is a string, and if so, the counter variable value plus 1: 3, after the end of the loop, the output counter variable can be.

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

In php, find several strings in the array, that is, determine how many elements in the array are of string type.

Realize the idea:

Traversing the array with foreach loop statement

In the loop, use the is_string () function to determine whether the array elements are strings one by one

If so, a counter is required to count (the counter is defined before the loop statement and the initial value is 0)

Implementation code:

It is calculated that there are three strings in the above example array, and we use var_dump () to output the array details to see if it is correct:

Var_dump ($arr)

Ok, it's right!

Description:

1. Foreach statement

Foreach is a statement specially designed to traverse an array. It is a common method when traversing an array and provides great convenience in traversing an array. After PHP5, you can also traverse objects (foreach can only be applied to arrays and objects).

The foreach statement traverses an array independent of the array subscript and can be used for discontiguous indexed arrays and associative arrays with strings as subscript.

Let's first take a look at the foreach statement, which has two grammatical formats:

Syntax format 1:

Foreach ($array as $value) {statement block;}

Iterate through the given $array array, assigning the value of the current array to $value in each loop.

Syntax format 2:

Foreach ($array as $key = > $value) {statement block;}

Iterate through the given $array array, assigning the value of the current array to $value and the key name to $key in each loop.

Each time the foreach statement loops, the pointer inside the array moves forward so that the next array element is obtained in the next loop until it traverses to the end of the array, stops traversing and exits the loop.

2. Is_string () function

The is_string () function is used to detect whether a variable is a string.

Returns TRUE if the specified variable is a string, FALSE otherwise.

This is the end of the article on "how to find a few strings in an array by php". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to find a few strings in the array by php". If you want to learn more, you are welcome to 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