20

For example, I have multidimensional array as below:

$array = array ( 0 => array ( 'id' => '9', 'gallery_id' => '2', 'picture' => '56475832.jpg' ), 1 => array ( 'id' => '8', 'gallery_id' => '2', 'picture' => '20083622.jpg' ), 2 => array ( 'id' => '7', 'gallery_id' => '2', 'picture' => '89001465.jpg' ), 3 => array ( 'id' => '6', 'gallery_id' => '2', 'picture' => '47360232.jpg' ), 4 => array ( 'id' => '5', 'gallery_id' => '2', 'picture' => '4876713.jpg' ), 5 => array ( 'id' => '4', 'gallery_id' => '2', 'picture' => '5447392.jpg' ), 6 => array ( 'id' => '3', 'gallery_id' => '2', 'picture' => '95117187.jpg' ) ); 

How can I get key of array(0,1,2,3,4,5,6)?

I have tried a lot of examples, but nothing has worked for me.

5 Answers 5

24

This is quite simple, you just need to use array_keys():

$keys = array_keys($array); 

See it working

EDIT For your search task, this function should do the job:

function array_search_inner ($array, $attr, $val, $strict = FALSE) { // Error is input array is not an array if (!is_array($array)) return FALSE; // Loop the array foreach ($array as $key => $inner) { // Error if inner item is not an array (you may want to remove this line) if (!is_array($inner)) return FALSE; // Skip entries where search key is not present if (!isset($inner[$attr])) continue; if ($strict) { // Strict typing if ($inner[$attr] === $val) return $key; } else { // Loose typing if ($inner[$attr] == $val) return $key; } } // We didn't find it return NULL; } // Example usage $key = array_search_inner($array, 'id', 9); 

The fourth parameter $strict, if TRUE, will use strict type comparisons. So 9 will not work, you would have to pass '9', since the values are stored as strings. Returns the key of the first occurence of a match, NULL if the value is not found, or FALSE on error. make sure to use a strict comparison on the return value, since 0, NULL and FALSE are all possible return values and they will all evaluate to 0 if using loose integer comparisons.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes it's working, but, i need to search in array for example id=9, and get return id of array key, where's id = 9
7

Try this , I think it will help you.

foreach ($array as $key=>$value) { echo $key.'<br/>'; echo $value['id'].'<br/>'; echo $value['gallery_id'].'<br/>'; echo $value['picture'].'<br/><br/>'; } 

Comments

2

sometimes it is to easy to find ;)

array_keys($array); 

array_keys

Comments

2

Probably http://php.net/manual/en/function.array-keys.php ?

Convert your double dimensional array on your own:

$tmp = null foreach($array as $key => $value) { $tmp[] = $key; } print_r($tmp); 

Comments

0

You mean something like this:

  function getKeys($array) { $resultArr = array(); foreach($array as $subArr) { $resultArr = array_merge($resultArr, $subArr); } return array_keys($resultArr); }  

1 Comment

I think i didn't explain well my situation. For example, i'm looking for array where's id = 9, and i need a array key of that wheres 9, it would be 0, if i will be looking for id wheres 8, i will get 1

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.