Jump to content
Orkun

Access value of a multidimensional associative array Element inside the same array

Recommended Posts


$stats = array(
	"total" => array(
		"main" => array( 
		     "selector" => "template=50, parent=$formSuperSelector", 
		     "count" => $this->pages->find($stats['total']['main']['selector'])->count
		 ),
		 "yes" => array( 
		     "selector" => $stats['total']['main']['selector'].", teilnahme=yes", 
		     "count" => $this->pages->find($stats['total']['yes']['selector'])->count
		 ),
	         "no" => array( 
		     "selector" => $stats['total']['main']['selector'].", teilnahme=no", 
		     "count" => $this->pages->find($stats['total']['no']['selector'])->count
		 )
	 ),

	 "intern" => array(
		  "main" => array( 
		       "selector" => "template=50, formtype=intern, parent=$formSuperSelector", 
		       "count" => $this->pages->find($stats['intern']['main']['selector'])->count
		   ),
		   "yes" => array( 
		       "selector" => $stats['intern']['main']['selector'].", teilnahme=yes", 
		       "count" => $this->pages->find($stats['intern']['yes']['selector'])->count
		   ),
		   "no" => array( 
		       "selector" => $stats['intern']['main']['selector'].", teilnahme=no", 
		       "count" => $this->pages->find($stats['intern']['no']['selector'])->count
		   )
	  ),

	  "extern" => array(
		   "main" => array( 
			"selector" => "template=50, formtype=extern, parent=$formSuperSelector", 
			"count" => $this->pages->find($stats['extern']['main']['selector'])->count
		    ),
		    "yes"  => array( 
			"selector" => $stats['extern']['main']['selector'].", teilnahme=yes", 
			"count" => $this->pages->find($stats['extern']['yes']['selector'])->count
		    ),
		    "no"   => array( 
			"selector" => $stats['extern']['main']['selector'].", teilnahme=no", 
			"count" => $this->pages->find($stats['extern']['no']['selector'])->count
		    )
	    ),

 );
			

 var_dump($stats['total']['main']['count']);

How can I bring this to work? I always get a "Notice: Undefined variable: stats in..." when trying to access:

 "count" => $this->pages->find($stats['intern']['yes']['selector'])->count

or

"selector" => $stats['intern']['main']['selector'].", teilnahme=yes", 

Share this post


Link to post
Share on other sites

I know I could do it like this(will probably do it like this ^^):

$totalselector = "template=50, parent=$formSuperSelector";
$internselector = "template=50, formtype=intern, parent=$formSuperSelector";
$externselector = "template=50, formtype=extern, parent=$formSuperSelector";

$stats = array(
...
...
"selector" => $totalselector, 
"count" => $this->pages->find($totalselector)->count
...
...
)

But now I am interested why the other method above doesnt works

EDIT: Just realised that this method is limited:

"yes"  => array( 
     "selector" => "$totalselector, teilnahme=yes", //need a new var with this selector 
     "count" => $this->pages->find($totalselector)->count //because of this
),

while this only works for "main" index:

"main"  => array( 
     "selector" => $totalselector, 
     "count" => $this->pages->find($totalselector)->count

Share this post


Link to post
Share on other sites

All the $pages->find() calls are evaluated while creating $stats, which in turn means $stats is not defined at that moment. If you want the count to run "on-demand" you'd need to look into using anonymous functions. 

Also using $pages->find()->count is most of the time an anti-pattern. If you're not using those pages, but only the count rather user $pages->count($selector). The latter does only count the pages in the db, whereas your version does load all those pages and does count them in memory (php). 

Edit:

Just to make things more clear about the array creation. You're building up an array and after the whole array is computed – with all dynamic calls being evaluated to static values – you're saving the data into the variable $stats. You could go another way like this:

$stats = array(); // Create array; $stats !== undefined
$stats["total"] = array(
  ["main"] => array(),
  ["yes"] => array()
);

// Update each value one at a time
$stats["total"]["main"]["selector"] = "template=50, parent=$formSuperSelector";
$stats["total"]["main"]["count"] = $this->pages->count($stats['total']['main']['selector']);

…

  • Like 1

Share this post


Link to post
Share on other sites

All the $pages->find() calls are evaluated while creating $stats, which in turn means $stats is not defined at that moment. If you want the count to run "on-demand" you'd need to look into using anonymous functions. 

Also using $pages->find()->count is most of the time an anti-pattern. If you're not using those pages, but only the count rather user $pages->count($selector). The latter does only count the pages in the db, whereas your version does load all those pages and does count them in memory (php). 

at the moment i have total pages of 317. But when I do $this->pages->count($selector), i get somehow 415?

Share this post


Link to post
Share on other sites

Can you post the selector for that? Also in which context do you have that issue (module, template or bootstraped)? Irrespective of that issue, you wouldn't want to load 300 pages (fully with all their field values) just to count how many you have.

Share this post


Link to post
Share on other sites

Can you post the selector for that? Also in which context do you have that issue (module, template or bootstraped)? Irrespective of that issue, you wouldn't want to load 300 pages (fully with all their field values) just to count how many you have.

Nevermind, it works now with your method. :)

Share this post


Link to post
Share on other sites

The $pages->count() does work? If not than it would be nice to investigate this further, because it should return the same as counting locally.

Share this post


Link to post
Share on other sites
$stats = array();
$mainIndexes = array("total", "intern", "extern");

foreach ($mainIndexes as $mainIndex) {
	$stats[$mainIndex] = array(
		'main' => array(),
		'yes' => array(),
		'no' => array(),
	);
}

