Jump to content

finding deeply nested array positions..


freelance84

Recommended Posts

 

Does anyone know a way of to find the 'coordinates' of a deeply nested key in a multidimensional array whilst using array_walk_recursive?

 

I am using array_walk_recursive to stripslashes of all values of an array. However I need to be able to get the full coordinates of a key if the said key is of a certain value. The function key only seems to get the highest level key.

 

As a test I have the following array which has three occurrences of a key named 'date'. Upon each occurrence I want to be able to know exactly where in the array it is so i can call is directly later.

 

<?php

$pages = array(array("variation"=>"xccsdc",
		"items"=>array(array("nm"=>"ss",
					  "type"=>"ta",
					  "kkl"=>"Prodion."
					  ),
			array("nm"=>"nm",
					"type"=>"nwsfd",
					"kkl"=>"nws fd",
					"nws"=>array(array("date"=>"1328982639",
										"kkl"=>"f nfgfgnfgn",
										"title"=>"nfgn fgn fhn "
											),
								 array("date"=>"1328982632", 
						  				"kkl"=>"fgn fgn",
						  				"title"=>" fgn"
								  			)
								  )
					),
			array("nm"=>"nm",
					"type"=>"nwsfd",
					"kkl"=>"nws fd",
					"nws"=>array(array("date"=>"1328982655",
									"kkl"=>" gh fgh","title"=>"n fgh n"
											)
								)
				)
				)
			)
	);

function test_print(&$item, $key, $pages)
{
if($key == 'date')
	{
		//strip the slashes
		$item = stripslashes($item);

		//if the key is 'date' which is deeply nested, return the key.
		echo key(current($pages));

		echo '<br/>';
	}
}

array_walk_recursive($pages, 'test_print', $pages);

?>

What is returned from the 'test_print' function is:

variation

variation

variation

 

Is this because array_walk_recursive does not move the internal pointer pointer along?

 

I found this thread on stackoverflow: http://stackoverflow.com/questions/3133054/php-find-key-position-in-multidimensional-array which has a great function for finding the key, however i'm struggling to get my head around how I can apply something like this whilst using array_walk_recursive.

 

Does anyone have any ideas?

 

Link to comment
https://forums.phpfreaks.com/topic/257030-finding-deeply-nested-array-positions/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.