$rule); */ I"m looking for something like this so that: print_r($GET); /* output: $GET 1, two => 2, ," /> $rule); */ I"m looking for something like this so that: print_r($GET); /* output: $GET 1, two => 2, ," />

Add Key/Value To Array Php

Take a look at this code:

$GET = array(); $key = "one=1"; $rule = explode("=", $key); /* array_push($GET, $rule<0> => $rule<1>); */

I"m looking for something like this so that:

print_r($GET); /* output: $GET 1, two => 2, ...> */

Is there a function lớn vị this? (because array_push won"t work this way)


Nope, there is no array_push() equivalent for associative sầu arrays because there is no way determine the next key.

You"ll have sầu lớn use

$arrayname = $value;
How khổng lồ add multiple keys và values to an array? for example I have = $value1 và = $value2, và I want khổng lồ add them to lớn $arrayname – King Goeks Oct 30 "13 at 7:59
KingGoeks $arrayname = array('indexname1' => $value1, 'indexname2' => $value2); would phối them as the only items in $arrayname. If you already have sầu $arrayname phối và want lớn keep its values, try $arrayname += $anotherarray. Keep in mind any existing keys in the first array would be overwritten by the second. – Charlie Schliesser Dec 17 "13 at 23:09
"Keep in mind any existing keys in the first array would be overwritten by the second" that is not true, the first array has priority. if you vì $a = array("name" => "John"); $a += array("name" => "Tom");then $a<"name"> will be "John" – santiago arizti Jan 31 "18 at 16:16
it's the easiest way. – NomanJaved Nov 15 "19 at 16:33
it is the faster of the two: array_push vs array<>. Array<> is about 2x as fast if i remember correctly... – jasonflaherty Nov 20 "19 at 18:18

Pushing a value into an array automatically creates a numeric key for it.

Bạn đang xem: Add key/value to array php

When adding a key-value pair lớn an array, you already have sầu the key, you don"t need one khổng lồ be created for you. Pushing a key into an array doesn"t make sense. You can only set the value of the specific key in the array.

// no key array_push($array, $value); // same as: $array<> = $value; // key already known $array<$key> = $value;

You can use the union operator (+) to lớn combine arrays and keep the keys of the added array. For example:

"bar"); $arr2 = array("baz" => "bof"); $arr3 = $arr1 + $arr2; print_r($arr3); // prints: // array( // "foo" => "bar", // "baz" => "bof", // );

So you could bởi vì $_GET += array("one" => 1);.

There"s more info on the usage of the union operator vs array_merge in the documentation at http://php.net/manual/en/function.array-merge.php.


The basic difference between array_merge() & + operator is when the 2 arrays contain values on the same key + operator ignores the value from second array (does not override), also it does not renumber/reindex the numeric keys... – jave.website Feb 16 "17 at 21:35
Thanks you, I have tried many arrays function but your answer helped me lớn achieve sầu what I want, – Danish Aquảng cáo 25 "trăng tròn at 10:03

I would lượt thích to add my answer khổng lồ the table và here it is :

//connect to db ...etc $result_sản phẩm = /*your mysql query here*/ $array_sản phẩm = array(); $i = 0; foreach ($result_hàng hóa as $row_product) //you can encode the array khổng lồ json if you want lớn send it to an ajax hotline $json_product = json_encode($array_product); echo($json_product);

hope that this will help somebody


I looked through dozens of solutions and this is the only one that fit my use case. Thank you! – Ryan Burney Feb 13 "17 at 17:16

Exactly what Pekka said...

Xem thêm: Top 5 Tướng Nào Đi Rừng Mạnh Nhất Liên Quân Moba, Ba Vị Thánh Rừng Ở Thời Điểm Hiện Tại

Alternatively, you can probably use array_merge lượt thích this if you wanted:

array_merge($_GET, array($rule<0> => $rule<1>));

But I"d prefer Pekka"s method probably as it is much simpler.


I wonder why the simplest method hasn"t been posted yet:

$arr = <"company" => "Apple", "product" => "iPhone">; $arr += <"version" => 8>;
it is not exactly the same, in array_merge, the array on the right wins on key conflict, in " += " the array on the left wins – santiago arizti Jan 31 "18 at 16:18
santiagoarizti What vị you mean by "wins"? – AlexioVay Aug 27 "19 at 9:13
if two arrays have sầu both the same key, array_merge & array union (+=) behave in the opposite way i.e. array_merge will respect the value from the second array and array union will respect the value from the first array. – santiago arizti Aug 28 "19 at 15:49
perfect solution for me. THX for sharing Alexio! :) – ThomasB Mar 5 "trăng tròn at 18:41

This is the solution that may useful for u

Class Form } $size = new Form(); $form->addTextField("myclass1","myid1"); $form->addTextField("myclass2","myid2"); $form->addTextField("myclass3","myid3"); When you dump it. The result like this array (size=3) "type" => array (size=3) 0 => string "text" (length=4) 1 => string "text" (length=4) 2 => string "text" (length=4) "class" => array (size=3) 0 => string "myclass1" (length=8) 1 => string "myclass2" (length=8) 2 => string "myclass3" (length=8) "id" => array (size=3) 0 => string "myid1" (length=5) 1 => string "myid2" (length=5) 2 => string "myid3" (length=5)
Share
Improve this answer
Follow
answered Jun 6 "15 at 15:49
Faris RayhanFaris Rayhan
3,051 1 1 gold badge 18 18 silver badges 14 14 bronze badges
Add a phản hồi  | 
7

I was just looking for the same thing & I realized that, once again, my thinking is different because I am old school. I go all the way bachồng lớn BASIC & PERL và sometimes I forget how easy things really are in PHPhường.

I just made this function lớn take all settings from the database where their are 3 columns. setkey, công trình (key) & value (value) & place them inlớn an array called settings using the same key/value without using push just lượt thích above.

Pretty easy & simple really

// Get All Settings $settings=getGlobalSettings(); // Apply User Theme Choice $theme_choice = $settings<"theme">; .. etc etc etc .... function getGlobalSettings() mysqli_close($dbc); return $settings; }

So like the other posts explain... In php there is no need to "PUSH" an array when you are using

Key => Value

AND... There is no need lớn define the array first either.

$array=array();

Don"t need lớn define or push. Just assign $array<$key> = $value; It is automatically a push & a declaration at the same time.

I must add that for security reasons, (P)oor (H)elpless (P)rotection, I means Programming for Dummies, I mean PHPhường.... hehehe I suggest that you only use this concept for what I intended. Any other method could be a security risk. There, made my disclaimer!