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 realize the forced type conversion of array Key in PHP

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to realize the forced type conversion of array Key in PHP. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

First, let's take a look at the following code:

$arr = [

"1" = > "a"

"01" = > "b"

1 = > "aa"

1.1 = > "aaa"

"0.1" = >" bb "

]

Var_dump ($arr)

/ / array (3) {

/ / [1] = >

/ / string (3) "aaa"

/ / '01' = >

/ / string (1) "b"

/ / '0.1' = >

/ / string (2) "bb"

/ /}

Huh? The subscript values of "1" and 1 defined by us have all become "aaa" of 1.1?

Yes, array key values in PHP accept only numeric and string types, and when Key is a string, it is cast to numeric types, following the rules of type casting. The same is true of floating-point numbers, which are directly converted to downward rounded integers.

So why are "0.1" and "01" still there? First of all, "01" is not a standard decimal value and cannot be converted to an integer, so "01" is still a string subscript, what about "0.1"? It is certainly not a standard decimal value. This violates the mandatory type conversion principle of string conversion digits. In the casting of variables, both strings will be converted to 0, but not in the array. This will be a pit and a place to pay attention to.

The conversion instructions for Key values given in the official PHP documentation are as follows:

Strings that contain legal integer values are converted to integers. For example, the key name "8" will actually be stored as 8. However, "08" is not cast because it is not a legal decimal value.

Floating-point numbers are also converted to integers, meaning that their fractional parts are dropped. For example, the key name 8.7 will actually be stored as 8.

Boolean values are also converted to integers. That is, the key name true will actually be stored as 1 and the key name false will be stored as 0.

The Null is converted to an empty string, that is, the key name null is actually stored as "".

Arrays and objects cannot be used as key names. Insisting on doing so will lead to a warning: Illegal offset type.

Next, there is an interview question that the author has done, which has a lot to do with this type conversion. The code is as follows:

$a = ['a']

$a [2] ='b'

$a [] ='c'

$a ['1'] ='d'

/ / what is the output of the following loop?

Foreach ($an as $v) {

Echo $v,','

}

/ / what is the output of the following loop?

For ($I = 0; $I < count ($a); + + $I) {

Echo $a [$I],','

}

This article participates in the "OSC Source Program". You are welcome to join us and share it.

On how to achieve the mandatory type conversion of array Key in PHP is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Internet Technology

Wechat

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

12
Report