内爆对象数组中给定名称的所有属性 - PHP

发布于 2024-10-31 03:07:07 字数 332 浏览 1 评论 0原文

有没有办法使数组中包含的相似对象的值内爆?我有一个对象数组:

$this->inObjs

并且我想要每个 messageID 属性的逗号分隔字符串:

$this->inObjs[$i]->messageID

是否有一种优雅的方法来执行此操作,或者我必须 MacGyver< /code> 使用 get_object_varsforeachs 或类似的解决方案?感谢您的帮助。

Is there a way to implode the values of similar objects contained in an array? I have an array of objects:

$this->inObjs

and I'd like a comma separated string of each of their messageID properties:

$this->inObjs[$i]->messageID

Is there an elegant way to do this or am I going to have to MacGyver a solution with get_object_vars or foreachs or something similar? Thanks for the help.

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

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

发布评论

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

评论(6

天邊彩虹 2024-11-07 03:07:07
$allMessageID = '';
foreach ($this->inObjs as $objectDetail) :
    $allMessageID[] = $objectDetail->messageID;
endforeach;

$allMessageID_implode = implode(",", $allMessageID);

echo $allMessageID_implode;
$allMessageID = '';
foreach ($this->inObjs as $objectDetail) :
    $allMessageID[] = $objectDetail->messageID;
endforeach;

$allMessageID_implode = implode(",", $allMessageID);

echo $allMessageID_implode;
牵强ㄟ 2024-11-07 03:07:07

如果可以修改类,则可以实现 __toString:

class MyObject {
    private $messageID = 'Hello';
    public function __toString() {
        return $this->messageID;
    }
}
// ...
$objectList = array(new MyObject, new MyObject);
echo implode(',', $objectList);
// Output: Hello,Hello

If you can modify the class, you can implement __toString:

class MyObject {
    private $messageID = 'Hello';
    public function __toString() {
        return $this->messageID;
    }
}
// ...
$objectList = array(new MyObject, new MyObject);
echo implode(',', $objectList);
// Output: Hello,Hello
扛刀软妹 2024-11-07 03:07:07

我发现最简单的方法是使用 array_map

$messageIDs = array_map( function($yourObject) { return $yourObject->messageID; }, $this->inObjs );
$string = implode(", ", $messageIDs );

The easiest way that I found is using array_map

$messageIDs = array_map( function($yourObject) { return $yourObject->messageID; }, $this->inObjs );
$string = implode(", ", $messageIDs );
颜漓半夏 2024-11-07 03:07:07
$messageIDArray;
foreach($this->inObjs as $obj){
   $messageIDArray[] = $obj->messageID;
}

$string = implode(',',$messageIDArray);
$messageIDArray;
foreach($this->inObjs as $obj){
   $messageIDArray[] = $obj->messageID;
}

$string = implode(',',$messageIDArray);
昵称有卵用 2024-11-07 03:07:07

我通常会针对这种情况制作一个Helper,并像这样使用它


function GetProperties(array $arrOfObjects, $objectName) {
     $arrProperties = array();
     foreach ($arrOfObjects as $obj) {
         if ($obj->$objectName) {
              $arrProperties[] = $obj->$objectName;
         }
     }
     return $arrProperties;
}

I usually make a Helper for this situation, and use it like this


function GetProperties(array $arrOfObjects, $objectName) {
     $arrProperties = array();
     foreach ($arrOfObjects as $obj) {
         if ($obj->$objectName) {
              $arrProperties[] = $obj->$objectName;
         }
     }
     return $arrProperties;
}

山人契 2024-11-07 03:07:07

这是一个两行:

array_walk($result, create_function('&$v', '$v = $v->property;'));
$result = implode(',', $result);

或者:

array_walk($result, function(&$v, &$k) use (&$result) { $v = $v->name; } );
$result = implode(',', $result);

其中 $v->property 是要内爆的对象属性名称。

另请参阅 array_map()

Here is a two liner:

array_walk($result, create_function('&$v', '$v = $v->property;'));
$result = implode(',', $result);

Or:

array_walk($result, function(&$v, &$k) use (&$result) { $v = $v->name; } );
$result = implode(',', $result);

Where $v->property is your object property name to implode.

Also see array_map().

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