Home  >  Article  >  Backend Development  >  How to remove the key value (value) of a one-dimensional array in php

How to remove the key value (value) of a one-dimensional array in php

青灯夜游
青灯夜游Original
2021-11-26 18:49:272577browse

In PHP, you can use the array_keys() function to remove the key value (value) of a one-dimensional array, leaving only the key name (key) of the array. The syntax format is "array_keys($array)".

How to remove the key value (value) of a one-dimensional array in php

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

In PHP, each element of the array It is composed of key-value pairs, and the value of the corresponding key is accessed through the key name of the element.

提示:“索引”和“键名”指的是同一样东西,“索引”多指数组数字形式的下标。

Sometimes, we need to remove the key value (value) of a one-dimensional array and leave only the key name (key) of the array. How to do this?

At this point, you can use the array_keys() function.

array_key() function can obtain some or all key names in the array. The syntax format is as follows:

array_keys($array [, $search_value = null [, $strict = false]])

The parameter description is as follows:

  • $array: required Parameter is the array to be operated;
  • $search_value: optional parameter. If the parameter is empty, the function will return all key names in the array. If this parameter is specified, the function will only return the value. is the key name of $search_value;
  • $strict: optional parameter to determine whether to use strict mode when searching. $strict defaults to false, which is non-strict mode. Only types are compared when searching, not Compare types. If $strict is set to true, it is strict mode. The value and type are compared at the same time during search, which is equivalent to ===.

array_key() function will return the obtained array key name in the form of an array.

<?php
header("Content-type:text/html;charset=utf-8");
$array = array(
        &#39;name&#39;  => &#39;PHP中文网&#39;,
        &#39;url&#39;   => &#39;https://www.php.cn/&#39;,
        &#39;title&#39; => &#39;PHP教程&#39;,
);
$key=array_keys($array);
var_dump($key);
?>

How to remove the key value (value) of a one-dimensional array in php

As you can see, the key value (value) of the one-dimensional array is removed, and only the key name (key) of the one-dimensional array is returned.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to remove the key value (value) of a one-dimensional array in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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