簡體   English   中英

使用php將所有數據存儲在一個數組中

[英]All Data Store in one Array using php

while 循環生成 12 位數字。 將此數字存儲在數組中的 while 循環之外。

for($i=1;$i<5;$i++){
    $active_id=''; 
    $count=0;
    $active_code=''; 
    $myarray = array();
    while ( $count < 12 ) {
        $random_digit = mt_rand(0, 9);
        $active_id .= $random_digit;
        $count++;
    }
    echo $active_id;
}
for($i=1;$i<5;$i++){
    $active_id='';
    $count=0;
    $active_code='';

    while ( $count < 12 ) {
        $value .= mt_rand(0, 9);
        $count++;
        $active_id[$i] = $value;
    }
    unset($value);
    $myarray[] = $active_id;
}
var_dump($myarray);

它生成一個數組,每個數字都有 12 位數字。

你得到一個巨大的數字,因為你只是一個接一個地打印值。 這樣做,而不是:

$nums = array();
for($i = 1; $i < 5; $i++){
    $active_id=''; 
    $count=0;
    while ( $count < 12 ) {
        $active_id .= mt_rand(0, 9);
        $count++;
    }
    $nums[] = $active_id;
}
echo implode('<br>', $nums);

輸出類似:

547806804306
795608578570
440070793444
942559796496

您還可以使用循環來顯示值,如下所示:

foreach($nums as $v){
    echo $v . '<br>';
}

輸出:

462186324671
222725884540
242904883364
589742052131

請注意,我刪除了一些並不真正需要的變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM