2013-12-12 1 views
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); 
} 
+0

PROTIP : 툴바의'''버튼이 아닌'{}'버튼을 사용하십시오 .. –

+0

원하는 배열이 이해가되지 않습니다 :'[ '' 타입 '=>' ', 0 => [],'타입 '=>' ', 1 => []]'여러 타입 키는 가질 수 없습니다. 당신이 원한다면? –

+0

stackoverflow wouldnt 나는 코드의 양과 비교하여 충분한 "텍스트"를 작성하지 않기 때문에 {}을 사용하게한다. 더 많은 텍스트! = 더 나은 문제 (waffling)를 설명했다. 다차원 배열을 만들고 모든 숫자 인덱스를 "own"으로 변환합니다. $ variable 그래서 할 수 있습니다 : http://redbeanphp.com/cooker – codepreneur

답변

1

array_insert2 두번째 PARAM의 위치이다. 첫 번째 코드에서는 실제로 정수로 지정하여 유효한 위치 ($ p, array_splice 함수와 결합)로 만듭니다. 두 번째 코드에서 array_insert2에 제공되는 위치는 $ results [$ p]의 foreach 루프에있는 키 ($ k)입니다. 주어진 키는 array_splice 함수에서 올바르게 사용할 수 없습니다. 아마 $ k를 제공하는 대신 $ results [$ p]에 $ k의 array_search 결과를 주면됩니다.

short-answer : 두 번째 코드에서 제공되는 위치는 정수가 아니므로 array_splice 함수에 사용할 수 없습니다.

관련 문제