2013-08-22 42 views
-2

我有这样的阵列中的PHP:分离PHP数组值

$a[]="Jason White"; 
$a[]="Jean Black"; 
$a[]="Billy Brown"; 

但我将在表中显示的值和应该是这样的:

+-----------+--------------+ 
| Firstname | Lastname  | 
+-----------+--------------+ 
| Jason  | White  | 
+-----------+--------------+ 
| Jean  | Black  | 
+-----------+--------------+ 
| Billy  | Brown  | 
+-----------+--------------+ 

是有可能做那样呢? 或任何建议,使这张表?

在此先感谢。

+2

是其可能,通过使用简单的'foreach'和'explode'你可以做到这一点,比如'list($ firstname,$ latname)= explode(“”,$ var)' – 2013-08-22 06:22:40

+1

Yes是你的问题的答案。但是,你还没有尝试过一些东西。 –

回答

1

尝试像

echo "<table>"; 
foreach($a as $name) { 
    echo "<tr>"; 
    $name_arr = explode(" ",$name); 
    echo "<td>".$name_arr[0]."</td>"; 
    echo "<td>".$name_arr[1]."</td>"; 
    echo "</tr>"; 
} 
echo "</table>"; 
+0

谢谢队友。但是,如果我想只显示1行,例如。 – WATTS

0

与空间的爆发,并显示它

0

是的,它是可能的。这个例子假设文本“名字”和“姓氏”没有从PHP产生:

<?php 
$a = array 
(
    'Jason White', 
    'Jean Black', 
    'Billy Brown', 
); 
?> 

<table> 
    <tr> 
    <th>Firstname</th> 
    <th>Lastname</th> 
    </tr> 
<?php 
    foreach($a as $val) 
    { 
    // creates an array from a string 
    // basing on a string character 
    // to be used as an indicator of new array value 
    $separate = explode(' ', $val); 

    echo '<tr>'; 

    // to avoid 'undefined index' errors, 
    // loop through the array that was just created. 
    foreach($separate as $display) 
    { 
     echo '<td>'. $display .'</td>'; 
    } 

    echo '</tr>'; 
    } 
?> 

</table> 
0

拆分及存储串入阵列中使用PHP函数explode

list($firstName, $lastName) = explode(" ", $var);