我正在尝试将带有array_push的随机数添加到'notes'=> []字段中的数组,但我认为这样做不正确,因为我看不到数组中存储的随机数。 / p>
我通过以下方式进行操作:
$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.
答案 0 :(得分:1)
您应按如下所示更改insertNumRandom
function insertNumRandom(){
$data = $_SESSION['data'];
foreach ( $data as &$student ) {
array_push($student['note'], rand(0,10));
}
$_SESSION['data'] = $data;
}
showNotes函数
function showNotes(){
$data = $_SESSION['data'];
foreach ( $data as $student ) {
echo implode($student['note']);
}
}
并在showNotes之前调用insertNumRandom。
insertNumRandom();
showNotes();
答案 1 :(得分:0)
您正在使用$ as关键字引用$ data来创建一个新对象。
您应该改为遍历现有对象。
var MyObj = function(x) {
this.x = x;
this.y = 5;
};
MyObj.prototype.example = function() {
// I want to return a new object which has access to this.x and this.y
var addTwo = function() {
return 2 + this.x
}
// For example:
return {
addTwo: addTwo.bind(this)
}
};
答案 2 :(得分:0)
也许可以尝试一下,仍然不确定如何调用insertNumRandom()
函数
function showNotes($arr){
foreach ( $arr as $student ) {
var_dump(implode($student['note']));
}
}
insertNumRandom();
showNotes($data);
答案 3 :(得分:0)
您的示例中有很多问题。
首先,使用$ _SESSION对象将一些值传递给函数的方式不是必需的,而且也不是很干净。您可以在函数内部使用global $data
,它们会突然从父作用域访问$ data变量。但这还不是很干净。我建议您通过引用函数传递数据对象。这将使您的功能更加可移植和可测试。
您的示例正在按值访问$ student变量。您所需要做的就是通过使用像foreach ( $data as &$student ) { ... }
我注意到您从未致电insertNumRandom()
为了更好地了解数组中的内容,建议您为implode()函数使用字段分隔符,并在对数组进行内插后打印新行。我敢打赌,您会喜欢结果的。
最后,仅需提一个建议... 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.
?>