Home Backend Development PHP Problem What are the common methods in php arrays

What are the common methods in php arrays

Nov 09, 2020 pm 02:26 PM
php array method

Commonly used methods in php arrays are: 1. is_array; 2. in_array; 3. array_key_exists; 4. array_search; 5. array_keys.

What are the common methods in php arrays

Commonly used array methods:

(Learning video recommendation: java video tutorial)

1 , is_array — Check whether a variable is an array

Syntax:

bool is_array ( mixed $var ) //如果 var 是 array,则返回 TRUE,否则返回 FALSE。
Copy after login

Usage:

	    $arr = [];
            $arr1 = 99;
            var_dump(is_array($arr));    //输出 bool(true)
            var_dump(is_array($arr1));    //输出 bool(false)
        类似的方法:
            1)、is_int — 检测变量是否是整数
                bool is_int ( mixed $var )        //如果 var 是 integer 则返回 TRUE,否则返回 FALSE。
                is_integer — is_int() 的别名
            2)、is_numeric — 检测变量是否为数字或数字字符串
                bool is_numeric ( mixed $var )    //如果 var 是数字和数字字符串则返回 TRUE,否则返回 FALSE。
                Note:若想测试一个变量是否是数字或数字字符串(如表单输入,它们通常为字符串),必须使用 is_numeric()。
            3)、is_bool — 检测变量是否是布尔型
                bool is_bool ( mixed $var )        //如果 var 是 boolean 则返回 TRUE。
            4)、is_float — 检测变量是否是浮点型
                bool is_float ( mixed $var )    //如果 var 是 float 则返回 TRUE,否则返回 FALSE。
                is_real — is_float() 的别名            
            5)、is_string — 检测变量是否是字符串
                bool is_string ( mixed $var )    //如果 var 是 string 则返回 TRUE,否则返回 FALSE。
            6)、is_object — 检测变量是否是一个对象
                bool is_object ( mixed $var )    //如果 var 是一个 object 则返回 TRUE,否则返回 FALSE。
Copy after login

2, in_array — Check whether a value exists in the array

Syntax:

bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] ) //大海捞针,在大海(haystack)中搜索针( needle),如果没有设置 strict 则使用宽松的比较。
Copy after login

Parameters:

needle The value to be searched. (If needle is a string, the comparison is case-sensitive.)

haystack The array to search.

strict If the value of the third parameter strict is TRUE, the in_array() function will also check whether the type of needle is the same as that in haystack.

Return value:

If needle is found, TRUE is returned, otherwise FALSE is returned.

Example:

    //区分大小写
    $fruits = [ "Apple", "Pear", "Bana", "Orange" ];
    if (in_array("Apple", $fruits)) {
        echo "Apple ";
    }
    if (in_array("apple", $fruits)) {
        echo "apple ";
    }
    //开启严格检查
    $number = [ 13, 14, 15, 16 ];
    if (in_array("13", $number, true)) {
        echo "string 13";
    }
    if (in_array(13, $number, true)) {
        echo "int 13";
    }
返回:Apple int 13
Copy after login

3. array_key_exists - Check whether there is a specified key name or index in the array

Syntax:

bool array_key_exists ( mixed $key , array $array ) // 数组里有键 key 时,array_key_exists() 返回 TRUE。 key 可以是任何能作为数组索引的值。
Copy after login

Parameter description:
key The key to be checked

array An array containing the key to be checked

Return value: TRUE on success, or FALSE on failure.

Example:

   $array = [ 1,2,3,4 ];
   var_dump(array_key_exists(0, $array));    //输出 bool(true)
Copy after login

4, array_search - Search for a given value in the array, if successful, return the first corresponding key name,

Syntax:

mixed array_search ( mixed $needle , array $haystack [, bool $strict = false ] ) //大海捞针,在大海(haystack)中搜索针( needle 参数)。
Copy after login

Parameter description:
needle search value. (If needle is a string, the comparison is case-sensitive. )

haystack this array.

strict If the optional third argument strict is TRUE, array_search() will check for identical elements in the haystack.

This means that the type of needle in haystack is also strictly compared, and the objects must be the same instance.

Return value:

If needle is found, return its key, otherwise return FALSE.

If needle appears more than once in haystack, return the first matching key. To return all keys that match a value, array_keys() with the optional argument search_value should be used instead.

Example:

 $array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
   $key = array_search('green', $array); // $key = 2;
   $key = array_search('red', $array);   // $key = 1;
Copy after login

5, array_keys - Return some or all key names in the array

Syntax:

array array_keys ( array $array [, mixed $search_value = null [, bool $strict = false ]] )
Copy after login

If optional parameters are specified search_value, only the key name of the value is returned. Otherwise all keys in the input array will be returned.

Parameter description:

input An array containing the keys to be returned.

search_value If this parameter is specified, only keys containing these values ​​will be returned.

strict Determines whether strict comparison (===) should be used when searching.

Return value: Return all keys in input.

Example:

        $array = array(0 => 100, "color" => "red");
       print_r(array_keys($array));

       $array = array("blue", "red", "green", "blue", "blue");
       print_r(array_keys($array, "blue"));

       $array = array("color" => array("blue", "red", "green"),
                      "size"  => array("small", "medium", "large"));
       print_r(array_keys($array));
   返回:
       Array
       (
           [0] => 0
           [1] => color
       )
       Array
       (
           [0] => 0
           [1] => 3
           [2] => 4
       )
       Array
       (
           [0] => color
           [1] => size
       )
Copy after login

Related recommendations: php training

The above is the detailed content of What are the common methods in php arrays. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles