Array.prototype.find() 在 PHP 中 returns 引用
Array.prototype.find() in PHP that returns reference
我正在尝试在 javascript 中创建一个类似于 Array.prototype.find() 的函数,但对于 PHP.
我的数组有这样的结构:
$array = [
["id" => 54, "type" => 5, "content" => [
["id" => 99, "type" => 516],
["id" => 88, "type" => 464],
["id" => 41, "type" => 845]]
],
["id" => 54, "type" => 55, "content"=> [
["id" => 54, "type" => 578],
["id" => 54, "type" => 354],
["id" => 75, "type" => 458]]
],
["id" => 65, "type" => 59, "content" => [
["id" => 87, "type" => 5454],
["id" => 65, "type" => 245],
["id" => 65, "type" => 24525]]
]
];
然后我创建了我的函数来像 javascript 中那样进行搜索:
function array_find($array, $function){
foreach($array as $value){
if($function($value)){
return $value;
}
}
}
$id=54;
$type=55;
$mycontent = array_find(
$array,
function($foo) {
global $id;
global $type;
return $foo["id"] == $id && $foo["type"] == $type;
}
)["content"];
它正在工作。但是我希望返回的结果是一个引用而不是一个值,所以我稍后可以在返回的数组中add/remove个元素。
我该怎么做?
我会尝试在不完全放弃您的原始代码的情况下回答这个问题。
如果你想return引用一个符合条件的行,那么我能想到的唯一方法就是
- 将您的数组数组转换为对象数组,
- 在函数名声明中添加
&
,并且
- 使用
= &
在全局范围内声明引用变量。
调整后的代码:(Demo)
function &getRowReference($array, $fn) {
foreach ($array as $row) {
if ($fn($row)) {
return $row->content;
}
}
}
function qualifyingRow($row) {
global $id;
global $type;
return $row->id == $id && $row->type == $type;
}
$id = 54;
$type = 55;
$array = [
(object)[
"id" => 54,
"type" => 5,
"content" => [
["id" => 99, "type" => 516],
["id" => 88, "type" => 464],
["id" => 41, "type" => 845]
]
],
(object)[
"id" => 54,
"type" => 55,
"content" => [
["id" => 54, "type" => 578],
["id" => 54, "type" => 354],
["id" => 75, "type" => 458]
]
],
(object)[
"id" => 65,
"type" => 59,
"content" => [
["id" => 87, "type" => 5454],
["id" => 65, "type" => 245],
["id" => 65, "type" => 24525]
]
]
];
$ref = &getRowReference($array, 'qualifyingRow');
$ref = 'this has been changed';
var_export($array);
我认为我自己不会费心去做所有这些努力。我可能会将数据结构保留在其原始状态,return 符合条件的行的索引,然后使用索引访问、改变和存储数据。
这是一个相关页面,提供一些关于 returning 参考的额外评论和警告:Return by reference in PHP
我正在尝试在 javascript 中创建一个类似于 Array.prototype.find() 的函数,但对于 PHP.
我的数组有这样的结构:
$array = [
["id" => 54, "type" => 5, "content" => [
["id" => 99, "type" => 516],
["id" => 88, "type" => 464],
["id" => 41, "type" => 845]]
],
["id" => 54, "type" => 55, "content"=> [
["id" => 54, "type" => 578],
["id" => 54, "type" => 354],
["id" => 75, "type" => 458]]
],
["id" => 65, "type" => 59, "content" => [
["id" => 87, "type" => 5454],
["id" => 65, "type" => 245],
["id" => 65, "type" => 24525]]
]
];
然后我创建了我的函数来像 javascript 中那样进行搜索:
function array_find($array, $function){
foreach($array as $value){
if($function($value)){
return $value;
}
}
}
$id=54;
$type=55;
$mycontent = array_find(
$array,
function($foo) {
global $id;
global $type;
return $foo["id"] == $id && $foo["type"] == $type;
}
)["content"];
它正在工作。但是我希望返回的结果是一个引用而不是一个值,所以我稍后可以在返回的数组中add/remove个元素。
我该怎么做?
我会尝试在不完全放弃您的原始代码的情况下回答这个问题。
如果你想return引用一个符合条件的行,那么我能想到的唯一方法就是
- 将您的数组数组转换为对象数组,
- 在函数名声明中添加
&
,并且 - 使用
= &
在全局范围内声明引用变量。
调整后的代码:(Demo)
function &getRowReference($array, $fn) {
foreach ($array as $row) {
if ($fn($row)) {
return $row->content;
}
}
}
function qualifyingRow($row) {
global $id;
global $type;
return $row->id == $id && $row->type == $type;
}
$id = 54;
$type = 55;
$array = [
(object)[
"id" => 54,
"type" => 5,
"content" => [
["id" => 99, "type" => 516],
["id" => 88, "type" => 464],
["id" => 41, "type" => 845]
]
],
(object)[
"id" => 54,
"type" => 55,
"content" => [
["id" => 54, "type" => 578],
["id" => 54, "type" => 354],
["id" => 75, "type" => 458]
]
],
(object)[
"id" => 65,
"type" => 59,
"content" => [
["id" => 87, "type" => 5454],
["id" => 65, "type" => 245],
["id" => 65, "type" => 24525]
]
]
];
$ref = &getRowReference($array, 'qualifyingRow');
$ref = 'this has been changed';
var_export($array);
我认为我自己不会费心去做所有这些努力。我可能会将数据结构保留在其原始状态,return 符合条件的行的索引,然后使用索引访问、改变和存储数据。
这是一个相关页面,提供一些关于 returning 参考的额外评论和警告:Return by reference in PHP