What is the difference between array () and [] in php?

Both get the union of arrays, but array_merge() overwrites duplicate non_numeric keys. Let us now see an example of the array+array−

Example

 Live Demo

<?php    $arr1 = array( "p"=>"150", "q"=>"100", "r"=>"120", "s"=>"110");    $arr2 = array("t"=>"115", "u"=>"103", "v"=>"105", "w"=>"125" );    var_dump ($arr1 + $arr2); ?>

Output

This will produce the following output−

array(8) {    ["p"]=>    string(3) "150"    ["q"]=>    string(3) "100"    ["r"]=>    string(3) "120"    ["s"]=>    string(3) "110"    ["t"]=>    string(3) "115"    ["u"]=>    string(3) "103"    ["v"]=>    string(3) "105"    ["w"]=>    string(3) "125" }

Example

Let us now see an example of array_merge() in PHP−

 Live Demo

<?php    $arr1 = array( "p"=>"150", "q"=>"100", "r"=>"120", "s"=>"110");    $arr2 = array("t"=>"115", "u"=>"110", "v"=>"105", "w"=>"100" );    var_dump (array_merge($arr1, $arr2)); ?>

Output

This will produce the following output−

array(8) {    ["p"]=>    string(3) "150"    ["q"]=>    string(3) "100"    ["r"]=>    string(3) "120"    ["s"]=>    string(3) "110"    ["t"]=>    string(3) "115"    ["u"]=>    string(3) "110"    ["v"]=>    string(3) "105"    ["w"]=>    string(3) "100" }

Updated on 27-Dec-2019 08:10:00

  • Related Questions & Answers
  • What is the difference between a python list and an array?
  • Difference Between Array and Structure
  • Difference Between Array and Pointer
  • What is the difference between a list and an array in C#?
  • Difference Between Array and Linked List
  • Difference Between Character Array and String
  • Difference between pointer and array in C
  • Difference between Array and Pointers in C.
  • Difference between Structure and Array in C
  • Difference between first and the second array in JavaScript
  • What is the difference between the size of ArrayList and length of Array in Java?
  • Merge and remove duplicates in JavaScript Array
  • Difference between String and Character array in Java.
  • Difference between List and Array types in Kotlin
  • Merge Sorted Array in Python

Top 5 Answer for PHP Difference between array() and []

98

Following [] is supported in PHP >= 5.4:

['name' => 'test', 'id' => 'theID']

This is a short syntax only and in PHP < 5.4 it won't work.

89

As of 2019, it has been 7 years since the [] syntax was added. That is long enough to drop array() except in old legacy programs, in my opinion.

71

If you are using 5.3 or previous version then you can't use [] as an array as well as associative array. If you are using 5.4 or later version of PHP then you can use either array() or [] to create an array, associative array or even multidimensional array.

65

51

Using php 7.2, for me it seems rather then [I am a an array] {I am an array seems to work}. Difference is between {} and []. My code

<p> <label for="post_category"> Cat 1 </label> <input type="checkbox" name="post_category{first}" value="cat1"> <br /> <label for="post_category{second}"> Cat 2 </label> <input type="checkbox" name="post_category" value="cat2"> </p>

Top 3 video Explaining PHP Difference between array() and []

What does array [] mean in PHP?

An array in PHP is actually an ordered map. A map is a type that associates values to keys.

What do square brackets mean in PHP?

In PHP, elements can be added to the end of an array by attaching square brackets ([]) at the end of the array's name, followed by typing the assignment operator (=), and then finally by typing the element to be added to the array.

What are the 3 types of PHP arrays?

Create an Array in PHP In PHP, there are three types of arrays: Indexed arrays - Arrays with a numeric index. Associative arrays - Arrays with named keys. Multidimensional arrays - Arrays containing one or more arrays.

What is array in PHP with example?

An array is a data structure that stores one or more similar type of values in a single value. For example if you want to store 100 numbers then instead of defining 100 variables its easy to define an array of 100 length.

Postingan terbaru

LIHAT SEMUA