PHP 通过引用传递问题 - 无法更改类型?

发布于 2024-12-09 20:41:59 字数 700 浏览 0 评论 0原文

我有一个奇怪的现象。我希望有人能向我解释那里发生了什么: 我想创建一个过滤器。原点类似于“-10”或“10-20”或“20+”(字符串类型),结果应为“低于 10 美元”,...以及“product_price <<”。 10', ... 对于 sql 命令。

但是将数组存储回原始字符串是行不通的。它只是提供“$Array”作为结果。难道不能通过引用传递并改变类型吗?

感谢您的知识!

    foreach($filters as &$filter){
        preg_match ('#^\-(\d+)$#ism', $filter, $match);
        if ($match[1]){
            $filter = array(
                'Under $'.intval($match[1]), 
                'product_price < '.intval($match[1])
            );
        }
        ...
    } 
    return $filtering;
}

PS:我不是在寻找解决方案,因为我可以将原始字符串更改为数组,或者我可以将 foreach 更改为 按值传递 并创建一个新的带有 $newFilter[] = ... 之类的数组的数组我只是好奇

I have a weird phenomenon. I hope someone can explain to me what is happening there:
I want to create a filter. The origin is something like '-10' or '10-20' or '20+' (type string) and the result should be 'Under $10', ... as well as 'product_price < 10', ... for a sql command.

But storing the array back on the original string doesn't work. It just delivers '$Array' as result. Is it not possible to pass by reference and change the type?

Thanks for your knowledge!

    foreach($filters as &$filter){
        preg_match ('#^\-(\d+)$#ism', $filter, $match);
        if ($match[1]){
            $filter = array(
                'Under 

P.S.: I am not looking for a solution, because I could change the origin string into array, or I could change the foreach in to a pass by value and create a new array with the arrays like $newFilter[] = ... I am only curious

.intval($match[1]), 'product_price < '.intval($match[1]) ); } ... } return $filtering; }

P.S.: I am not looking for a solution, because I could change the origin string into array, or I could change the foreach in to a pass by value and create a new array with the arrays like $newFilter[] = ... I am only curious

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

勿忘初心 2024-12-16 20:41:59

您可以更改它的类型。构造证明:

<?php
header('Content-type:text/plain');

        $arr = array(
                '1',
                '2',
        );

        foreach ($arr as &$filter) {
                $filter = array($filter);
        }

        print_r($arr);
?>

打印:

Array
(
    [0] => Array
        (
            [0] => 1
        )

    [1] => Array
        (
            [0] => 2
        )

)

You can change it's type. Proof by construction:

<?php
header('Content-type:text/plain');

        $arr = array(
                '1',
                '2',
        );

        foreach ($arr as &$filter) {
                $filter = array($filter);
        }

        print_r($arr);
?>

Prints:

Array
(
    [0] => Array
        (
            [0] => 1
        )

    [1] => Array
        (
            [0] => 2
        )

)
灯角 2024-12-16 20:41:59

您应该将 foreach 更改为

foreach($filters as $index => $filter)

并更新您的过滤器,

$filters[$index] = array(...);

我相信 foreach() 语句创建的 $filter 变量是数组中数据的副本,而不是对其的引用。

You should change your foreach into

foreach($filters as $index => $filter)

and update your filter by doing

$filters[$index] = array(...);

I believe the $filter variable created by the foreach() statement is a copy of the data in the array and not a reference to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文