在php中将数组转换为对象

发布于 2024-12-10 10:52:52 字数 708 浏览 1 评论 0原文

在 php 中,我将发布的数据从表单转换为如下所示的对象:

<?php

...some code...

    $post = new stdClass;

    foreach ($_POST as $key => $val)
        $post->$key = trim(strip_tags($_POST[$key]));

?>

然后在我的页面中,我只是回显这样的发布数据:

<?php echo $post->Name; ?>
<?php echo $post->Address; ?>

等等...

这工作正常,但我有多个作为组一部分的复选框,并且我回显结果其中,像这样:

<?php
  $colors = $_POST['color_type'];
  if(empty($colors))
  {
    echo("No color Type Selected.");
  }
  else
  {
    $N = count($colors);

     for($i=0; $i < $N; $i++)
    {
      echo($colors[$i] . ", ");
    }
  }
?>

当我只使用数组时,这是有效的,但是如何将其编写为对象语法?

In php I am converting posted data from a form to objects like this:

<?php

...some code...

    $post = new stdClass;

    foreach ($_POST as $key => $val)
        $post->$key = trim(strip_tags($_POST[$key]));

?>

Then in my page I just echo posted data like this :

<?php echo $post->Name; ?>
<?php echo $post->Address; ?>

etc...

This works fine but I have multiple checkboxes that are part of a group and I echo the results of that, like this:

<?php
  $colors = $_POST['color_type'];
  if(empty($colors))
  {
    echo("No color Type Selected.");
  }
  else
  {
    $N = count($colors);

     for($i=0; $i < $N; $i++)
    {
      echo($colors[$i] . ", ");
    }
  }
?>

That works when I am just using array, but how do I write this as object syntax?

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

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

发布评论

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

评论(3

提笔落墨 2024-12-17 10:52:52

的代码

function array_to_object($arr) {
    $post = new stdClass;
    foreach ($arr as $key => $val) {
        if(is_array($val)) {
            $post->$key = post_object($val);
        }else{
            $post->$key = trim(strip_tags($arr[$key]));
        }
    }
    return $post;
}

$post = array_to_object($_POST);

或更复杂的解决方案

function arrayToObject($array) {
    if(!is_array($array)) {
        return $array;
    }

    $object = new stdClass();
    if (is_array($array) && count($array) > 0) {
      foreach ($array as $name=>$value) {
         $name = strtolower(trim($name));
         if (!empty($name)) {
            $object->$name = arrayToObject($value);
         }
      }
      return $object;
    }
    else {
      return FALSE;
    }
}

使用http://www 中 .richardcastera.com/blog/php-convert-array-to-object-with-stdclass

using your code

function array_to_object($arr) {
    $post = new stdClass;
    foreach ($arr as $key => $val) {
        if(is_array($val)) {
            $post->$key = post_object($val);
        }else{
            $post->$key = trim(strip_tags($arr[$key]));
        }
    }
    return $post;
}

$post = array_to_object($_POST);

or more complex solution

function arrayToObject($array) {
    if(!is_array($array)) {
        return $array;
    }

    $object = new stdClass();
    if (is_array($array) && count($array) > 0) {
      foreach ($array as $name=>$value) {
         $name = strtolower(trim($name));
         if (!empty($name)) {
            $object->$name = arrayToObject($value);
         }
      }
      return $object;
    }
    else {
      return FALSE;
    }
}

from http://www.richardcastera.com/blog/php-convert-array-to-object-with-stdclass

地狱即天堂 2024-12-17 10:52:52

你为什么想要那个?数组有什么问题?

使用面向对象编程,这可能就是您正在寻找的。将其视为一个对象,通过创建一个名为 Color 的类并执行 $colors[$i] = new Color();

这样您就可以用它做任何您想做的事情,并为其添加功能。

why would you want that? What's wrong with an array?

Use Object Oriented Programming, which might be what you are looking for. Treat it as an object, by making a class called Color and doing $colors[$i] = new Color();

This way you can do whatever you want with it, and add functions to it.

柏林苍穹下 2024-12-17 10:52:52

非常简单 - 当您将 color_type 键附加到对象时,它将成为一个数组,该数组是对象的属性。这很可能是您想要的:您可能不想将该数组转换为它自己的基于 stdClass 的对象,因为这样您将无法迭代所有值(就像)。这是一个片段:

<?php
    // putting in both of these checks prevents you from throwing an E_WARNING
    // for a non-existent property. E_WARNINGs aren't dangerous, but it makes
    // your error messages cleaner when you don't have to wade through a bunch
    // of E_WARNINGS.
    if (!isset($post->color_type) || empty($post->color_type)) {
        echo 'No colour type selected.'; // apologies for the Canadian spelling!
    } else {
        // this loop does exactly the same thing as your loop, but it makes it a
        // bit more succinct -- you don't have to store the count of array values
        // in $N. Bit of syntax that speeds things up!
        foreach ($post->color_type as $thisColor) {
            echo $thisColor;
        }
    }
?>

希望这有帮助!当然,在现实生活中,您需要进行各种数据验证和清理 - 例如,您需要检查浏览器是否确实传递了 $_POST[ 的值数组'color_type'],并且您需要清理输出,以防有人试图将漏洞注入您的页面(通过 echo htmlspecialchars($thisColor); -- this将所有字符变成 < 和> 到 HTML 实体中,这样它们就无法插入 JavaScript 代码)。

Pretty simple -- when you attach the color_type key to your object, it'll become an array that's a property of your object. This is most likely what you want: you probably won't want to turn that array into its own stdClass-based object, because then you won't be able to iterate through all the values (as easily). Here's a snippet:

<?php
    // putting in both of these checks prevents you from throwing an E_WARNING
    // for a non-existent property. E_WARNINGs aren't dangerous, but it makes
    // your error messages cleaner when you don't have to wade through a bunch
    // of E_WARNINGS.
    if (!isset($post->color_type) || empty($post->color_type)) {
        echo 'No colour type selected.'; // apologies for the Canadian spelling!
    } else {
        // this loop does exactly the same thing as your loop, but it makes it a
        // bit more succinct -- you don't have to store the count of array values
        // in $N. Bit of syntax that speeds things up!
        foreach ($post->color_type as $thisColor) {
            echo $thisColor;
        }
    }
?>

Hope this helps! Of course, in a real-life setting, you'll want to do all sorts of data validation and cleaning -- for instance, you'll want to check that the browser actually passed an array of values for $_POST['color_type'], and you'll want to clean the output in case someone is trying to inject an exploit into your page (by going echo htmlspecialchars($thisColor); -- this turns all characters like < and > into HTML entities so they can't insert JavaScript code).

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