将新对象插入对象树。怎么样?

时间:2020-02-28 04:24:57

标签: php

我刚完成创建这个漂亮的php对象树。我将如何使用循环将新对象插入items节点数组中?基本上如何将新的匿名项目对象插入此对象树中的项目数组

看看

<?php 

    $phpObjectTree = 

    (object)[
      'apiVersion' => '1.0',
      'data' => (object)[
        'requested' => '2020-01-01T23:59:59.001Z',
        'status' => 'OK',
        'estimate' => (object)[
          'id' => '1001',
          'type' => 'Commercial',
          'client' => (object)[
            'name' => 'ABC Construction, Inc.',
            'address1' => '123 Main St',
            'city' => 'Salt Lake City',
            'state' => 'UT',
            'postalCode' => '84101'
          ],
          'summary' => (object)[
            'generalRequirements' => (object)[
              'default' => 0
            ],
            'salesTax' => (object)[
              'material' => 4.85,
              'labor' => 4.85,
              'equipment' => 4.85
            ],
            'overheadProfit' => (object)[
              'material' => 10,
              'labor' => 10,
              'equipment' => 10
            ],
            'contingency' => (object)[
              'default' => 3
            ],
            'performanceBond' => (object)[
              'default' => 1.3
            ],
            'laborPlus' => (object)[
              'dailyRate' => 280,
              'crewCount' => 0,
              'dayCount' => 0
            ],
            'locationAdjustment' => (object)[
              'factor' => 90.1
            ],
          ]
        ],
        'items' => [
          (object)[
            'id' => '2001',
            'number' => '09 91 23.00 0000',
            'description' => 'Line Item 1',
            'quantity' => 0,
            'unit' => 'sf',
            'material' => (object)[
              'cost' => 0,
              'costEach' => 9.99
            ],
            'labor' => (object)[
              'cost' => 0,
              'costEach' => 9.99
            ],
            'equipment' => (object)[
              'cost' => 0,
              'costEach' => 9.99
            ],
            'lineTotal' => 0
          ],
          (object)[
            'id' => '2002',
            'number' => '09 91 23.00 0000',
            'description' => 'Line Item 2',
            'quantity' => 0,
            'unit' => 'sf',
            'material' => (object)[
              'cost' => 0,
              'costEach' => 9.99
            ],
            'labor' => (object)[
              'cost' => 0,
              'costEach' => 9.99
            ],
            'equipment' => (object)[
              'cost' => 0,
              'costEach' => 9.99
            ],
            'lineTotal' => 0
          ]
        ]
      ]
    ];
    // SET JSON HEADER...
    header('Content-type:application/json;charset=utf-8');

    print_r(json_encode($phpObjectTree));

?>

2 个答案:

答案 0 :(得分:1)

以同样的方式修改php中的任何数组:

https://www.php.net/manual/en/language.types.array.php#language.types.array.syntax.modifying

$phpObjectTree->data->items[] = (object)[
  'id' => '2003',
  'number' => '09 91 23.00 0000',
  'description' => 'Line Item 3',
  'quantity' => 0,
  'unit' => 'sf',
  'material' => (object)[
    'cost' => 0,
    'costEach' => 9.99
  ],
  'labor' => (object)[
    'cost' => 0,
    'costEach' => 9.99
  ],
  'equipment' => (object)[
    'cost' => 0,
    'costEach' => 9.99
  ],
  'lineTotal' => 0
];

答案 1 :(得分:0)

或者您可以使用stdClass创建新对象并添加属性。

import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup
import ssl

# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

urllist = list()
taglist = list()
url = input('Enter - ')
count = int(input('Enter count: '))
pos = int(input('Enter position: ')) - 1
urllist = list()
for i in range(count):
    html = urllib.request.urlopen(url).read()
    soup = BeautifulSoup(html, 'html.parser')
    tags=soup('a')
    for tag in tags: 
    # the most important part is keep updating the variable of tags by putting in front of this loop
        taglist.append(tag)
    print('Retrieving: ', url)
    url = taglist[pos].get('href', None) 
    urllist.append(url)

print('Retrieving: ', urllist[-1])
相关问题