dsai1991
2018-07-11 09:05
浏览 158
已采纳

创建一个对象PHP数组

I am trying to create this structure on PHP

enter image description here

and I don't know how to I create an array of object on PHP. It always get the last data from the object.

This is my current code:

array(
     "description": getDescription($id),
     "deposit": getPrices($id);
)


function getPrices($id) {
    $test = Prices::where('price_id',$id)->where('promo',0)->get();
    $price = [];
    $data = [];

    foreach($test as $t) {
        $data["item_id"] = $t->id;
        $data["price"] = $t->list;
        $data["type"] = $t->type;

        $price = $data;
    }

    return $price;
}
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duandou9931 2018-07-11 09:11
    已采纳

    Fixing the actual issue with not receiving all records from the getPrices function:

    array(
         "description": getDescription($id),
         "deposit": getPrices($id);
    )
    
    
    function getPrices($id) {
        $test = Prices::where('price_id',$id)->where('promo',0)->get();
        $price = [];        
    
        foreach($test as $t) {            
            $price[] = ["item_id" => $t->id, "price" => $t->list, "type": $t->type];
        }
    
        return $price;
    }
    

    You're missing the square brackets after $price (Correct: $price[]) variable which tells PHP to append to array rather than actually replace it.

    The other option is to use array_push, which is more explicit but does the same; Read more here.

    Fixing the issue of serialisation:

    You can use json_encode to serialise the array into JSON (JavaScript Object Notation).

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douze2890241475 2018-07-11 09:11
    $dataArray=array(); 
     foreach($test as $t) {
          $dataArray[] = array('item'=> $t->id,'item'=> $t->price,'type'=> $t->type);
     }
     $finalArray= array('deposit'=>$dataArray);
    

    convert final array to json using json_encode

    评论
    解决 无用
    打赏 举报
  • dongyirong3564 2018-07-11 09:18
    $structure = new stdClass();
    
    $structure->description = 'tobedefined'; // Define the description here
    $structure->deposit = getPrices($id);
    
    function getPrices($id) {
        $deposits = array();
    
        // Change this to contain the real data
        // $test = Prices::where('price_id',$id)->where('promo',0)->get();
        $test = array(
            array(
                'item_id' => 100,
                'price' => '1, 2, 3, 4, 5',
                'type' => 'child'
            ),
            array(
                'item_id' => 101,
                'price' => '2, 4, 6, 8, 10',
                'type' => 'child'
            ),
            array(
                'item_id' => 102,
                'price' => '2, 4, 6, 8, 10',
                'type' => 'child'
            )
        );
    
        foreach ($test as $t) {
            $deposit = new stdClass();
    
            $deposit->item_id = $t['item_id'];
            $deposit->price = $t['price'];
            $deposit->type = $t['type'];
    
            $deposits[] = $deposit;
        }
        return $deposits;
    }
    
    echo 'PHP Structure';
    echo '<pre>';
    print_r($structure);
    echo '</pre>';
    
    echo 'JSON string';
    echo '<pre>';
    print_r(json_encode($structure, JSON_PRETTY_PRINT));
    echo '</pre>';
    

    Output will be:

    PHP Structure

    stdClass Object
    (
        [description] => tobedefined
        [deposit] => Array
            (
                [0] => stdClass Object
                    (
                        [item_id] => 100
                        [price] => 1, 2, 3, 4, 5
                        [type] => child
                    )
    
                [1] => stdClass Object
                    (
                        [item_id] => 101
                        [price] => 2, 4, 6, 8, 10
                        [type] => child
                    )
    
                [2] => stdClass Object
                    (
                        [item_id] => 102
                        [price] => 2, 4, 6, 8, 10
                        [type] => child
                    )
    
            )
    
    )
    

    JSON string

    {
        "description": "tobedefined",
        "deposit": [
            {
                "item_id": 100,
                "price": "1, 2, 3, 4, 5",
                "type": "child"
            },
            {
                "item_id": 101,
                "price": "2, 4, 6, 8, 10",
                "type": "child"
            },
            {
                "item_id": 102,
                "price": "2, 4, 6, 8, 10",
                "type": "child"
            }
        ]
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题