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
$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 に答える
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 に答える