0

I have this key "this.key.exists". Key can be: a.b.c.d.e.f etc. this is only example.

And i want to check if exists in array:

$array = [ // depth of array and number of values are variable
  'this' => [
       'key' => [
           'exists' => 'some value' // this is what am i looking for
       ],
       'key2' => [
           'exists' => 'some value' // this is not what am i looking for
       ],
  ],
  'that' => [
       'this' => [
            'key' => [
                'exists' => 'some value' // this is not what am i looking for
            ],
       ]
  ]
];

I need to find this key for update his value.

$array['this']['key']['exists'] = 'need to set new value'; 

Thanks for help

Jack
  • 55
  • 6
  • I need to modify value of result i found. That is why i think it is not duplicate question. – Jack Sep 01 '15 at 11:12

1 Answers1

0
$str = "this.key.exists";

$p = &$array;                               // point to array root
$exists =  true;
foreach(explode('.', $str) as $step) { 
   if (isset($p[$step])) $p = &$p[$step];   // if exists go to next level
   else { $exists = false; break; }         // no such key  
}

if($exists) $p = "new value";
splash58
  • 26,043
  • 3
  • 22
  • 34