배열의 첫 번째 키가 1로 시작되며, 그와 같이 필요합니다. php array_map은 원래 하나의 키를 0으로 변경합니다.
//count($collections) = 56;
$collections = array_map(function($array)use($other_vars){
//more stuff here
//finally return:
return $arr + ['newVariable'=$newVal];
},$collections);
첫 번째 키를 var_dump($collections);
//first iteration of $collections
$collections[1] = $data1;
$collections[2] = $data2;
...
//It does not have to start with zero for my own purposes
그래서 나는 다음과 같이 필요한 물건을 할. 그러나
, 나는이 같은 배열에 또 다른 변수를 추가 할 :
//another array starting at one //count($anotherArray) = 56;
$anotherArray[] = ['more'=>'values'];
$collections = array_map(function($arr,$another)use($other_vars){
//more stuff here
//finally return:
return $arr + ['newVariable'=$newVal,'AnotherVar'=>$another['key']];
},$collections,$anotherArray);
을 그럼 난 다시 $ 컬렉션을 반복하는 경우는, 지금은 0에서 시작. 왜? 첫 번째 키에서 0 대신 1부터 시작하도록하려면 어떻게해야합니까? 아이디어가 있으십니까?
왜 첫 번째 키가 0으로 변경 되었습니까? 나는 그것을 어떻게 유지할 수 있습니까?
는 다음과 같은 코드 (for example on php online)을 실행하여 문제를 재현 할 수 있습니다 lerouche
echo '<hr>';
array_unshift($grandcollection2, null);
unset($grandcollection2[0]);
var_dump($grandcollection2);
에 의해
$collections[1]=['data1'=>'value1'];
$collections[2]=['data2'=>'value2'];
$collections[3]=['data3'=>'value3'];
$collections[4]=['data4'=>'value4'];
$another[1]=['AnotherData'=>'AnotherVal1'];
$another[2]=['AnotherData'=>'AnotherVal2'];
$another[3]=['AnotherData'=>'AnotherVal3'];
$another[4]=['AnotherData'=>'AnotherVal4'];
var_dump($collections);
echo '<hr>';
var_dump($another);
echo '<hr>';
$grandcollection=array_map(function($a){
return $a + ['More'=>'datavalues'];
},$collections);
var_dump($grandcollection);
echo '<hr>';
$grandcollection2 = array_map(function($a,$b){
return $a + ['More'=>'datavalues','yetMore'=>$b['AnotherData']];
},$collections,$another);
var_dump($grandcollection2);
지금 제안 된 솔루션을 추가하는 것은 의도 한대로 지금 수행 작업
'array_map를 첫 번째 실제 요소를 이동합니다, 그냥 처리하는 값을 가져 와서 결과 배열을 반환합니다. – Barmar
배열 색인은 일반적으로 0에서 시작합니다. – Barmar
[배열 키를 변경하여 0 대신 1에서 시작하는 방법] 가능한 복제본 (http://stackoverflow.com/questions/5374202/how-to-change-the-array- 0에서 시작하여 1에서 시작하는 키 - 시작) – mickmackusa