0
이 코드 :키와 값을 선택하여 다른 배열의 각 요소 앞에 배열을 추가하는 방법은 무엇입니까?
for ($p=0;$p<count($results);$p++){
foreach ($results[$p] as $k => $v){
$typeee = ['type' => strtolower(str_replace('_','',$results[$p][$k]['metric']))."container"];
array_insert2($results[$p],0,$typeee);
}
}
print_r($results);
이 나에게주는이 :
Array (
[0] => Array
(
[type] => pagestoriescontainer
[0] => Array
(
[type] => pagestories
[object_id] => 123456778
[metric] => page_stories
[end_time] => 1386057600
[period] => 86400
[value] => 2090
)
[1] => Array
(
[type] => pagestorytellers
[object_id] => 123456778
[metric] => page_storytellers
[end_time] => 1386057600
[period] => 86400
[value] => 2041
)
[...]
[1] => Array
(
[0] => Array
(
[type] => pagestories
[object_id] => 199193463123456778
[metric] => page_stories
[end_time] => 1386057600
[period] => 86400
[value] => 0
)
[1] => Array
(
[type] => pagestorytellers
[object_id] => 199193463123456778
[metric] => page_storytellers
[end_time] => 1386057600
[period] => 86400
[value] => 0
)
[...]
그러나이 코드 :
Array (
[0] => Array
(
[type] => pagestoriescontainer
[0] => Array
(
[type] => pagestories
[object_id] => 123456778
[metric] => page_stories
[end_time] => 1386057600
[period] => 86400
[value] => 2090
)
[type] => pagestorytellerscontainer
[1] => Array
(
[type] => pagestorytellers
[object_id] => 123456778
[metric] => page_storytellers
[end_time] => 1386057600
[period] => 86400
[value] => 2041
)
[...]
[1] => Array
(
[type] => pagestoriescontainer
[0] => Array
(
[type] => pagestories
[object_id] => 199193463123456778
[metric] => page_stories
[end_time] => 1386057600
[period] => 86400
[value] => 0
)
[type] => pagestorytellerscontainer
[1] => Array
(
[type] => pagestorytellers
[object_id] => 199193463123456778
[metric] => page_storytellers
[end_time] => 1386057600
[period] => 86400
[value] => 0
)
[...]
이유 :
이for ($p=0;$p<count($results);$p++){
foreach ($results[$p] as $k => $v){
$typeee = ['type' => strtolower(str_replace('_','',$results[$p][$k]['metric']))."container"];
array_insert2($results[$p],$k,$typeee);
}
}
날이 제공하지 않습니다? 내가 원하는 것을 어떻게 얻을 수 있습니까? :) 또한
,
function array_insert2 (&$array, $position, $insert_array) {
$first_array = array_splice ($array, 0, $position);
$array = array_merge ($first_array, $insert_array, $array);
}
PROTIP : 툴바의'''버튼이 아닌'{}'버튼을 사용하십시오 .. –
원하는 배열이 이해가되지 않습니다 :'[ '' 타입 '=>' ', 0 => [],'타입 '=>' ', 1 => []]'여러 타입 키는 가질 수 없습니다. 당신이 원한다면? –
stackoverflow wouldnt 나는 코드의 양과 비교하여 충분한 "텍스트"를 작성하지 않기 때문에 {}을 사용하게한다. 더 많은 텍스트! = 더 나은 문제 (waffling)를 설명했다. 다차원 배열을 만들고 모든 숫자 인덱스를 "own"으로 변환합니다. $ variable 그래서 할 수 있습니다 : http://redbeanphp.com/cooker – codepreneur