dongre6227 2013-07-18 13:41
浏览 1178

如何在php中创建一个对象数组

I want to clean my old_array and split it up to array of objects. So the new_array contains string and integer.

This is my old_array named $test_temp_variable which is only in string.

discount_org: [
[
"{"day":"00:00"",
""time":"08:00"",
""discount":"10:00""
],
[
""day":"00:00"",
""time":"14:00"",
""discount":"10:00""
]

This is my desired output

discount_org: [
{
day: 0,
time: 8,
discount: 10
},
{
day: 0,
time: 14,
discount: 10
}

but is this even possible in PHP? I have tried in several object-oriented features, but always end up not working. If yes, can you give a finger point?

var dump of my old array

array(7) {
  [0]=>
  array(3) {
    [0]=>
    string(13) "{"day":"8:00""
    [1]=>
    string(14) ""time":"12:00""
    [2]=>
    string(15) ""discount":"10""
  }
  [1]=>
  array(3) {
    [0]=>
    string(12) ""day":"8:00""
    [1]=>
    string(14) ""time":"12:00""
    [2]=>
    string(15) ""discount":"10""
  }
  • 写回答

3条回答 默认 最新

  • douju2331 2013-07-18 13:47
    关注

    If i do understand you correctly you have to make an object for each array-index you want to have, like:

    class Obj {
        public $Int;
        public $String;
    
        public function setInt($Int) {
            $this->Int = $Int
        }
        public function setString($String) {
            $this->String= $String
        }
    }
    

    then make some objects out of it, like:

    $Test = new Obj();
    $Test->setInt(3);
    

    and add them to your array like:

    $TestArray[] = $Test;
    

    You can now access them like:

    $TestArray[0]->Int;
    

    Is that what you mean?

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