Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用PHP实现动态数组_Php_Mysql_Arrays - Fatal编程技术网

用PHP实现动态数组

用PHP实现动态数组,php,mysql,arrays,Php,Mysql,Arrays,更新后 我想在PHP中创建动态数组,以提供与下面的静态数组相同的输出,我曾尝试使用while语句,但它不起作用。你能给我一些建议吗 我想使用MySQL中的2个值,并将它们保存到$to变量中 因此,首先将_发送到1(用户帐户),然后发送值1(金额) 只需将元素添加到数组中即可: //Setup blank array $example = array(); //Create a loop, for example purposes. foreach(range(0, 99) as $i){

更新后

我想在PHP中创建动态数组,以提供与下面的静态数组相同的输出,我曾尝试使用while语句,但它不起作用。你能给我一些建议吗

我想使用MySQL中的2个值,并将它们保存到$to变量中 因此,首先将_发送到1(用户帐户),然后发送值1(金额)


只需将元素添加到数组中即可:

//Setup blank array
$example = array();

//Create a loop, for example purposes.
foreach(range(0, 99) as $i){

    //Create random variable that we'll add to our array
    $randVar = mt_rand(1, 90000);

    //$i will be our array key/index
    $example[$i] = randVar;

}

//var_dump the array so you can see the structure / end result
var_dump($example);
您也可以这样创建它:

//Create random array key/index
$myRandKey = mt_rand(1, 90000);

//Create random variable value.
$myRandVar = mt_rand(1, 90000);

//Setup an array
$example = array(
    $myRandKey => $myRandVar
);

//Another array key that we'll add to our array
$secondKey = 'test';

//Add it
$example[$secondKey] = 'This is just an example!';

//Dump out the array
var_dump($example);
array\u push也可以工作(使用mysql\u fetch\u assoc,如您的示例所示):

在您的特定示例中(因为您添加了代码):

应改为:

print_r($new_array[] = $row);
在您的代码中,我将其更改为:

$new_array = array();
while($row = mysqli_fetch_array($result)){
  $new_array[] = $row;
}
或者,如果要按唯一列(例如主键)为数组设置键,请执行以下操作:


只需将元素添加到数组中即可:

//Setup blank array
$example = array();

//Create a loop, for example purposes.
foreach(range(0, 99) as $i){

    //Create random variable that we'll add to our array
    $randVar = mt_rand(1, 90000);

    //$i will be our array key/index
    $example[$i] = randVar;

}

//var_dump the array so you can see the structure / end result
var_dump($example);
您也可以这样创建它:

//Create random array key/index
$myRandKey = mt_rand(1, 90000);

//Create random variable value.
$myRandVar = mt_rand(1, 90000);

//Setup an array
$example = array(
    $myRandKey => $myRandVar
);

//Another array key that we'll add to our array
$secondKey = 'test';

//Add it
$example[$secondKey] = 'This is just an example!';

//Dump out the array
var_dump($example);
array\u push也可以工作(使用mysql\u fetch\u assoc,如您的示例所示):

在您的特定示例中(因为您添加了代码):

应改为:

print_r($new_array[] = $row);
在您的代码中,我将其更改为:

$new_array = array();
while($row = mysqli_fetch_array($result)){
  $new_array[] = $row;
}
或者,如果要按唯一列(例如主键)为数组设置键,请执行以下操作:


实际上你差点就明白了。它在“数组”中较低

要初始化空数组,请执行以下操作:

$to = array();
在您的情况下(您已经有了一些值),您可以执行以下操作:

$to = array(
   $send_to1 => $value1,
   $send_to2=> $value2
);
在这两种情况下,您都可以在以后添加更多元素

$to[$someOtherKey] = $someOtherValue;

事实上,你几乎成功了。它在“数组”中较低

要初始化空数组,请执行以下操作:

$to = array();
在您的情况下(您已经有了一些值),您可以执行以下操作:

$to = array(
   $send_to1 => $value1,
   $send_to2=> $value2
);
在这两种情况下,您都可以在以后添加更多元素

$to[$someOtherKey] = $someOtherValue;

听着,这太容易了,你只需要更加注意你在这里得到的答案

以下是最简单的方法:

$to = array();
$to[] = array($send_to1 => $value1);
$to[] = array($send_to2 => $value2);

while ( $row = mysqli_fetch_array($result) ) {
    $to[] = array($row['send_tox' => $row['valuex']);
}

您需要首先了解数组和循环在PHP中的工作方式,然后尝试在循环中创建动态数组。

看,这很简单,您只需要更加注意这里的答案

以下是最简单的方法:

$to = array();
$to[] = array($send_to1 => $value1);
$to[] = array($send_to2 => $value2);

while ( $row = mysqli_fetch_array($result) ) {
    $to[] = array($row['send_tox' => $row['valuex']);
}

您需要首先了解数组和循环在PHP中的工作方式,然后尝试在循环中创建动态数组。

$to[$send\u toX]=$valueX$new\u array[]=$row[]
更改为
$new\u array[]=$row
$row
已经是一个数组替换
打印($new\u array[]=$row[])
$new_array[]=$row
@user2511149
$to[$i]['valx']=$row['valx']。。。但这里需要一个计数器,或者至少需要一个
id
到[$send\u toX]=$valueX$new\u array[]=$row[]
更改为
$new\u array[]=$row
$row
已经是一个数组替换
打印($new\u array[]=$row[])
$new_array[]=$row
@user2511149
$to[$i]['valx']=$row['valx']。。。但是你需要一个计数器,或者至少需要一个
id
,看看我更新的答案。请参阅答案末尾添加的代码。另外,如果您得到数组(0),可能是因为您的查询没有返回任何行和/或查询失败,并且您没有检查是否有MySQL错误。@user25111459发送到变量的来源是哪里?请看我更新的答案。请参阅答案末尾添加的代码。此外,如果您得到数组(0),可能是因为您的查询没有返回任何行和/或查询失败,并且您没有检查是否有MySQL错误。@User2511149发送到变量的来源是哪里?