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 add a field to an array in php

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge about "how to add a field to an array in php". In the actual case operation process, many people will encounter such a dilemma. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

Php to add a field to the array method: 1, define an array in the form of key-value pairs;2, directly through the "$array_test <$'new_test']= $new_test;" way to add a field.

Operating environment: Windows 7, PHP 7.4, DELL G3

How to add a field to an array in php?

PHP adds a field to an array to store an array

As I like lazy, find a PHP function is the most convenient, but I found that the original method is a lot, but there are also a lot of pits. Let's define an array and show you what we need to add.

//define an array of key pairs.$ array_test = array ('a ' => "aa",'b' => "bb",);//Define a key-value pair to be added, which is also placed in the array, that is, add a new field'new_test'=>$new_test$new_test = array ('c' => "cc",'d' => "dd",);

For example: array_push($array_test ,array ('new_test' => $new_test)), although it can also be spliced, but the newly added key-value pair you will find, why is the original data changed to'0'=>array(2); instead of adding a new field'new_test'=>$new_test.

This is the problem with array_push(), and according to the Internet:

Even if there are string keys in the array, the elements you add are always numeric keys.

If array_push() is used to add a cell to the array, it is better to use $array[] =, because there is no extra burden of calling the function.

Note: array_merge() is not allowed because it concatenates two strings, rather than adding another array with new fields to the specified array.

So what do we do?

Super simple, so simple that I want to take out my own brain to see if it is stupid to learn, water!

$array_test ['new_test']= $new_test; is super simple, wow, now look at what the array looks like after this step:

$array_test = array ('a ' => "aa",'b' => "bb",'new_test' => array ('c' => "cc",'d' => "dd",));"How to add a field to an array in php" is introduced here. Thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

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