php – 将Object的受保护成员的数据复制到数组中的最佳方法

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 将Object的受保护成员的数据复制到数组中的最佳方法脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
*在确定强调问题后编辑了我的问题.
非常感谢帮助诊断问题的choz *

*描述*
我有一个有两个成员的班级,相应地保存专辑数据和照片数据.
在构造函数中,我使用从DB查询提取的数据填充它们.
这些查询返回一个Object,该数组是Object的受保护成员.

我试图在Object中添加一个新的键和值,并将一个关联数组错误地视为一个数组.
这些新的关键和价值没有设定.大概是因为Object的数组成员受到保护.
请参阅下面的VAR dump,其中Object转储的开头位置为:
[ “数据”:保护].
这是问题吗?如果是这样,创建我自己可以自由操作的副本的最佳方法是什么

/****** Class  Memebers ********/ 
//Object wITh array of associative album arrays.
//each inner album array is defined by unique key ='albumID'.
PRivate $member_albums; 


//array of objects holding associative photo arrays.
//Each inner photo array is defined by unique key ='albumID'.
private $member_photos;

这是用于添加键和值的方法

/*
* For each photo in each album,we will add a new key and value.  
* key = 'img_url',value is an array with structure of 
* ['S']['{url pointing to this photo in storage}'] where 'S' is the 
* key (representing a string value) and value is the url string.  
*/
private function add_Photo_Url_To_Photos_Data_Array(){

  // If data was retrieved From DB without errors
  // because query fail returns false.
  if( $this->;member_albums && $this->member_photos){

    // If user has albums - because 'Count' is always 0 or
    // greater,representing the number of inner arrays
    if($this->member_albums['Count'] > 0 ){ 

        // For number of albums
        foreach ($this->member_albums['Items'] as $albumArr) {

            // current album ID 
            $curr_AlbumId = $albumArr['albumID']['S'];                  

            // If current album contains photos
            if( $this->member_photos[$curr_AlbumId]['Count'] > 0){                                              

                // For each photo in this album
                foreach( $this->member_photos[$curr_AlbumId]['Items'] as &$photosArr) {

                    // method that creates a url from photo data- returns string url                                                                                    
                    $url = $this->build_Photo_Url($photosArr,$curr_AlbumId);                                                       

                    // Insert new key = 'img_url' and value into the array.                                                                                 
                    // new value is itself an array with key ['S']  
                    // signifying a string and value $url   
                    $photosArr['img_url'] = array('S' =>
                    $url);

                    // this echo shows key and value were added
                    // correctly into $photosArr
                    echo ' <br>Img URL = ' . $photosArr['img_url']['S'];                                                            
                }                   
            }                                   
        }
    }
}

但是如果我尝试从$this-> member_photos数组项回显’img_url’,它就不存在了.

谢谢.

***编辑3 ****
添加var_dump($this-> member_albums)和var_dump($this-> member_photos)

的var_dump($这 – > member_albums)

object(Guzzle\Service\Resource\Model)#88 (2) { 
["structure":protected]=> NULL ["data":protected]=> array(3) { 
    ["Count"]=> int(2) 
    ["Items"]=> array(2) { 
        [0]=> array(7) { 
            ["PlaceLong"]=> array(1) { ["N"]=> string(1) "0" } 
            ["AlbumId"]=> array(1) { ["S"]=> string(7) "album_1" } 
            ["Title"]=> array(1) { ["S"]=> string(5) "test1" } 
            ["UserId"]=> array(1) { ["S"]=> string(36) "810725E5-D235-43F7-AA50-4CCDACD6AB36" } 
            ["Year"]=> array(1) { ["N"]=> string(1) "0" } 
            ["PlaceLat"]=> array(1) { ["N"]=> string(1) "0" } 
            ["Timestamp"]=> array(1) { ["N"]=> string(16) "472572846.470462" } 
        }

的var_dump($这 – > member_photos)

array(2) { 
["album_1"]=> object(Guzzle\Service\Resource\Model)#89 (2) { 
["structure":protected]=> NULL ["data":protected]=> array(3) { 
    ["Count"]=> int(5) 
    ["Items"]=> array(5) { 
        [0]=> array(8) { 
            ["ShotId"]=> array(1) { ["S"]=> string(6) "Photo2" } 
            ["AlbumId"]=> array(1) { ["S"]=> string(7) "album_1" } 
            ["UserId"]=> array(1) { ["S"]=> string(16) "1111-11111-11111" } 
            ["Year"]=> array(1) { ["N"]=> string(1) "0" } 
            ["Discovery"]=> array(1) { ["N"]=> string(1) "0" } 
            ["Timpstamp"]=> array(1) { ["N"]=> string(14) "472572840.3845" } 
            ["ExtractedPhotoId"]=> array(1) { ["S"]=> string(32) "Photo2_0-0-746-0-0-1000-746-1000" } 
            ["ManualEdit"]=> array(1) { ["N"]=> string(1) "0" } 
        }

解决方法

Jay,在阅读了所有注释并对您正在使用的模型进行基本搜索之后,您的对象支持toArray()函数,该函数将以没有受保护成员的数组格式返回数据.

这是参考:http://docs.aws.amazon.com/aws-sdk-php/v2/guide/feature-models.html

首先,尝试运行var_dump(get_class_methods($this-> member_albums)));看看它将返回什么样的方法.我确信有一个内置的toArray()函数,主要是因为它是一个响应,所以它们可以让你轻松提取和操作你的数据.

首先尝试var_dump(),然后尝试执行以下$clearArr = $this-> member_albums-> toArray();和print_r / var_dump $clearArr,它应该导致标准的多维数组.

脚本宝典总结

以上是脚本宝典为你收集整理的php – 将Object的受保护成员的数据复制到数组中的最佳方法全部内容,希望文章能够帮你解决php – 将Object的受保护成员的数据复制到数组中的最佳方法所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。