2010-01-23 28 views
244

看看這個代碼:如何推動價值和鑰匙插入陣列

$GET = array();  
$key = 'one=1'; 
$rule = explode('=', $key); 
/* array_push($GET, $rule[0] => $rule[1]); */ 

我正在尋找這樣的事情,這樣:

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

是否有一個函數來完成這個? (因爲array_push不會以這種方式工作)

回答

538

不,沒有array_push()等效於關聯數組,因爲沒有辦法確定下一個鍵。

你將不得不使用

$arrayname[indexname] = $value; 
+6

我不明白。這不是將項目添加到數組中的正常方式嗎? – rgin 2013-05-18 18:38:06

+23

@rgin:是的。但是有時候你會在注意別的東西時錯過顯而易見的解決方案(比如'array_push')。 :) – Jonik 2013-08-02 12:16:21

+0

如何將多個鍵和值添加到數組?例如我有[indexname1] = $ value1和[indexname2] = $ value2,我想將它們添加到$ arrayname – 2013-10-30 07:59:37

15

究竟是什麼佩卡說...

或者,你也許可以使用array_merge這樣,如果你想:

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

但我更喜歡佩卡的方法,因爲它更簡單。

5

我只是在尋找同樣的東西,我意識到,再次,我的想法是不同的,因爲我是一所老校。我一路回到BASIC和PERL,有時我忘記了PHP中真正的事情。

我只是讓這個函數從數據庫中取出所有設置,它們是3列。 setkey,item(key)& value(value),並使用相同的鍵/值將它們放入一個名爲settings的數組中,而不用像上面那樣使用push。

很容易&其實很簡單

 

// Get All Settings 
$settings=getGlobalSettings(); 


// Apply User Theme Choice 
$theme_choice = $settings['theme']; 

.. etc etc etc .... 




function getGlobalSettings(){ 

    $dbc = mysqli_connect(wds_db_host, wds_db_user, wds_db_pass) or die("MySQL Error: " . mysqli_error()); 
    mysqli_select_db($dbc, wds_db_name) or die("MySQL Error: " . mysqli_error()); 
    $MySQL = "SELECT * FROM systemSettings"; 
    $result = mysqli_query($dbc, $MySQL); 
    while($row = mysqli_fetch_array($result)) 
     { 
     $settings[$row['item']] = $row['value']; // NO NEED FOR PUSH 
     } 
    mysqli_close($dbc); 
return $settings; 
} 


所以像其他職位說明...在PHP中,沒有必要「PUSH」,當您使用

關鍵=>值

數組

AND ...沒有必要首先定義數組。

$ array = array();

不需要定義或推送。只需分配$ array [$ key] = $ value;它同時自動推送和聲明。 (P)oor(H)elpless(P)rotection,我的意思是編程傻瓜,我的意思是PHP .... hehehe我建議你只使用這個概念,爲什麼我意。任何其他方法都可能是安全風險。在那裏,做了我的免責聲明!

56

值推入數組會自動爲其創建一個數字鍵。

向數組中添加鍵值對時,您已經擁有該鍵,您不需要爲其創建一個鍵。將一個按鍵插入數組是沒有意義的。您只能設置數組中特定鍵的值。

// no key 
array_push($array, $value); 
// same as: 
$array[] = $value; 

// key already known 
$array[$key] = $value; 
41

您可以使用聯合運算符(+)合併數組並保留添加的數組的鍵。例如:

<?php 

$arr1 = array('foo' => 'bar'); 
$arr2 = array('baz' => 'bof'); 
$arr3 = $arr1 + $arr2; 

print_r($arr3); 

// prints: 
// array(
// 'foo' => 'bar', 
// 'baz' => 'bof', 
//); 

所以你可以做$_GET += array('one' => 1);

有關工會運營商的使用情況的更多信息,請登錄http://php.net/manual/en/function.array-merge.php的文檔array_merge

+1

