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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.