开发者

Insert index/Keys into an array

D

Could you give a an advice how to insert keys/index to a an array in a specific order as you can se.... I would like to in the result index the values id1 = 3 id2 = 4 id3 = 5. How do I do that? This code

public static function getTest($ids){
    $input = array();
    foreach ($ids as $id) {
        $input['result'] = $ids;
    }

    $result = array('status'=>"success",
                    'message'=>"blah blah",
             开发者_运维技巧       'result'=> $ids
              );
    var_dump($result);
    return $result;
}

produces this (getTest is called from another file and it gives out array(3,4,5))

array(3) { 
    ["status"]=> string(7) "success" 
    ["message"]=> string(9) "blah blah" 
    ["result"]=> array(3) { 
        [0]=> int(3) 
        [1]=> int(4) 
        [2]=> int(5) 
    }
} 


If you create an array the elements are in the order you added them:

$a = array();
$a[2] = 2;
$a[1] = 1;
$a[9] = 9;
var_dump($a);
// array(3) { [2]=> int(2) [1]=> int(1) [9]=> int(9) }

I am not really sure what you want but have a look here the page always helps my lot.

Response to comment: Try this

foreach ($ids as $key => $id) {
    $input['result']['ID'.$key] =  $id;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