簡體   English   中英

多維數組手動推送PHP Codeigniter

[英]Multidimensional array push manually php codeigniter

Array(    

    [0] => stdClass Object
    (
        [id] => 1
        [user_id] => 1
        [following_id] => 2
        [type] => user
        [created_on] => 2014-03-01 04:43:57
    )

   [1] => stdClass Object
    (
        [id] => 15
        [user_id] => 1
        [following_id] => 4
        [type] => user
        [created_on] => 2014-05-27 11:55:17
    )

  )

嗨,朋友們,我有一個需要手動創建下一個stdclass Object的產品數組。這些數組是通過推入值生成的。

我現在試圖解決這個問題已經超過一個小時,但我無法正常工作。 我知道這應該很容易...但是無論如何-我不明白:D

我已經工作了一個小時來手動創建下一個stdclass對象。 我要這樣 我如何創建這個。

有人能幫忙嗎?

在此先感謝,J

陣列(

    [0] => stdClass Object
    (
        [id] => 1
        [user_id] => 1
        [following_id] => 2
        [type] => user
        [created_on] => 2014-03-01 04:43:57
    )

   [1] => stdClass Object
    (
        [id] => 15
        [user_id] => 1
        [following_id] => 4
        [type] => user
        [created_on] => 2014-05-27 11:55:17
     )
   [2] => stdClass Object
    (
        [id] => 16
        [user_id] => 1
        [following_id] => 5
        [type] => user
        [created_on] => 2014-05-27 11:55:17
    )

  )

一種實現方法是使用如下函數:

function create($id, $user_id, $following_id, $type, $created_on){
    $obj = new stdClass;
    $obj->id = $id;
    $obj->user_id = $user_id;
    $obj->following_id = $following_id;
    $obj->type = $type;
    $obj->created_on = $created_on;
    return $obj; 
}

並假設您的數組是$array ,您可以像這樣向其中添加項目:

$array[] = create(16, 1, 5, 'user', '2014-05-27 11:55:17');

希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM