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
Post a Comment