1

I have the following array structure:

$this->entity += [
  $key => [
    'column' => $column,
    'required' => $required,
    'type' => $type,
    'value' => $value,
    'valueIfNull' => $valueIfNull,
    'useDBDefaultWhenNull' => $useDBDefaultWhenNull
  ]
];


var_dump($this->entity);

array(13) {
  ["id"]=>
  array(6) {
    ["column"]=>
    string(2) "fk_tbl_users_id"
    ["required"]=>
    bool(false)
    ["type"]=>
    array(2) {
      [0]=>
      string(7) "integer"
      [1]=>
      int(1)
    }
    ["value"]=>
    string(0) ""
    ["valueIfNull"]=>
    string(0) ""
    ["useDBDefaultWhenNull"]=>
    bool(true)
  }
  ["isAdmin"]=>
  array(6) {
    ....
  }
}

I need to be able to find the $key from a given $column value. I've tried the following, but of course, doesn't work:

$entity_key = array_search('fk_tbl_users_id', $this->entity, true);

I've also tried to make my $entity array with this structure (ommiting the keys 'column' etc):

$this->entity += [
  $key => [
    $column,
    $required,
    $type,
    $value,
    $valueIfNull,
    $useDBDefaultWhenNull
  ]
];

var_dump($this->entity);

array(13) {
  ["id"]=>
  array(6) {
    [0]=>
    string(2) "fk_tbl_users_id"
    [1]=>
    bool(false)
    [2]=>
    array(2) {
      [0]=>
      string(7) "integer"
      [1]=>
      int(1)
    }
    [3]=>
    string(0) ""
    [4]=>
    string(0) ""
    [5]=>
    bool(true)
  }
  ["isAdmin"]=>
  array(6) {
    ....
  }
}

Am I doing something wrong? I am assuming not, and the ONLY way I'll be able to retrieve the key is by force looping my array, get the 'column' value, compare it against a $col value, if true, then keep the $key and break my loop. Something like this:

  private function getKeyname(string $col)
  {
    foreach ( $this->entity as $key => $value )
    {
      $key_colname = $value[0]; // or $value['column'], depending on array structure I end up using
      if ( $key_colname === $col ) return $key;
    }
  }

I would of preferred not having to loop my array and use PHP builtin functions, if at all possible?

Thanks for your inputs!!

Pat.

Pat
  • 449
  • 1
  • 4
  • 14
  • Even with using built in functions you will get the loop, now it will be hidden under hood. So, what's the problem with loop? – u_mulder Jun 04 '20 at 10:32
  • 1
    `array_search` looks on the top level of the array only, it does not recursively go down any sub-arrays. The value you are looking for is not on the top level, it is inside the array under the `id` key. If you only want to search below that key, then specify the correct input array for the function – `$this->entity['id']`. If you want to search below all top-level keys - then you will have to loop over those, yes. – CBroe Jun 04 '20 at 10:33
  • Please don't yatta-yatta your input and desired output. @Pat use `var_export()` to present your data. Be clear about your requirements. I don't understand your question. – mickmackusa Jun 04 '20 at 10:38

2 Answers2

0

Any type of multidimensional array search would use loops to search through,wheather you do it with a built-in function or a custom one.For this there are answers present at this resource PHP multidimensional array search by value.

Huzaifa
  • 345
  • 4
  • 15
  • If you wish to resolve a question by suggesting another Stack Overflow page, then you should not answer, you should flag to close. – mickmackusa Jun 04 '20 at 10:53
0

Thanks for your replies folks! I went ahead and did my getKeyname() function. As a few mentioned, in the end buildin php functions still do loops anyhow ;)

Cheers! Pat

Pat
  • 449
  • 1
  • 4
  • 14