Jump to content

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


Orkun
 Share

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", 
Link to comment
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
Link to comment
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
Link to comment
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?

Link to comment
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.

Link to comment
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. :)

Link to comment
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

Link to comment
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
 Share

×
×
  • Create New...