Array push - change name on key

434 Views Asked by At

I can not get this to work.
How do I change key [0] to [id]. In the below array ex. I want the [0] => 2 to look like [id] => 2

Array ex.:

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

My php script:

<?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");
}
?> 

SOLVED

<?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");
}
?>
2

There are 2 best solutions below

0
On BEST ANSWER

You should not use array_push for this

$workCard['id'] = $id;

If it is already setted you can unset($workCard[0])

0
On

Use this:

$workCard['id'] = $id;