Home > Backend Development > PHP Tutorial > Summary of methods for adding associated elements to PHP array_PHP tutorial

Summary of methods for adding associated elements to PHP array_PHP tutorial

WBOY
Release: 2016-07-13 10:43:55
Original
1156 people have browsed it

What we are introducing here is adding an associative array to an array. This synthesizes a multi-dimensional array. Let me give you a few examples. I hope it will be helpful to all classmates.

In the article "Summary of methods for adding elements to php arrays", we introduced how to add elements to an array. So what should I do if I want to add an element like $array=array('title'=>'php tutorial').

array_push, array_pop, array_shift, array_unshift These functions are all designed for numeric type index arrays.
To add an associative array, you can use the array_merge method or the + operator

Let’s first look at adding array elements to an array

1. How to add array elements in php:

(1) Add array elements through assignment: $states[‘name’]=’Tom’;

(2)int array_push(array target_array,mixed variable [,mixed variable...]) The function adds the variable to the end of the target_array and returns true when successful, otherwise it returns false, where the variable can be multiple.

(3)int array_unshift(array target_array,mixed variable [,mixed variable...]) The function adds variable to the array head of target_array and returns true when successful, otherwise returns false, where the variable can be multiple. All existing numeric keys will be modified accordingly, while associated keys are unaffected.

(4)array array_pad(array target_array,integer length,mixed pad_value) Increase the size of target_array to the length specified by length.

Is it okay to use array_push or array_unshift?

The answer is no


Specific method:

1. Use the array_merge method to implement a function similar to array_unshift that adds elements at the beginning

[front] => hello [0] => a
The code is as follows
 代码如下 复制代码

$queue = array('a', 'B');
$queue = array_merge(array('front' => 'hello'), $queue);
/*
Array
(
    [front] => hello
    [0] => a
    [1] => b
)
*/
?>

Copy code

 代码如下 复制代码

$queue = array('a', 'B');
$queue = array('front' => 'Hello') + $queue;
?>

$queue = array('a', 'B');

$queue = array_merge(array('front' => 'hello'), $queue);

/*

Array
 代码如下 复制代码

$queue = array('a', 'B');
$queue['front'] = 'hello';
/*
输出
Array
(
[0] => a
    [1] => b
    [front] => hello 
)
*/
?>

(
[1] => b )

*/

?> 2.+operator
The code is as follows Copy code
$queue = array('a', 'B'); $queue = array('front' => 'Hello') + $queue; ?>
The output result is the same as using the array_merge method. 3. Add an associative array element at the end of the element
The code is as follows Copy code
$queue = array('a', 'B');<🎜> $queue['front'] = 'hello';<🎜> /*<🎜> Output <🎜> Array<🎜> (<🎜> [0] => a [1] => b [front] => hello ) */ ?> http://www.bkjia.com/PHPjc/633139.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633139.htmlTechArticleWhat we introduce here is to add an associative array to the array. This synthesizes a multi-dimensional array. Let me do it next Let me give you a few examples, I hope it will be helpful to all students. In...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template