0

配列内のキーの値を取得するにはどうすればよいですか?

配列は、次のGoogleショッピングAPIによって行われます。

// Valid source values are "public", "cx:cse", and "gan:pid"
// See http://code.google.com/apis/shopping/search/v1/getting_started.html#products-feed
$source = "public";

// For more information about full text search with the shopping API, please
// see http://code.google.com/apis/shopping/search/v1/getting_started.html#text-search
$query = "\"mp3 player\" | ipod";

//The order in which the API returns products is defined by a ranking criterion.
// See http://code.google.com/apis/shopping/search/v1/getting_started.html#ranking
$ranking = "relevancy";

$results = $service->products->listProducts($source, array(
  "country" => "UK",
  "q" => $query,
  "rankBy" => $ranking,
));

print "<h1>Shopping Results</h1><pre>" . print_r($results, true) . "</pre>";

出力する次の配列があります: Shopping Results

Array
(
    [kind] => shopping#products
    [etag] => "*********"
    [id] => tag:google.com,2010:shopping/products
    [selfLink] => https://www.googleapis.com/shopping/search/v1/public/products?country=UK&q=iphone&rankBy=relevancy
    [nextLink] => https://www.googleapis.com/shopping/search/v1/public/products?country=UK&q=iphone&rankBy=relevancy&startIndex=26
    [totalItems] => 771622
    [startIndex] => 1
    [itemsPerPage] => 25
    [currentItemCount] => 25
    [requestId] => 0CMjH976CqbECFYNWtAodLRwAAA
    [items] => Array
    (
        [0] => Array
            (
                [kind] => shopping#product
                [id] => tag:google.com,2010:shopping/products/5735617/11254757413841304510
                [selfLink] => https://www.googleapis.com/shopping/search/v1/public/products/5735617/gid/11254757413841304510
                [product] => Array
                    (
                        [googleId] => 11254757413841304510
                        [author] => Array
                            (
                                [name] => Amazon.co.uk
                                [accountId] => 5735617
                            )

                        [creationTime] => 2012-05-04T05:03:50.000Z
                        [modificationTime] => 2012-07-20T02:02:16.000Z
                        [country] => GB
                        [language] => en
                        [title] => Apple iPod touch 8GB - Black - 4th Generation (Latest Model - Launched Sept 2010)
                        [description] => Apple iPod touch 8GB - Black - 4th Generation (Latest Model - Launched Sept 2010)
                        [link] => http://www.amazon.co.uk/dp/B0040GIZTI/ref=asc_df_B0040GIZTI8843997?smid=A1YZ4RXO7GUOYN&tag=googlecouk06-21&linkCode=asn&creative=22218&creativeASIN=B0040GIZTI
                        [brand] => Apple
                        [condition] => new
                        [gtin] => 00885909394739
                        [gtins] => Array
                            (
                                [0] => 00885909394739
                            )

                        [mpns] => Array
                            (
                                [0] => MC540BT/A
                            )

                        [inventories] => Array
                            (
                                [0] => Array
                                    (
                                        [channel] => online
                                        [availability] => inStock
                                        [price] => 135.95
                                        [shipping] => 1.99
                                        [currency] => GBP
                                    )

                            )

                        [images] => Array
                            (
                                [0] => Array
                                    (
                                        [link] => http://ecx.images-amazon.com/images/I/41p2rNmazRL.jpg
                                        [status] => available
                                    )

                            )

                    )

            )

        [1] => Array
            (
                [kind] => shopping#product
                [id] => tag:google.com,2010:shopping/products/5735617/4597224105326146239
                [selfLink] => https://www.googleapis.com/shopping/search/v1/public/products/5735617/gid/4597224105326146239
                [product] => Array
                    (
                        [googleId] => 4597224105326146239
                        [author] => Array
                            (
                                [name] => Amazon.co.uk
                                [accountId] => 5735617
                            )

                        [creationTime] => 2012-05-04T05:03:50.000Z
                        [modificationTime] => 2012-07-20T02:02:16.000Z
                        [country] => GB
                        [language] => en
                        [title] => SanDisk Sansa Clip+ 8GB MP3 Player with Radio and Expandable MicroSD/SDHC Slot - Black
                        [description] => 8 GB memory Digital FM-tuner with 40 preset radio stations Extendable microSD/microSDHC card slot
                        [link] => http://www.amazon.co.uk/dp/B002NX0ME6/ref=asc_df_B002NX0ME68843997?smid=A3P5ROKL5A1OLE&tag=googlecouk06-21&linkCode=asn&creative=22206&creativeASIN=B002NX0ME6
                        [brand] => SanDisk
                        [condition] => new
                        [gtin] => 00619659059989
                        [gtins] => Array
                            (
                                [0] => 00619659059989
                            )

                        [mpns] => Array
                            (
                                [0] => SDMX18-008G-E46K
                            )

                        [inventories] => Array
                            (
                                [0] => Array
                                    (
                                        [channel] => online
                                        [availability] => inStock
                                        [price] => 46.95
                                        [shipping] => 0
                                        [currency] => GBP
                                    )

                            )

                        [images] => Array
                            (
                                [0] => Array
                                    (
                                        [link] => http://ecx.images-amazon.com/images/I/419U6bYDF1L.jpg
                                        [status] => available
                                    )

                            )

                    )

            )

このすべてのデータは必要ありません。必要なのは 3 ~ 4 個のキーだけですが、どうすればそれらにアクセスできますか? 各配列から言う[タイトル]の値をどのようにエコーしますか?

4

4 に答える 4

0

各商品のタイトルを$resultsに出力するには:

foreach ($results as $result) {
    echo $result['product']['title'];
}
于 2012-07-20T18:59:23.807 に答える
0

array_walk_recursiveの使用を検討してください

実例

<?php
$a = array("hai", array("ha"=>1, "hai"=>2, array("a"=>1, "b"=>2)));
function test($item, $key) 
{
     echo "$key holds $item\n";
}
array_walk_recursive($a, 'test');

0 holds hai
ha holds 1
hai holds 2
a holds 1
b holds 2

タイトルだけに興味があるなら

foreachの使用を検討してください

foreach($results['item'] as $result) {
   echo $result['product']['title'];
}
于 2012-07-20T19:02:14.077 に答える
0

これはうまくいくはずです:

foreach( $results as $result)
    foreach( $result['product'] as $product) 
        echo $product['title']; 
于 2012-07-20T18:49:24.620 に答える
0

上で指摘したように配列をループするか、次のように array_walk_recursive を使用することができます。

$title_array = array();

array_walk_recursive($input_array, 'find_titles');

function find_titles($value, $key) {
    global $title_array;
    if ($key == 'title') {
        $title_array[] = $value;
    }
}

入力配列の構造が不明な場合 (つまり、探しているキーがネストされているレベル数) が不明な場合は、これがより良い解決策になる可能性があります。

于 2012-07-20T18:54:55.527 に答える