【问题标题】:Array push - change name on key数组推送 - 更改键名
【发布时间】:2014-11-10 13:43:10
【问题描述】:

我无法让它工作。
如何将密钥 [0] 更改为 [id]。在下面的数组中。我希望 [0] => 2 看起来像 [id] => 2

数组示例:

Array
(
    [pickup] => 2014/11/10 15:15
    [tire_front] => tire_front
    [service] => 4
    [message] => 
    [user] => 1
    [0] => 2
)

我的 php 脚本:

<?php
if (isset($_POST['rep_list'])) {
    //insert database connect script here 
    $workCard = $_POST['rep_list'];
    array_push($workCard, $id); 
    print_r($workCard) ."<br>"; 
}
else{
    echo("errorrrrr");
}
?> 

已解决

<?php
if (isset($_POST['rep_list'])) {
    //insert database connect script here 
    $workCard = $_POST['rep_list'];
    $workCard['id'] = $id;
    print_r($workCard) ."<br>"; 
}
else{
    echo("errorrrrr");
}
?>

【问题讨论】:

  • $a['id'] = $a[0]; unset($a[0]); 也许?

标签: php arrays array-push array-key


【解决方案1】:

您不应为此使用array_push

$workCard['id'] = $id;

如果已经设置,可以unset($workCard[0])

【讨论】:

    【解决方案2】:

    使用这个:

    $workCard['id'] = $id;
    

    【讨论】:

      猜你喜欢
      • 2021-09-21
      • 1970-01-01
      • 2011-12-10
      • 1970-01-01
      • 2014-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-14
      相关资源
      最近更新 更多