2017-01-09 3 views
1

функция 1:Как объединить массивы из цикла foreach?

function print_vcard($card, $hide){ 

    $names = array('N', 'FN', 'TITLE', 'TEL', 'EMAIL', 'URL', 'ADR', 'NOTE'); 

    $row = 0; 

    foreach ($names as $name) { 
     if (in_array_case($name, $hide)) { 
      continue; 
     } 
     $properties = $card->getProperties($name); 
     if ($properties) { 
      foreach ($properties as $property) { 
       $show = true; 
       $types = $property->params['TYPE']; 
       if ($types) { 
        foreach ($types as $type) { 
         if (in_array_case($type, $hide)) { 
          $show = false; 
          break; 
         } 
        } 
       } 
       if ($show) { 
        $class = ($row++ % 2 == 0) ? "property-even" : "property-odd";     
        print_vcard_property($property, $class, $hide); 
       } 
      } 
     } 
    } 
} 

функция 2:

function print_vcard_property($property, $class, $hide){ 
     $items = array(); 
     $name = $property->name; 
     $value = $property->value; 
     $types = $property->params['TYPE']; 
     switch ($name) { 
      case 'N': 
       $name = $property->getComponents(); 
       print_r(array_filter($name)); 
       break; 
       case 'TEL': 
       $phone = $property->getComponents(); 
       print_r(array_filter($phone)); 
       break; 
       case 'ADR': 
       $adr = $property->getComponents(); 
       print_r(array_filter($adr)); 
       break; 
      default: 
       $components = $property->getComponents(); 
       $lines = array(); 
       foreach ($components as $component) { 
        if ($component) { 
         $lines[] = $component; 
        } 
       } 
       $html = join("\n", $lines); 
       break; 
     } 
     echo "<br><br>"; 
    } 

мой выход:

Array ([0] => Miller [1] => Sam) 

Array ([0] => 434234234) 

Array ([2] => Sunstreet 3211 [3] => Miami [5] => 1234) 

Я хочу, чтобы объединить теперь выход в один массив:

print_r(array_merge($name,$phone,$adr)); 

Но я получаю сообщение об ошибке:

Warning: array_merge(): Argument #2 is not an array in mypage.php on line 259 
+0

Он работал при замене внутри функции 1 'print_vcard_property ($ собственность, $ класса $ скрыть),' с содержанием функции 2 – Jarla

+0

добавьте этот комментарий в качестве ответа и примите его, чтобы ваш вопрос больше не выглядел без ответа. – Veve

ответ

0

рабочего раствора:

function print_vcard($card, $hide){ 

    $names = array('N', 'FN', 'TITLE', 'TEL', 'EMAIL', 'URL', 'ADR', 'NOTE'); 

    $row = 0; 

    foreach ($names as $name) { 
     if (in_array_case($name, $hide)) { 
      continue; 
     } 
     $properties = $card->getProperties($name); 
     if ($properties) { 
      foreach ($properties as $property) { 
       $show = true; 
       $types = $property->params['TYPE']; 
       if ($types) { 
        foreach ($types as $type) { 
         if (in_array_case($type, $hide)) { 
          $show = false; 
          break; 
         } 
        } 
       } 
       if ($show) { 
        $class = ($row++ % 2 == 0) ? "property-even" : "property-odd";     
        $items = array(); 
        $name = $property->name; 
        $value = $property->value; 
        $types = $property->params['TYPE']; 
        switch ($name) { 
         case 'N': 
          $name = $property->getComponents(); 
          print_r(array_filter($name)); 
          break; 
          case 'TEL': 
          $phone = $property->getComponents(); 
          print_r(array_filter($phone)); 
          break; 
          case 'ADR': 
          $adr = $property->getComponents(); 
          print_r(array_filter($adr)); 
          break; 
          default: 
          $components = $property->getComponents(); 
          $lines = array(); 
          foreach ($components as $component) { 
          if ($component) { 
           $lines[] = $component; 
          } 
         } 
         $html = join("\n", $lines); 
         break; 
        } 
        echo "<br><br>"; 
       } 
      } 
     } 
    print_r(array_merge($name,$phone,$adr)); 
    } 
} 
Смежные вопросы