Php函数返回关联数组

时间:2013-05-25 10:14:05

标签: php arrays function

我想知道如何使函数返回一个关联数组。我只获取返回数组的值而不是键

1 个答案:

答案 0 :(得分:5)

你需要自己设置这些键。如果没有,它会自动添加从0开始的索引。看看我的例子

function myfunc(){
    $arr = array();
    $arr[] = 'value0';
    $arr['key1'] = 'value1';
    $arr['key2'] = 'value2';
    $arr[] = 'value3';
    return $arr;
}

将输出

// you see the first and fourth value has the index 0 and 1, 
// the other those that are defined
array(4) {
  [0]=>
  string(6) "value0"
  ["key1"]=>
  string(6) "value1"
  ["key2"]=>
  string(6) "value2"
  [1]=>
  string(6) "value3"
}

http://php.net/manual/de/language.types.array.php