jonboy
jonboy

Reputation: 2749

Multidimensional Associative Array Output

I am using PHP / Codeigniter and working with Google APIs Client Library for PHP, I am trying to output some data from a multidimensional array.

In my controller I have;

$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$client->setDeveloperKey("MyKey");
$service = new Google_Service_Books($client);

$data = array(
    'title' => 'Library Items',
    'gbook' => $service->volumes->listVolumes('0-7515-3831-0')
);

In my view I have;

  echo 'Page Title is: ' .$title; // this successfully prints 'Library Items'
  echo 'Book Title is: '; // need to output the book title here 
  echo 'Description is: '; // need to output the book description here 

When I print_r($gbook) in my view I see the following https://pastebin.com/HM5hds6e

I'm not sure how to select specific values from this array (namely title and isbn). Any help or tips would be appreciated!

Thanks

Upvotes: 0

Views: 168

Answers (2)

Cvetan Mihaylov
Cvetan Mihaylov

Reputation: 950

Try accessing the volume data like this:

echo 'Page Title is: ' .$title; // this successfully prints 'Library Items'
echo 'Book Title is: ' .$gbook->getItems()[0]['volumeInfo']['title'];
echo 'Description is: ' .$gbook->getItems()[0]['volumeInfo']['description'];

Upvotes: 0

Vara Prasad
Vara Prasad

Reputation: 497

You can try this code.

As I have checked in this url https://github.com/google/google-api-php-client you can directly loop that variable gbook

Loop the items array $gbook from the response and get the details of books

foreach ($gbook as $bookInfo) {
  echo "Book title : ".$bookInfo["volumeInfo"]["title"];
  $bookIdent = array_map(function($v){
    return $v["identifier"];
  }, $bookInfo["volumeInfo"]["industryIdentifiers"]);
  echo "Book ISBN : ".(implode(",", $bookIdent));
}

Upvotes: 1

Related Questions