在php中将数组转换为对象
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
的代码
或更复杂的解决方案
使用http://www 中 .richardcastera.com/blog/php-convert-array-to-object-with-stdclass
using your code
or more complex solution
from http://www.richardcastera.com/blog/php-convert-array-to-object-with-stdclass
你为什么想要那个?数组有什么问题?
使用面向对象编程,这可能就是您正在寻找的。将其视为一个对象,通过创建一个名为
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.
非常简单 - 当您将 color_type 键附加到对象时,它将成为一个数组,该数组是对象的属性。这很可能是您想要的:您可能不想将该数组转换为它自己的基于 stdClass 的对象,因为这样您将无法迭代所有值(就像)。这是一个片段:
希望这有帮助!当然,在现实生活中,您需要进行各种数据验证和清理 - 例如,您需要检查浏览器是否确实传递了
$_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 ownstdClass
-based object, because then you won't be able to iterate through all the values (as easily). Here's a snippet: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 goingecho htmlspecialchars($thisColor);
-- this turns all characters like < and > into HTML entities so they can't insert JavaScript code).