2017-11-06 7 views
0

어떻게이 assoc 배열을 배열 (제목, 설명)을 가진 배열에 병합 할 수 있습니까? 내 요구 사항에 맞는 GetContent 함수의 데이터 정렬. foreach는 결과의여러 개의 연관 배열을 하나로 병합하는 방법은 무엇입니까?

function getContent($data) { 
     $tabs = $data->result->data->tab;  
     $type = findByType($tabs,'content'); 
      $content = array(); 
      foreach ($type->unified_content->item as $item) { 
       if($item->type->name == 'header') { 
        $content[] = array(
         'title' => $item->text 
        ); 
       } else { 
        $content[] = array(
         'description' => $item->text 
        ); 
       } 
      } 
      return $content;   
    } 

vardump, 어떻게 내가 제목 + 설명을 병합 할 수 있습니다 :

array(1) { 
    ["title"]=> 
    string(13) "Test článku" 
} 
array(1) { 
    ["description"]=> 
    string(20) "Nový článek test." 
} 
array(1) { 
    ["title"]=> 
    string(15) "Test článku 2" 
} 
array(1) { 
    ["description"]=> 
    string(22) "Nový článek test 2." 
} 
array(1) { 
    ["title"]=> 
    string(15) "Test článku 3" 
} 
array(1) { 
    ["description"]=> 
    string(22) "Nový článek test 3." 
} 
+0

병합 할 2 개의 배열을 표시하겠습니까? – Mark

+0

가능한 복제본 [PHP : 두 개의 배열을 병합하면서 키를 다시 색인화하는 대신 병합 할 수 있습니까?] (https://stackoverflow.com/questions/3292044/php-merge-two-arrays-while-keeping-keys-instead-of-reindexing)) – kabanus

+0

좋은 옛날 배열'+'연산자를 잊지 마라. – biziclop

답변

1

로 , 임시 배열을 사용하여 titledescription을 다음과 같이 저장할 수 있습니다. ,

foreach ($type->unified_content->item as $item) { 
    if($item->type->name == 'header') { 
     $arr = []; 
     $arr['title'] = $item->text; 
    } else { 
     $arr['description'] = $item->text; 
     $content[] = $arr; 
    } 
} 

그렇지 않은 경우 배열을 병합해야합니다.

+0

고마워 :)) – David

0

하면 사용자가 array_merge 수

같은 콘텐츠 엄격한 순서가있는 경우

foreach ($type->unified_content->item as $item) { 
       if($item->type->name == 'header') { 
        $content = array_merge($content,array(
         'title' => $item->text 
        )); 
       } else { 
        $content = array_merge($content,array(
         'description' => $item->text 
        )); 
       } 
      } 
+0

결과가 마지막 배열 일 뿐이므로 좋지 않습니다. string (15) "Test čl k 3" string (22) "Nový článek test 3" – David

0

당신은 제목 한 배열의 설명를 저장하고 $의 콘텐츠 배열에 전달할 수 있습니다.

이 교체 :이

foreach ($type->unified_content->item as $item) { 
    if($item->type->name == 'header') { 
     $content[] = array(
      'title' => $item->text 
     ); 
    } else { 
     $content[] = array(
      'description' => $item->text 
     ); 
    } 
} 

:

$i = 0; 
$content_temp = array(); 
foreach ($type->unified_content->item as $item) { 
    if($item->type->name == 'header') { 
     $content_temp['title'] = $item->text; 
    } else { 
     $content_temp['description'] = $item->text 
    } 
    $i++; 
    if($i % 2 == 0){ 
     $content[] = $content_temp; 
    } 
} 

전체 코드 :

function getContent($data) { 
    $tabs = $data->result->data->tab;  
    $type = findByType($tabs,'content'); 
    $content = array(); 
    $i = 0; 
    $content_temp = array(); 
    foreach ($type->unified_content->item as $item) { 
     if($item->type->name == 'header') { 
      $content_temp['title'] = $item->text; 
     } else { 
      $content_temp['description'] = $item->text 
     } 
     $i++; 
     if($i % 2 == 0){ 
      $content[] = $content_temp; 
     } 
    } 
    return $content;   
} 
0

array_me rge()가 더 효율적이지만 두 가지 옵션이 있습니다.

$array1 = array("id1" => "value1"); 

$array2 = array("id2" => "value2", "id3" => "value3", "id4" => "value4"); 

$array3 = array_merge($array1, $array2/*, $arrayN, $arrayN*/); 
$array4 = $array1 + $array2; 

echo '<pre>'; 
var_dump($array3); 
var_dump($array4); 
echo '</pre>';