如何从foreach循环内部合并数组?

时间:2017-01-09 09:56:24

标签: php arrays foreach merge

功能1:

 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

1 个答案:

答案 0 :(得分: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));
    }
}