$stats['total']['main']['selector'] = "template=50, parent=$formSuperSelector";
$stats['total']['main']['count'] = $this->pages->count($stats['total']['main']['selector']);

$stats['total']['yes']['selector'] = $stats['total']['main']['selector'].", teilnahme=yes";
$stats['total']['yes']['count'] = $this->pages->count($stats['total']['yes']['selector']);

$stats['total']['no']['selector'] = $stats['total']['main']['selector'].", teilnahme=no";
$stats['total']['no']['count'] = $this->pages->count($stats['total']['no']['selector']);

$stats['intern']['main']['selector'] = "template=50, formtype=intern, parent=$formSuperSelector";
$stats['intern']['main']['count'] = $this->pages->count($stats['intern']['main']['selector']);

$stats['intern']['yes']['selector'] = $stats['intern']['main']['selector'].", teilnahme=yes";
$stats['intern']['yes']['count'] = $this->pages->count($stats['intern']['yes']['selector']);

$stats['intern']['no']['selector'] = $stats['intern']['main']['selector'].", teilnahme=no";
$stats['intern']['no']['count'] = $this->pages->count($stats['intern']['no']['selector']);
		
$stats['extern']['main']['selector'] = "template=50, formtype=extern, parent=$formSuperSelector";
$stats['extern']['main']['count'] = $this->pages->count($stats['extern']['main']['selector']);

$stats['extern']['yes']['selector'] = $stats['extern']['main']['selector'].", teilnahme=yes";
$stats['extern']['yes']['count'] = $this->pages->count($stats['extern']['yes']['selector']);

$stats['extern']['no']['selector'] = $stats['extern']['main']['selector'].", teilnahme=no";
$stats['extern']['no']['count'] = $this->pages->count($stats['extern']['no']['selector']);

print_r($stats);

It works like a charm.

post-3125-0-18402500-1456919150_thumb.pn

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Atlasfreeman
      Hi!
      I want to make a small site, a one page site.
      And i have this idea about doing 2 to 3 diffrent template that i can load into the index / home page.

      I want to do this with an array so that i can keep creating more topics (with the template) id needed.
      <?php include('./head.inc'); // include header markup ?> <?php $children = $page->get('template=onecolmn|twocolumn, sort=sort'); foreach($children as $child) { include($child); } ?> <?php include('./foot.inc'); // include footer markup ?>  
      As you can see i have to template wish i want to control from the backend.
      I know include does not work this way, put what is my other option to make an array that loads the whole page on an other pages.
       
       
    • By Fran
      Hi, I'm using this piece of code to retrieve the names of two arrays of images and it works really well. But my question is how can I use only one foreach so i can use both arrays inside of it? Thanks very much.
      $precioschico = $page->get("planos"); $preciosgrande = $page->get("mapas"); foreach( $preciosgrande as $preciogrande ) { echo $preciogrande . '<br/>'; } foreach( $precioschico as $preciochico ) { echo $preciochico . '<br/>'; }  
    • By Macrura
      Needed to show someone how to quickly setup some settings for various things in a simple text area:
      could be used for slider settings, site settings, etc;
      What it does: gives you a matching variable for each key of each line...
      1.) setup a textarea field for the settings ; i'm calling it settings_ta
      2.) add delimited settings, 1 per line; i use a pipe (|) delimiter;
      example:
      address|some info here facebook|https://www.facebook.com twitter|https://twitter.com phone|(999) 999-9999 3.) in your _init.php, get the settings - replace the page number and the name of your settings field:
      $settings_ta = $pages->get(1644)->settings_ta; $settings_lines = explode(PHP_EOL, $settings_ta);         foreach($settings_lines as $settings_row) {         $settings_pair = explode('|', $settings_row);    ${trim($settings_pair[0])} = trim($settings_pair[1]);     } more condensed version, for those of you who like brevity...
      foreach(explode(PHP_EOL, $pages->get(1644)->settings_ta) as $settings_row) { $settings_pair = explode('|', $settings_row); ${trim($settings_pair[0])} = trim($settings_pair[1]); } now you can do this in your templates:
      echo $address; echo $facebook; echo $twitter; echo $phone; Edit: made the code simpler....; 2nd edit - added trim to support using ace editor with tabs
      Addendum: as an added convenience, you could use the Ace text editor module which would give you a monospaced text field and the ability to use tabs for your settings, which could be more readable and easier to edit. The code has been updated to support this by trimming the exploded strings prior to generating the variable/value.
      address | some info here facebook | http://www.facebook.com twitter | http://twitter.com phone | (999) 999-9999
    • By lsag
      Hello ppl
      I'm trying to create an image banner to show in all pages of the site, and for this i created a page to hold multiple images in a field (latter im gonna deal with animating them). The page is published, So this is the tree of values i got:

      i can address the page with
      $pg_settings = $pages->get('name=settings'); it seem to be correct because print_r gets me a biiiig array if info
      now the problem is getting the array of images, with their respective url's. I tried:
      $pg_settings = $pages->get('name=settings'); // echo "<br />pg_settings: " . print_r($pg_settings->get("banners_slideshow")); // seems ok... $imgs = $pg_settings->get("banners_slideshow"); print_r($imgs[0]->get("url")); // i got nothing?? In this page i also have a non repeatable image field for the logo, so i think i cant use the images array from that page. 
      the question remains: how do i address a field with multiples images, that is in another page?
      thanks for the patience!
×
×
  • Create New...