2011-08-05 9 views
-1

다른 다차원 배열 한 다차원 배열을 추가 개의 어레이내가 가진 동일한 키

이하
$input = Array 
(
    [0] => Array 
     (
      [section] => 725 
      [location] => New Building - 26 
      [rows] => DDD 
      [seats] => DDD 
     ) 

    [1] => Array 
     (
      [section] => 721 
      [location] => Helipad - II 
      [rows] => R1,R2 
      [seats] => S1,S2 
     ) 

    [2] => Array 
     (
      [section] => 724 
      [location] => NDTV Times 
      [rows] => R1,R2,R3 
      [seats] => S1,S2,S3 
     ) 
); 

는 제 2 어레이

$extra = Array 
(
    [0] => dry||Obstacles Present||yes 
    [1] => wet||Not Find||no 
    [2] => icy||||yes 
    [3] => 
) 

내가 아래 요구 수량 배열을 필요

$output =Array 
(
    [0] => Array 
     (
      [section] => 725 
      [location] => New Building - 26 
      [rows] => DDD 
      [seats] => DDD 
      [conditions] => dry 
      [obstacles] => Obstacles Present 
      [normal_lighting] => yes 
     ) 

    [1] => Array 
     (
      [section] => 721 
      [location] => Helipad - II 
      [rows] => R1,R2 
      [seats] => S1,S2 
      [conditions] => wet 
      [obstacles] => Not Find 
      [normal_lighting] => no 
     ) 

    [2] => Array 
     (
      [section] => 724 
      [location] => NDTV Times 
      [rows] => R1,R2,R3 
      [seats] => S1,S2,S3 
      [conditions] => icy 
      [obstacles] => 
      [normal_lighting] => yes 
     ) 
) 

나는 원하는 배열을 얻기 위해 시퀀스를 따라 갔다 :

foreach($extra as $efk=>$efv) 
{ 
    if(!empty($efv)) { 
     $arr_field_value[] = explode("||", $efv); 
    } 
} 
$arr_key = array('conditions','obstacles','normal_lighting'); 
foreach($arr_field_value as $fv) 
{ 
    $arr_extra_field[]=array_combine($arr_key,$fv); 
} 

foreach($input as $k=>$v) 
{ 
    $output[]=array_merge($v,$arr_extra_field[$k]); 
} 
echo "<pre>"; print_r($output); 

정말 길다는 것을 알고 있습니다. 그렇게 할 수있는 스마트 방법을 제안 해주세요.

당신은 WORKING DEMO

감사를 볼 수 있습니다.

+0

왜 downvotes을 찾고 생각하고 폐쇄 ??? 문제가있는 곳을 설명해주세요 : -X – diEcho

+0

안녕하세요 !! 내가 downvoting 및 폐쇄의 이유를 알고 싶습니다 ??? 도대체 문제가 – diEcho

답변

3
foreach($extra as $key => $val){ 
    if($val !== ''){ 
     list($conditions, $obstacles, $normal_lighting) = explode('||', $val); 
     $input[$key]['conditions'] = $conditions; 
     $input[$key]['obstacles'] = $obstacles; 
     $input[$key]['normal_lighting'] = $normal_lighting; 
    } 
} 
+0

+1 Yesss; 완벽 해. 고마워. – diEcho

0

난 당신이 array_merge_recursive()

foreach ($extra as $key => &$extra_elem) { 
    if (!$extra_elem) { 
     unset($extra[$key]); 
     continue; 
    } 
    $extra_elem = array_combine(array(
     'conditions', 
     'obstacles', 
     'normal_lighting', 
    ), explode('||', $extra_elem)); 
} 
$desired = array_merge_recursive($input, $extra); 
+0

출력은 내가 예상 한 것이 아니다 ... – diEcho