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?

    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码