foreach - 循环对象并更改值 - php5

发布于 2024-10-07 11:56:47 字数 615 浏览 2 评论 0原文

我在 PHP 中有一个简单的小便清理函数,

它接受一个值或值数组并进行一些输入清理。现在我正在使用 mysqli,它将行作为对象获取,因此我需要能够将其应用于对象以及数组

function filter_out($output=''){
    if($output != ''){
        // i.e passed $_POST array
        if(is_array($output)){
            $newoutput = array();
            foreach($output as $outputname=>$outputval){
                $newoutput[$outputname] = stripslashes($outputval);
                $newoutput[$outputname] = htmlspecialchars($newoutput[$outputname]); 
            }
        } else if(is_object($input)){
            ?
        }
    }
}

有人能告诉我如何使用对象作为输入来执行相同的操作吗?

I have a simple wee cleansing function in PHP

It takes a value or array of values and does some input cleansing. Now I'm using mysqli which is fetching rows as objects so I need to be able to apply it to obejcts as well as arrays

function filter_out($output=''){
    if($output != ''){
        // i.e passed $_POST array
        if(is_array($output)){
            $newoutput = array();
            foreach($output as $outputname=>$outputval){
                $newoutput[$outputname] = stripslashes($outputval);
                $newoutput[$outputname] = htmlspecialchars($newoutput[$outputname]); 
            }
        } else if(is_object($input)){
            ?
        }
    }
}

Can anyone tell me how I can do the equivalent with object as input?

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

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

发布评论

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

评论(3

清风无影 2024-10-14 11:56:47

您要查找的函数是 get_object_vars

$vars = get_object_vars($input);
foreach ($vars as $outputname => $outputval) {
    ///...
}

不要尝试迭代对象本身(foreach ($object as $key => $value)),因为它并不总是能正常工作。有时会(例如 stdClass),有时不会(任何实现 Traversable...

编辑

就您的评论而言...只要类没有执行任何有趣的事情(__get__setprotectedprivate),你可以这样做:

$newoutput = clone $input; //make a copy to return
$vars = get_object_vars($input);
foreach ($vars as $outputname => $outputval) {
    $newoutput->$outputname = htmlspecialchars(stripslashes($outputval));
}

但我真的不能想想任何能够 100% 成功的方法...另一种选择是返回一个 nieve 对象 (stdclass) 而不是提交的对象:

$newoutput = new StdClass();
$vars = get_object_vars($input);
foreach ($vars as $outputname => $outputval) {
    $newoutput->$outputname = htmlspecialchars(stripslashes($outputval));
}

The function you're looking for is get_object_vars:

$vars = get_object_vars($input);
foreach ($vars as $outputname => $outputval) {
    ///...
}

Don't try iterating on the object itself (foreach ($object as $key => $value)), because it won't always work right. Sometimes it will (stdClass as an example), and sometimes it won't (any class implementing Traversable...

Edit

As far as your comment goes... As long as the classes aren't doing anything funny (__get or __set, protected or private), you could do:

$newoutput = clone $input; //make a copy to return
$vars = get_object_vars($input);
foreach ($vars as $outputname => $outputval) {
    $newoutput->$outputname = htmlspecialchars(stripslashes($outputval));
}

But I can't really think of any method that will work 100% of the time... The other option, would be to return a nieve object (stdclass) instead of the submitted one:

$newoutput = new StdClass();
$vars = get_object_vars($input);
foreach ($vars as $outputname => $outputval) {
    $newoutput->$outputname = htmlspecialchars(stripslashes($outputval));
}
凯凯我们等你回来 2024-10-14 11:56:47

回答OP对ircmaxell的回答的评论:

$vars = get_object_vars($input);
foreach ($vars as $outputname => $outputval) {
    $input->$outputname = htmlspecialchars(stripslashes($outputval));
}

To answer the OP's comment on ircmaxell's answer:

$vars = get_object_vars($input);
foreach ($vars as $outputname => $outputval) {
    $input->$outputname = htmlspecialchars(stripslashes($outputval));
}
浅沫记忆 2024-10-14 11:56:47

既然你提到来自 mysqli 的数组和对象,我猜测它们只是 stdClass,那么为什么不将对象转换为数组呢?

$newoutput = array()
foreach ((array) $output as $key => $value) {
  $newoutput[$key] = htmlspecialchars(stripslashes($value));
}

或者您可以就地执行:

$output = (array) $output;
foreach ($output as &$value) {
  $value = htmlspecialchars(stripslashes($value));
}

因此单个流程可能如下所示:

function filter_out($output=''){
  if($output != ''){
    // i.e passed $_POST array
    $is_array = is_array($output);
    $output = (array) $output;
    foreach($output as &$outputval){
      $outputval = htmlspecialchars(stripslashes($outputval)); 
    }
    if (!$is_array) {
      $output = (object) $output;
    }
  }
  return $output;
}

Since you mention arrays and object coming from mysqli I'm guessing that they're just stdClass so why not just cast the object to an array?

$newoutput = array()
foreach ((array) $output as $key => $value) {
  $newoutput[$key] = htmlspecialchars(stripslashes($value));
}

Or you could probably just do it in place:

$output = (array) $output;
foreach ($output as &$value) {
  $value = htmlspecialchars(stripslashes($value));
}

So a single flow could look like:

function filter_out($output=''){
  if($output != ''){
    // i.e passed $_POST array
    $is_array = is_array($output);
    $output = (array) $output;
    foreach($output as &$outputval){
      $outputval = htmlspecialchars(stripslashes($outputval)); 
    }
    if (!$is_array) {
      $output = (object) $output;
    }
  }
  return $output;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文