开发者

add key to array at user defined position [duplicate]

开发者 https://www.devze.com 2023-04-04 11:04 出处:网络
This question already has answers here: 开发者_如何学运维Closed 11 years ago. Possible Duplicate:
This question already has answers here: 开发者_如何学运维 Closed 11 years ago.

Possible Duplicate:

array_splice() for associative arrays

I have an associative array with products data.

Array
(
    [0] => Array
        (
            [ID] => 1
            [Name] => Game 1
            [Price] => 19.95
            [Status] => active
        )
    [1] => Array etc..
)

How can I add a key to any location in the array and not just the beginning or the end? Eg. between intID and strName, or at the/beginning, or before enumStatus?


You can sort them or rewrite them, for example in foreach loop

$newarray = array();
foreach($array as $index => $data){
   $newarray[$index]['ID'] = $data['ID'];
   $newarray[$index]['Price'] = $data['Price'];
   $newarray[$index]['Name'] = $data['Name'];
   $newarray[$inedx]['Status'] = $data['Status'];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消