如何在不指定索引号的情况下在 PHP 中构建对象数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3707722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to build arrays of objects in PHP without specifying an index number?
提问by dylanized
Here is a weird question. I am building an array of objects manually, like this:
这里有一个奇怪的问题。我正在手动构建一个对象数组,如下所示:
$pages_array[0]->slug = "index";
$pages_array[0]->title = "Site Index";
$pages_array[0]->template = "interior";
$pages_array[1]->slug = "a";
$pages_array[1]->title = "100% Wide (Layout A)";
$pages_array[1]->template = "interior";
$pages_array[2]->slug = "homepage";
$pages_array[2]->title = "Homepage";
$pages_array[2]->template = "homepage";
I like how clearcut this is, but because I have to specify the index number, I can't rearrange them easily. How can I do this without the index number? Related, what is a better way to do this?
我喜欢这是多么清晰,但是因为我必须指定索引号,所以我不能轻松地重新排列它们。没有索引号我怎么能做到这一点?相关,有什么更好的方法可以做到这一点?
I also tried writing this by making a class, and having each spot on the array be instances of that class. But since this is a configuration file, it was hard to read and know what argument went with what parameter. That's why I chose this old-school method above.
我还尝试通过创建一个类来编写它,并使数组上的每个点都是该类的实例。但是由于这是一个配置文件,因此很难阅读并知道哪个参数与哪个参数对应。这就是我选择上面这种老派方法的原因。
Any thoughts are much appreciated!
任何想法都非常感谢!
回答by user187291
This code
这段代码
$pages_array[1]->slug = "a";
is invalid anyways - you'll get a "strict" warning if you don't initialize the object properly. So you have to construct an object somehow - either with a constructor:
无论如何都是无效的 - 如果您没有正确初始化对象,您将收到“严格”警告。所以你必须以某种方式构造一个对象 - 或者使用构造函数:
$pages_array[] = new MyObject('index', 'title'....)
or using a stdclass cast
或使用 stdclass 演员表
$pages_array[] = (object) array('slug' => 'xxx', 'title' => 'etc')
回答by Samir Talwar
If you make them arrays with named keys rather than objects, you can do it like this:
如果使用命名键而不是对象使它们成为数组,则可以这样做:
$pages_array = array(
array(
'slug' => 'index',
'title' => 'Site Index',
'template' => 'interior'
),
array(
'slug' => 'a',
'title' => '100% Wide (Layout A)',
'template' => 'interior'
),
array(
'slug' => 'homepage',
'title' => 'Homepage',
'template' => 'homepage'
)
);
You can combine this with Fanis' solution and use the slugs as the keys if you like.
如果您愿意,您可以将其与 Fanis 的解决方案结合使用,并使用 slug 作为键。
回答by Aether
Assuming you're not setting any $pages_array
elements elsewhere, you can set a simple variable.
假设您没有$pages_array
在其他地方设置任何元素,您可以设置一个简单的变量。
$i = 0;
$pages_array[$i]->slug = "index";
$pages_array[$i]->title = "Site Index";
$pages_array[$i]->template = "interior";
$i++;
$pages_array[$i]->slug = "a";
$pages_array[$i]->title = "100% Wide (Layout A)";
$pages_array[$i]->template = "interior";
$i++;
$pages_array[$i]->slug = "homepage";
$pages_array[$i]->title = "Homepage";
$pages_array[$i]->template = "homepage";
$i++;
You just have to remember to increment $i
every time so you don't overwrite an element.
你只需要记住$i
每次递增,这样你就不会覆盖一个元素。
回答by Fanis Hatzidakis
Assuming the slug will be unique, perhaps use it to identify your page arrays:
假设 slug 是唯一的,也许可以用它来识别您的页面数组:
$pages_array = array() ;
$pages_array['index'] = array() ;
$pages_array['index']['title'] = 'Site Index' ;
$pages_array['index']['template'] = 'interior' ;
$pages_array['a'] = array() ;
$pages_array['a']['title'] = '100% Wide (Layout A)' ;
$pages_array['a']['template'] = 'interior' ;
etc
等等
You'll just need to use the array key to get your slug value.
您只需要使用数组键来获取您的 slug 值。
回答by Naveed
If you want to maintain sequence indexes. May be this will help.
如果要维护序列索引。可能这会有所帮助。
$pages_array[]->slug = "index";
$key = array_search(end($pages_array),$pages_array );
$pages_array[$key]->title = "Site Index";
$pages_array[$key]->template = "interior";
$pages_array[]->slug = "a";
$key = array_search(end($pages_array),$pages_array );
$pages_array[$key]->title = "100% Wide (Layout A)";
$pages_array[$key]->template = "interior";
$pages_array[]->slug = "homepage";
$key = array_search(end($pages_array),$pages_array );
$pages_array[$key]->title = "Homepage";
$pages_array[$key]->template = "homepage";