php - Search value in same array and replace it -


i have array

array (     [0] => array         (             [id] => 15             [parent] => #             [text] => shb2-1         )      [1] => array         (             [id] => 17             [parent] => 16             [text] => shb2-3         )      [2] => array         (             [id] => 18             [parent] => 17             [text] => shb2-4         )  ) 

i search if [parent] value exist in same array [id]

if not exist exists, [parent] value replaced 0.

example:

check if value 16 exists in whole array [id] (in example available [id] 15, 17 , 18).

if not exist, 16 replaced 0. check next array key, , until un output final replaced values.

thank help.

if wanted change parent 0 if not equal id, think simple code it:

foreach ($array $key => $val) {     // search if 'id' exists in array     $a_key = array_search($val['parent'], array_column($array, 'id'));     // check if 'id' in array, if not replace 'parent' 0     if ($a_key == false) $array[$key]['parent'] = '0'; } 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -