1

I would like to get/find array key by some of array's values.

I tried array_search but this doesn't help with multidimensional arrays as I hoped.

For example I have this kind of array and I need to get all array keys where personal_code = 12345678910. In this example I should get array(0,1) because in first and second array are the personal_code 12345678910.

How could I get those keys?

Array
(
  [0] => Array
  (
    [id] => 32155
    [personal_code] => 12345678910
    [cadaster] => 12345:321:1234
    [purpose] => Purpose 1
    [address] => Blah blah 1
    [area] => 600m2
  )

  [1] => Array
  (
    [id] => 14131
    [personal_code] => 12345678910
    [cadaster] => 12345:123:4321
    [purpose] => Purpose 2
    [address] => Blah blah 3
    [area] => 1200m2
  )

  [2] => Array
  (
    [id] => 32303
    [personal_code] => 54321678910
    [cadaster] => 12345:123:1234
    [purpose] => Purpose 3
    [address] => Blah blah 2
    [area] => 1800m2
  )
)

2 Answers 2

1
$newArr = array();
foreach ($yourArr as $arr)
{
  if($arr['personal_code']==12345678910)
  {
    $newArr []=$arr;
  }
}
print_r($newArr);
Sign up to request clarification or add additional context in comments.

Comments

1

You can just use a simple foreach to get those keys. Example:

$find = '12345678910';
$keys = array();
foreach($your_array as $key => $values) { // loop your array
    // now values will hold each array batch inside that parent array
    if($values['personal_code'] == $find) { // so it if matches
        $keys[] = $key; // then put it inside
    }
}

echo '<pre>';
print_r($keys);

Sample Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.