Cedric
Cedric

Reputation: 1256

Access an array values in twig symfony2

I have this array of elements to be printed as a chart:

array(7) {
  [0]=>
  array(2) {
    [0]=>
    array(0) {
    }
    [1]=>
    string(22) "/app_dev.php/users/new"
  }
  [1]=>
  array(2) {
    [0]=>
    array(0) {
    }
    [1]=>
    string(19) "/app_dev.php/users/"
  }



  [2]=>
  array(2) {
[0]=>
    array(1) {
      [0]=>
      object(BTI\RepSysBundle\Objects\gapiReportEntry)#359 (2) {
        ["metrics":"BTI\RepSysBundle\Objects\gapiReportEntry":private]=>
        array(1) {
          ["pageviews"]=>
          int(54)
        }
        ["dimensions":"BTI\RepSysBundle\Objects\gapiReportEntry":private]=>
        array(1) {
          ["dimension1"]=>
          string(14) "rainercedric23"
        }
      }
    }
    [1]=>
    string(30) "/app_local.php/admin/analytics"
  }
  [3]=>
  array(2) {
    [0]=>
    array(0) {
    }
    [1]=>
    string(16) "/admin/analytics"
  }
}

The problem is I can access the string values like the "/app_dev.php/users/new" by this code

{{ foo[0].1}}

But I can't seem to access the object with metrics and pageviews I am trying this one

{{ foo[2].0.0.metrics.pageviews}}

But it doesn't work, anyone have an idea to access an object element? I have this service:

<?php

namespace BTI\RepSysBundle\Services;

use BTI\RepSysBundle\Objects\Gapi;

class GapiManager {
public function GAPIGetter() {
    $Gapi = new Gapi('[email protected]', 'somepassword');
    $path = array("/app_dev.php/users/new",
        "/app_dev.php/users/",
        "/app_local.php/admin/analytics",
        "/admin/analytics",
        "/app_dev.php/account/",
        "/app_dev.php",
        "/app_dev.php/account/new"
    );

    foreach ($path as $filterpath) {
        $filters[] = "ga:pagePath==" . $filterpath;
    }
    $ctr = 0;
    foreach ($filters as $filter) {
        $Gapisquery[] = array($Gapi->requestReportData('81757262', array('dimension1'), array('pageviews'), 'pageviews', $filter), $path[$ctr]);
        $ctr++;
    }



    return array_filter($Gapisquery);
}

}

basically this service request the report data from the Google Analytics and returns the page views from each url path mentioned. the problem is that it returns a private object from the Gapi class.

Upvotes: 0

Views: 554

Answers (1)

NHG
NHG

Reputation: 5877

Your metrics property is private. You have to write getter for this property like:

// file BTI\RepSysBundle\Objects\gapiReportEntry.php
namespace BTI\RepSysBundle\Objects;

class gapiReportEntry{
    // other code
    public function getMetrics{
       return $this->metrics;
    }
}

After that you have access in twig via object.metrics.

Please, read documentation about twig Twig Variables.

Upvotes: 1

Related Questions