2013-03-07 2 views
0

배열이 있습니다. 이php - 배열 값 앞에 키가 붙습니다.

$choices = array(
array('label' => 'test1','value' => 'test1'), 
array('label' => 'test2','value' => 'test2'), 
array('label' => 'test3','value' => 'test3'), 
) 

지금 난 내 배열 키를 가지고 있기 때문에 내가 array_unshift 기능을 사용할 수 없습니다 것 같습니다 $choices 배열

array('label' => 'All','value' => 'all'), 

이 값 앞에 추가하고 싶은 것 같습니다.

누군가 앞에 줄 수있는 방법을 알려주시겠습니까?

+0

예, 정상적으로 작동합니다. $ 선택 항목에 명시 적으로 키를 정의하지 않았더라도 모든 ** HAS **에 키가 있습니다. –

+0

'array_unshift'를 사용할 수없는 이유를 모르겠습니다. 모든 배열에는 키가 있습니다. – jeroen

+0

가능한 것처럼 보입니다. 외부 배열은 숫자 키가있는 목록입니다. 내부에 넣은 것 (뚜렷한 연관 배열)은 중요하지 않습니다. – mario

답변

2

$choices 배열에는 숫자 키만 있으므로 array_unshift()은 원하는대로 정확하게 숫자를 입력 할 수 있습니다.

$choices = array(
    array('label' => 'test1','value' => 'test1'), 
    array('label' => 'test2','value' => 'test2'), 
    array('label' => 'test3','value' => 'test3'), 
); 
echo $choices[0]['label']; // echoes 'test1' 

$array_to_add = array('label' => 'All','value' => 'all'); 
array_unshift($choices, $array_to_add); 

/* resulting array would look like this: 
$choices = array(
    array('label' => 'All','value' => 'all') 
    array('label' => 'test1','value' => 'test1'), 
    array('label' => 'test2','value' => 'test2'), 
    array('label' => 'test3','value' => 'test3'), 
); 
*/ 
echo $choices[0]['label']; // echoes 'All' 
+0

예. 작동합니다. 감사. 7 분 후에 너의 대답을 수락하겠다. – Giri