0

如何将 stdClass 对象的名称导入数据库?

JSON 对象包含:

[items] => stdClass Object
                    ( 
  [bigitems] => stdClass Object
                    (
                        [nameitem1] => stdClass Object
                            (
                                [height] => 100
                                [width] => 137
            )
            [nameitem2] => stdClass Object
                            (
                                [height] => 506
                                [width] => 678
            )
            [nameitem3] => stdClass Object
                            (
                                [height] => 330
                                [width] => 845
            )
            [nameitem3] => stdClass Object
                            (
                                [height] => 793
                                [width] => 788
            )
        )
)

我只想将值 nameitem1 - nameitem4 存储到数据库中

Nameitems 是例如 - box,case - 不同的词没有数字范围 Bigitems 是一个 NUMBER

    [items] => stdClass Object
                    ( 
  [4856985254] => stdClass Object
                    (
                        [box] => stdClass Object
                            (
                                [height] => 100
                                [width] => 137
            )
            [case] => stdClass Object
                            (
                                [height] => 506
                                [width] => 678
            )
            [paper] => stdClass Object
                            (
                                [height] => 330
                                [width] => 845
            )
        )

对于此操作,我尝试使用此脚本。脚本工作但缺少商店部分

function wwwResponse($url, $isUrl = true)
{
  if ($isUrl && !ini_get('allow_url_fopen')) {
    throw new Exception('allow_url_fopen = Off');
  }

  $content = file_get_contents($url);
  if ($isUrl) {
    $status_code = substr($http_response_header[0], 9, 3);
    if ($status_code != 200) {
      throw new Exception('Got status code ' . $status_code . ' expected 200');
    }
  }

  $json = json_decode($content);
  if (!$json) {
    throw new Exception('Response contained an invalid JSON response');
  }

  if ($json->status != 'ok') {
    throw new Exception($json->status_code);
  }

  return $json;
}

// --------------------------DB------------------------------- 
 mysql_connect("localhost", "baze", "xxxxxxx");
 mysql_select_db("baze");
//------------------------------------------------------------

    try {
    $json = wwwResponse($url);
        print_r ($json);
    }
  catch (Exception $e) {
  echo $e->getMessage();
}
echo "<pre>\n";
}

感谢帮助

4

3 回答 3

1

如果它可以帮助您以这种方式解码 JSON 以获取数组而不是 STD 对象:

$result = json_decode($json, TRUE);

无论如何,你可以这样做:

foreach ($data as $d) {
    foreach ($d as $actual) {
        //echo stuff here

    }
}
于 2013-10-10T20:58:06.500 回答
1
$items = json_decode(json_encode($obj->items), true);
$names = array_keys((Array) $items['4856985254']);

// will return Array("nameitem1","nameitem2","nameitem3")
于 2013-10-10T20:55:40.770 回答
0
$vars = get_object_vars($items->bigitems);
foreach ($vars as $key => $value)
... insert $key ....
于 2013-10-10T20:56:45.053 回答