php - 将对象的 protected 成员的数据复制到数组中的最佳方法

标签 php

* 在确定下划线问题后编辑了我的问题。
非常感谢帮助诊断问题的 choz*

* 描述 * 我有一个类(class),有两个成员分别持有相册数据和照片数据。 在构造函数中,我用从数据库查询中提取的数据填充它们。 这些查询返回一个带有数组的对象,该数组是对象的 protected 成员。

我试图将一个新的键和值添加到包含关联数组的对象中,错误地将其视为一个单纯的数组。 未设置这些新键和值。大概是因为 Object 的数组成员是 protected 。 请参阅下面的 var dump,其中 Object dump 以: [“数据”: protected ]。 这是问题吗?如果是这样,创建我自己可以自由操作的副本的最佳方法是什么?

/****** 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;     

这是用来添加key和value的方法——

/*
* 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($this->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($this->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,在阅读了所有评论并对您正在使用的 Model 进行了基本搜索后,您的对象支持 toArray() 函数,该函数将返回数组中的数据没有 protected 成员的格式。

这是引用: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 - 将对象的 protected 成员的数据复制到数组中的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34463386/

相关文章:

php - 使用 php 发送文件而不使用 POST 但使用 type=file

php - 如何使用 Google OAuth 获取 Google 通讯录信息?

php - 从YouTube网址获取视频ID的模式

php - 在 Netbeans PHP 中定义变量类型

php - 无法从 mysql 数据库中获取 BLOB 类型图像

php - WSL2 Ubuntu 20.04、Docker 和 Laravel 启动本地主机不会显示在浏览器上

php - 左连接 2 列

javascript - Ajax表单$_POST返回空白数据

php - UPDATE 中的 CASE 用于选择列

php - 错误的月份(二月)- DateTime::createFromFormat