[英]PHP - What is the best way to access a property of an object which is an array element
I have an object which is an array of JSON objects. 我有一个对象,它是JSON对象的数组。 Kinda Like this,
有点像这样
$object = [
{
"id":1,
"name":"blue",
"order":4
},
{
"id":2,
"name":"green",
"order":6
},
{
"id":3,
"name":"yellow",
"order":2
}
]
I wanted to access the properties in a simple way and a single line maybe like this, 我想以一种简单的方式访问这些属性,也许一行这样,
Say if I wanted the "order" of the object with name "blue"
说我是否想要名称为“ blue”的对象的“ order”
$blue_order = $object[something]->[name="blue"]->order;
$ blue_order = $ object [something]-> [name =“ blue”]-> order;
Kinda mixed Jquery in this. Kinda在此混合了Jquery。 But I hope you understand.
但我希望你能理解。 Right now the best I've got is this,
现在我所能拥有的最好的是
for($i=0; $i<count($object); $i++){
if($object[$i]->name == "blue"){
$blue_order = $object[$i]->order;
}
}
This seems very inefficient though and I don't want to use a loop because the array is very large and looping through it will be very slow. 但是,这似乎效率很低,我也不想使用循环,因为数组很大,循环遍历非常慢。 So how do I do this?
那么我该怎么做呢?
I used a "for" loop instead of foreach because the array can be null.
我使用“ for”循环而不是foreach,因为该数组可以为null。 And also the order of the array elements will not always be the same.
而且,数组元素的顺序也不总是相同的。
So I can't do something like 所以我不能做这样的事情
$object[0]->order
You could still use foreach
but check if the array isn't empty first, like this: 您仍然可以使用
foreach
但首先检查数组是否不为空,如下所示:
if(!empty($object)){
foreach($object as $element){
if($element->name == "blue"){
$blue_order = $element->order;
}
}
}
Also, your code looks efficient to me, what I would probably add is a break
after you find the value, like this: 另外,您的代码对我来说看起来很高效,我可能会添加一个找到值的
break
,如下所示:
if(!empty($object)){
foreach($object as $element){
if($element->name == "blue"){
$blue_order = $element->order;
break;
}
}
}
If your object has a lot of information and you're going to do a lot of searches, then you could do some pre-processing so it get easier to search, like this: 如果您的对象具有大量信息,并且您将要进行大量搜索,那么您可以进行一些预处理,以便于搜索,例如:
$object_by_name = array();
if(!empty($object)){
foreach($object as $element){
$object_by_name[$element->name] = $element;
}
}
Then, you could search it like this: 然后,您可以像这样搜索它:
if(!empty($object_by_name['blue'])){
$blue_order = $object_by_name['blue']->order
}
Keep it simple. 把事情简单化。
you're writing way too much code. 您正在编写太多代码。
$array_list = ($array_list)?:[];
$array_list = array_filter($array_list,function($var) {
return ($var->name=="blue");
});
$order = ($array_list)? $array_list[0]->order :'';
Happy coding 快乐编码
<?php
$arr = array(
array('id'=>1,'name'=>'blue','order'=>4),
array('id'=>2,'name'=>'green','order'=>6),
array('id'=>3,'name'=>'yellow','order'=>2),
);
// $json is JSON version of the arrays in $arr
$json = json_encode($arr);
// show $json
echo $json . "\n";
// create arrays from JSON so it can be used in PHP easier
$obj = json_decode($json);
$color = 'blue';
$blue_order = array_filter($obj, function($i) use ($color) { return $i->name == $color; })[0]->order;
echo "Blue Order: " . $blue_order;
You may be able to use array_filter to help this become a one-liner. 您可能可以使用array_filter使其成为单行。 I included the json_decode and json_encode so I could have a complete example.
我包括了json_decode和json_encode,所以我可以有一个完整的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.