'array_merge()'和'+'運算符之間的基本區別是: 2數組在同一個鍵上包含值'+'操作符忽略第二個數組的值(不會覆蓋),也不會重新編號/重新索引數字鍵... – 2017-02-16 21:35:29

9

我想我的答案添加到表,在這裏,它是:

//connect to db ...etc 
$result_product = /*your mysql query here*/ 
$array_product = array(); 
$i = 0; 

foreach ($result_product as $row_product) 
{ 
    $array_product [$i]["id"]= $row_product->id; 
    $array_product [$i]["name"]= $row_product->name; 
    $i++; 
} 

//you can encode the array to json if you want to send it to an ajax call 
$json_product = json_encode($array_product); 
echo($json_product); 

希望這將幫助別人

+0

我查看了幾十種解決方案,這是唯一的解決方案這符合我的用例。謝謝! – 2017-02-13 17:16:22

1

有點晚了,但如果你不介意嵌套數組你可以採取這種做法:

$main_array = array(); //Your array that you want to push the value into 
$value = 10; //The value you want to push into $main_array 
array_push($main_array, array('Key' => $value)); 

爲了澄清, 如果輸出json_encode($ main_array)將廁所K類似於[{ 「關鍵」: 「10」}]

3

這是解決方案,可有用ü

Class Form { 
# Declare the input as property 
private $Input = []; 

# Then push the array to it 
public function addTextField($class,$id){ 
    $this->Input ['type'][] = 'text'; 
    $this->Input ['class'][] = $class; 
    $this->Input ['id'][] = $id; 
} 

} 

$form = new Form(); 
$form->addTextField('myclass1','myid1'); 
$form->addTextField('myclass2','myid2'); 
$form->addTextField('myclass3','myid3'); 

當你放棄它。這樣

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) 
2

A中的結果位奇怪,但這個工作對我

$array1 = array("Post Slider", "Post Slider Wide", "Post Slider"); 
    $array2 = array("Tools Sliders", "Tools Sliders", "modules-test"); 
    $array3 = array(); 

    $count = count($array1); 

    for($x = 0; $x < $count; $x++){ 
     $array3[$array1[$x].$x] = $array2[$x]; 
    } 

    foreach($array3 as $key => $value){ 
     $output_key = substr($key, 0, -1); 
     $output_value = $value; 
     echo $output_key.": ".$output_value."<br>"; 
    } 
1
$arr = array("key1"=>"value1", "key2"=>"value"); 
    print_r($arr); 

//輸出陣列[ 'KEY1'=> 「VALUE1」, 'KEY2'=>」值2「]

0
array_push($arr, ['key1' => $value1, 'key2' => value2]); 

這工作得很好。 其陣列中的價值創造的關鍵

+2

已下調。這只是在現有的'$ arr'數組的末尾推入一個新的數組。 – AlexioVay 2017-02-21 13:18:04

0

我不知道爲什麼最簡單了方法尚未發佈尚未:

$arr = ['company' => 'Apple', 'product' => 'iPhone']; 
$arr += ['version' => 8]; 

這是相同的像array_merge.

+0

它不完全一樣,在array_merge中,右邊的數組贏得了鍵衝突,在「+ =」左邊的數組贏得勝利 – 2018-01-31 16:18:39

0

合併兩個數組在一起您好我有同樣的問題,我覺得這是解決方案,你應該使用兩個數組,然後將它們結合起來既

<?php 

$fname=array("Peter","Ben","Joe"); 

$age=array("35","37","43"); 

$c=array_combine($fname,$age); 

print_r($c); 

?> 

參考:w3schools

0

array_push($ GET,GET $ [ '一個'] = 1);

作品我

0

實施例array_merge()....

$array1 = array("color" => "red", 2, 4); $array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4); $result = array_merge($array1, $array2); print_r($result);

陣列([顏色] =>綠,[0] => 2,[1] => 4 ,[2] => a,[3] => b,[形狀] =>梯形,[4] => 4,