Php 添加值;排列
我试图将带有array_push的随机数添加到“notes”=>[]字段中的数组中,但我认为我做得不对,因为我没有看到数组中存储的随机数 我是这样做的:Php 添加值;排列,php,arrays,Php,Arrays,我试图将带有array_push的随机数添加到“notes”=>[]字段中的数组中,但我认为我做得不对,因为我没有看到数组中存储的随机数 我是这样做的: $person1= [ 'name' => 'person1', 'note' => [] ]; $person2= [ 'name' => 'person2', 'note' => [] ]; $person3= [ 'name' => 'person3', '
$person1= [
'name' => 'person1',
'note' => []
];
$person2= [
'name' => 'person2',
'note' => []
];
$person3= [
'name' => 'person3',
'note' => []
];
$data=[$person1, $person2, $person3];
$_SESSION['data'] = $data;
function insertNumRandom(){
$data = $_SESSION['data'];
foreach ( $data as $student ) {
array_push($student['note'], rand(0,10));
}
}
function showNotes(){
foreach ( $data as $student ) {
echo implode($student['note']);
}
}
showNotes(); // To show the notes but it shows nothing.
您正在使用创建新对象的“as”关键字引用$data 相反,您应该遍历已有的对象
function insertNumRandom(){
$index = 0;
for($index = 0; $index<sizeof($data['data']); $index++ ) {
array_push($data[$index]['note'], rand(0,10));
}
}
函数insertNumRandom(){
$index=0;
对于($index=0;$index可以试试这个,但仍然不确定如何调用insertNumRandom()
函数
function showNotes($arr){
foreach ( $arr as $student ) {
var_dump(implode($student['note']));
}
}
insertNumRandom();
showNotes($data);
function showNotes(){
$data = $_SESSION['data'];
foreach ( $data as $student ) {
echo implode($student['note']);
}
}
您应该更改insertNumRandom,如下所示
function insertNumRandom(){
$data = $_SESSION['data'];
foreach ( $data as &$student ) {
array_push($student['note'], rand(0,10));
}
$_SESSION['data'] = $data;
}
showNotes函数
function showNotes($arr){
foreach ( $arr as $student ) {
var_dump(implode($student['note']));
}
}
insertNumRandom();
showNotes($data);
function showNotes(){
$data = $_SESSION['data'];
foreach ( $data as $student ) {
echo implode($student['note']);
}
}
并在showNotes之前调用insertNumRandom
insertNumRandom();
showNotes();
您的示例中有许多问题
首先,您使用$\u会话对象向函数传递某些值的方式是不必要的,也不干净。您可以在函数内部使用global$data
,它们会突然从父作用域访问$data变量。但即使这样也不是很干净。我建议您传递数据对象。这将使您的函数更易于移植和测试
您的示例是按值访问$student变量。您只需使用foreach()中的符号(&)通过引用访问它,就像这样foreach($data as&$student){…}
我注意到您从未调用过insertNumRandom()
为了更好地了解数组中的内容,我建议您为infrade()函数使用字段分隔符,并在数组内爆后打印新行。我打赌您会喜欢结果
最后,给你一个建议…array_push()允许你在一个数组中推送一个或多个项目。但我个人只在需要在一个数组中推送多个项目时才使用它。对于只推送项目新项目,我使用缩写$myArray[]=“new item”;
所以在实践中,以下是你的例子将成为
<?php
// Since you are not reusing $person1, $person2 and $person3 beyond the initialization
// phase, then let's keep things cleaner.
$data[] = [
'name' => 'person1',
'note' => []
];
$data[] = [
'name' => 'person2',
'note' => []
];
$data[] = [
'name' => 'person3',
'note' => []
];
/**
* In this function signature, the & means that the argument is passed by reference.
* That make it possible for the function to change the value of the argument.
*/
function insertNumRandom(&$theData) {
// The foreach() also needs to use $student by reference so that we can modify it
// by adding the random number to it.
foreach($theData as &$student) {
$student['note'][] = rand(0,10);
}
}
/**
* No need to pass $theData by reference because the function will not change its value.
*/
function showNotes($theData) {
// Here, the foreach() is using student by value because we do not need to change it's value.
foreach($theData as $student) {
echo implode($student['note'], ', ') . "\n";
}
}
// Inserting some random notes 3 times.
insertNumRandom($data);
insertNumRandom($data);
insertNumRandom($data);
showNotes($data); // To show the notes but it shows nothing.
?>
您如何调用insertNumRandom()
函数?为什么要将其存储在$\u会话中?我不介意只使用代码示例,但不清楚这里有什么不同,以及您为什么采用这种方法。您能解释更多问题吗?@progrock您能解释您的答案吗?(1)在insertNumRandom函数中,应使用$data=$\u SESSION['data']在SESSION中获取数据,然后使用&(&$student)将值重新分配给$data变量并将数据设置为SESSION(2)如果不使用&(&$student),则数组_push($student['note',rand(0.10))无法将数据分配给$data。(3)在showNotes函数中,要显示数据,我们应该从$_SESSION['data']获取数据。(4)最后,我们必须在$data变量中显示之前添加随机数。