创建对象PHP的数组

时间:2018-07-11 09:05:47

标签: php

我正在尝试在PHP上创建此结构

enter image description here

,我不知道如何在PHP上创建对象数组。它总是从对象获取最后的数据。

这是我当前的代码:

FALSE

3 个答案:

答案 0 :(得分:0)

解决了实际问题,没有从getPrices函数接收所有记录:

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;
}

$price(正确:$price[])变量之后,您缺少了方括号,该变量告诉PHP追加到数组而不是实际替换它。

另一种选择是使用array_push,它虽然更明确,但功能相同。了解更多here

解决序列化问题:

您可以使用json_encode将数组序列化为JSON( J ava S cript O bject N < / strong>通知)。

答案 1 :(得分:0)

$dataArray=array(); 
 foreach($test as $t) {
      $dataArray[] = array('item'=> $t->id,'item'=> $t->price,'type'=> $t->type);
 }
 $finalArray= array('deposit'=>$dataArray);

使用json_encode

将最终数组转换为json

答案 2 :(得分:0)

$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>';

输出将是:

PHP结构

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字符串

{
    "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"
        }
    ]
}