Find the location of an Item in multidimensional array

Find the location of an Item in multidimensional array

Sometimes we have a large multi-dimensional array and we need only one tree of an array where a specific key word or specific element are exits. So in this situation we require searching in an array or something like that, That's why now I am going to tell you a specific function to return an array of matched item from top level to end.

 

We will use the array_merge PHP function to merge an array in every step of an array.

array_merge($keys, array($key))

 

For each steps a loop will work with function call itself, for example:

Loading...
function array_get_location($array, $search, $keys = array())
{
    foreach($array as $key => $value) {
        if (is_array($value)) {
            $child_array = array_get_location($value, $search, array_merge($keys, array($key)));
            if (count($child_array)) {
                return $child_array;
            }
        } elseif ($value === $search) {
            return array_merge($keys, array($key));
        }
    }
    return array();
}

 

Now create an multi-dimensional array example:

$array = array(
		'sales' => array(
			'id' 		=> 'sales',
			'department'=> 'Sales',
			'employe'	=> array(
				'0' => array(
					'name' 		=> 'Thor',
					'age'		=> '34'
				),
				'1' => array(
					'name' 		=> 'Barton',
					'age'		=> '30'
				),
				'2' => array(
					'name' 		=> 'Hulk',
					'age'		=> '37'
				),
				'3' => array(
					'name' 		=> 'Tony',
					'age'		=> '35'
				)
			)
		),
		'accounts' => array(
			'id' 		=> 'accounts',
			'department'=> 'Accounts',
			'employe'	=> array(
				'0' => array(
					'name' 		=> 'Iron Man',
					'age'		=> '34'
				),
				'1' => array(
					'name' 		=> 'Black',
					'age'		=> '30'
				),
				'2' => array(
					'name' 		=> 'Captain',
					'age'		=> '37'
				),
				'3' => array(
					'name' 		=> 'Loki',
					'age'		=> '35'
				)
			)
		)
	);

 

And now if we try to find tree by the name "Hulk" then return array will be like this:

Array
(
    [0] => sales
    [1] => employe
    [2] => 2
    [3] => name
)

 

Loading...

And if we try to find tree by the name "Loki" then return array will be like this:

Array
(
    [0] => accounts
    [1] => employe
    [2] => 3
    [3] => name
)

 

So, Final code is:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php
$array = array(
		'sales' => array(
			'id' 		=> 'sales',
			'department'=> 'Sales',
			'employe'	=> array(
				'0' => array(
					'name' 		=> 'Thor',
					'age'		=> '34'
				),
				'1' => array(
					'name' 		=> 'Barton',
					'age'		=> '30'
				),
				'2' => array(
					'name' 		=> 'Hulk',
					'age'		=> '37'
				),
				'3' => array(
					'name' 		=> 'Tony',
					'age'		=> '35'
				)
			)
		),
		'accounts' => array(
			'id' 		=> 'accounts',
			'department'=> 'Accounts',
			'employe'	=> array(
				'0' => array(
					'name' 		=> 'Iron Man',
					'age'		=> '34'
				),
				'1' => array(
					'name' 		=> 'Black',
					'age'		=> '30'
				),
				'2' => array(
					'name' 		=> 'Captain',
					'age'		=> '37'
				),
				'3' => array(
					'name' 		=> 'Loki',
					'age'		=> '35'
				)
			)
		)
	);

function array_get_location($array, $search, $keys = array())
{
    foreach($array as $key => $value) {
        if (is_array($value)) {
            $child_array = array_get_location($value, $search, array_merge($keys, array($key)));
            if (count($child_array)) {
                return $child_array;
            }
        } elseif ($value === $search) {
            return array_merge($keys, array($key));
        }
    }
    return array();
}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Find Location Of An Item In Multidimensional Array</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">
  <h2 class="text-success">Find Location Of An Item In Multidimensional Array</h2>
    <?php
    echo "<pre>";
	print_r(array_get_location($array, 'Loki'));
    ?>
</div>
   
</body>
</html>

Related posts

Write a comment