php数组由多个集合组成

时间:2014-10-24 11:03:54

标签: php mysql arrays

我有一组组中的数据 每个组包含3个值作为名称,移动,消息。

如何在数组中存储这些多个值并获得单独组的回声。??

<?php
$full_name = array( );

$full_name["name"] = "john1";
$full_name["mobile"] = "923456812536";
$full_name["message"] = "message1";

$full_name["name"] = "john2";
$full_name["mobile"] = "565656565656";
$full_name["message"] = "message2";

$full_name["name"] = "john3";
$full_name["mobile"] = "444442222222222";
$full_name["message"] = "message3";

$full_name["name"] = "john4";
$full_name["mobile"] = "2222";
$full_name["message"] = "2222222222";

echo $full_name["name"]."</br>".
     $full_name["mobile"]."</br>".
     $full_name["message"]."</br></br>"

?>

3 个答案:

答案 0 :(得分:2)

你可以使用像这样的多维数组

$full_name["name"][0] = "john1";
$full_name["mobile"][0] = "923456812536";
$full_name["message"][0] = "message1";

$full_name["name"][1] = "john2";
$full_name["mobile"][1] = "565656565656";
$full_name["message"][1] = "message2";

回应像这样的团体

echo implode(", ", $full_name["name"]);

但是,您需要确保["name"][x]["mobile"][x]对应,因为这些子阵列彼此独立。如果您使用Mark Ba​​ker的回答,您知道[x]["name"][x]["mobile"]属于同一个人

答案 1 :(得分:2)

将插入插入数组中,如:

$full_name[] = array(
    "name" => "john1",
    "mobile" => "923456812536",
    "message" => "message1",
);

给自己一个多维数组

然后使用

循环
foreach($full_name as $name) {
    echo $name['name'], ' ', $name['mobile'], ' ', $name['message'], PHP_EOL;
}

答案 2 :(得分:0)

这应该适合您:使用[]将数据插入数组的下一个字段

<?php 
    $full_name = array( );

    $full_name["name"][] = "john1";
    $full_name["mobile"][] = "923456812536";
    $full_name["message"][] = "message1";

    $full_name["name"][] = "john2";
    $full_name["mobile"][] = "565656565656";
    $full_name["message"][] = "message2";

    $full_name["name"][] = "john3";
    $full_name["mobile"][] = "444442222222222";
    $full_name["message"][] = "message3";

    $full_name["name"][] = "john4";
    $full_name["mobile"][] = "2222";
    $full_name["message"][] = "2222222222";

    print_r($full_name["name"]);
    echo "</br>";
    print_r($full_name["mobile"]);
    echo "</br>";
    print_r($full_name["message"]);
?>