In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-06-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "what is the result of connecting an array with a + sign in PHP". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
What is the result of concatenating arrays with a + sign in PHP?
In our development, we sometimes combine two numbers and connect them together. At this time, we should be careful not to be lazy and use the + sign directly. Why? Let's look at the following code:
$a = [1,2]
B = [4, 5, 6]
$c = $a + $b
Print_r ($c)
Please tell me directly what the result is with the first one. Maybe if I ask you this, you should be able to guess, the result is:
Array
(
[0] = > 1
[1] = > 2
[2] = > 6
)
As you can see, the array connected by the + operator turns out to be a union. That is, according to the key, the same key will not be overwritten, and no key will be added to form a new array. It's not really adding up the two arrays.
What if we use $baked rooma? The result is the content of $b.
$c = $b + $a
Print_r ($c)
Array
(
[0] = > 4
[1] = > 5
[2] = > 6
)
So what are we going to do if we get an array of 1, 2, 4, 5, 6? Yes, using the array_merge () function, notice the location of the array Key:
C = array_merge ($a, $b)
Print_r ($c)
Array
(
[0] = > 1
[1] = > 2
[2] = > 4
[3] = > 5
[4] = > 6
)
C = array_merge ($b, $a)
Print_r ($c)
Array
(
[0] = > 4
[1] = > 5
[2] = > 6
[3] = > 1
[4] = > 2
)
What if it's a Hash array in the form of key/value? The result is the same, no keys in $a will be merged, and the same keys will not be processed.
$a = ['a'= > 1,'b' = > 2]
B = ['a'= > 4,'b'= > 5,'c'= > 6]
Print_r ($astatb)
Array
(
[a] = > 1
[B] = > 2
[C] = > 6
)
C = array_merge ($a, $b)
Print_r ($c)
C = array_merge ($b, $a)
Print_r ($c)
Array
(
[a] = > 1
[B] = > 2
[C] = > 6
)
Array
(
[a] = > 4
[B] = > 5
[C] = > 6
)
Array
(
[a] = > 1
[B] = > 2
[C] = > 6
)
For the above Hash array, the result of using the array_merge () function is the same as using the + sign, because they still compare the keys. So the merged array will not add content, if it is undefined subscript, it will be added directly as a numeric subscript.
Finally, let's try again. Connection of operator:
C = $a. $b
Print_r ($c)
ArrayArray
Well, it doesn't make any sense for strings that are forcibly converted to string to be concatenated.
Test code: https://github.com/zhangyue0503/dev-blog/blob/master/php/201911/source/PHP%E4%B8%AD%E7%94%A8%2B%E5%8F%B7%E8%BF%9E%E6%8E%A5%E6%95%B0%E7%BB%84%E7%9A%84%E7%BB%93%E6%9E%9C%E6%98%AF%EF%BC%9F.php
Reference document: https://www.php.net/manual/zh/language.operators.array.php
This is the end of the content of "what is the result of joining an array with a + sign in PHP". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.