Heim >Backend-Entwicklung >PHP-Tutorial >如何搜索多维数组中指定键名,然后返回关联父级

如何搜索多维数组中指定键名,然后返回关联父级

WBOY
WBOYOriginal
2016-06-23 13:16:23989Durchsuche

$arr = array (  1 =>   array (    'id' => '1',    'pid' => '0',    'token' => '1b0d714c66096a96c482936486e4c40e',    'sons' =>     array (      12 =>       array (        'id' => '12',        'pid' => '1',        'token' => 'a7dcfa46460ca637fa286c14ee57acdf',      ),      6 =>       array (        'id' => '6',        'pid' => '1',        'token' => '2e7518ec4f5289bff8f3e05116c4929e',        'sons' =>         array (          19 =>           array (            'id' => '19',            'pid' => '6',            'token' => '858713efb349f93af67dadb49b8f6b5f',          ),          20 =>           array (            'id' => '20',            'pid' => '6',            'token' => '7363f6aabd2b3dcf50ccbe88f3c5a8b2',          ),          21 =>           array (            'id' => '21',            'pid' => '6',            'token' => '8a986418ee5c8f713df6e9604005e69f',          ),          22 =>           array (            'id' => '22',            'pid' => '6',            'token' => '6311e80bcbe0d6a85675a31a608c1915',          ),          23 =>           array (            'id' => '23',            'pid' => '6',            'token' => '2624675dba4e5a337c2439e46e3e72e7',          ),          24 =>           array (            'id' => '24',            'pid' => '6',            'token' => '54cb492d5dd42153d4582a477d50801f',          ),        ),      ),      7 =>       array (        'id' => '7',        'pid' => '1',        'token' => 'e805b7b1c80fabb3254a811b03d863a9',      ),      8 =>       array (        'id' => '8',        'pid' => '1',        'token' => 'fa19fc35fc606ea8ee42220988a1dd13',      ),      9 =>       array (        'id' => '9',        'pid' => '1',        'token' => '7b719ab0aca16f75f7ff7d343b636b6b',      ),      10 =>       array (        'id' => '10',        'pid' => '1',        'token' => 'da54b292552224a177e98caa7343bfc4',      ),      11 =>       array (        'id' => '11',        'pid' => '1',        'token' => '05af12b7314e56ec1152df8755f2adad',      ),    ),  ),  2 =>   array (    'id' => '2',    'pid' => '0',    'token' => '22f0f784a4624e34f99464756edfa96d',  ),  3 =>   array (    'id' => '3',    'pid' => '0',    'token' => '35a86d0851e0a58e7586d21e2f35b542',  ),  4 =>   array (    'id' => '4',    'pid' => '0',    'token' => 'd6010381df2741f5fef54f0715bfab9e',  ),  5 =>   array (    'id' => '5',    'pid' => '0',    'token' => '1b0d79a0b84656aee02143b64e1c06cc',    'sons' =>     array (      14 =>       array (        'id' => '14',        'pid' => '5',        'token' => 'eb4595eb1f17a7135367b0d637a095ae',      ),      15 =>       array (        'id' => '15',        'pid' => '5',        'token' => '02622648a1c23d56a1c8d114897841e1',      ),      16 =>       array (        'id' => '16',        'pid' => '5',        'token' => '2ed8c0684e0bc2f6ad7c44c45c0e7008',      ),      17 =>       array (        'id' => '17',        'pid' => '5',        'token' => '3b3376d825dbb47e79b93972df61d87e',      ),      18 =>       array (        'id' => '18',        'pid' => '5',        'token' => '1acdddb596cdcfa1b22778b5fb04d958',      ),      13 =>       array (        'id' => '13',        'pid' => '5',        'token' => '7719c663ccd503f6de4bf039c7e7ddf6',      ),    ),  ),);



多维数组中怎么搜索指定键名,然后返回关联父级

如:我搜索 token = 7719c663ccd503f6de4bf039c7e7ddf6

则:返回
$arr[5][sons][13]=array(	id => '13',	pid => '5',	token => '7719c663ccd503f6de4bf039c7e7ddf6',);


回复讨论(解决方案)

$arr = array (  1 =>   array (    'id' => '1',    'pid' => '0',    'token' => '1b0d714c66096a96c482936486e4c40e',    'sons' =>     array (      12 =>       array (        'id' => '12',        'pid' => '1',        'token' => 'a7dcfa46460ca637fa286c14ee57acdf',      ),      6 =>       array (        'id' => '6',        'pid' => '1',        'token' => '2e7518ec4f5289bff8f3e05116c4929e',        'sons' =>         array (          19 =>           array (            'id' => '19',            'pid' => '6',            'token' => '858713efb349f93af67dadb49b8f6b5f',          ),          20 =>           array (            'id' => '20',            'pid' => '6',            'token' => '7363f6aabd2b3dcf50ccbe88f3c5a8b2',          ),          21 =>           array (            'id' => '21',            'pid' => '6',            'token' => '8a986418ee5c8f713df6e9604005e69f',          ),          22 =>           array (            'id' => '22',            'pid' => '6',            'token' => '6311e80bcbe0d6a85675a31a608c1915',          ),          23 =>           array (            'id' => '23',            'pid' => '6',            'token' => '2624675dba4e5a337c2439e46e3e72e7',          ),          24 =>           array (            'id' => '24',            'pid' => '6',            'token' => '54cb492d5dd42153d4582a477d50801f',          ),        ),      ),      7 =>       array (        'id' => '7',        'pid' => '1',        'token' => 'e805b7b1c80fabb3254a811b03d863a9',      ),      8 =>       array (        'id' => '8',        'pid' => '1',        'token' => 'fa19fc35fc606ea8ee42220988a1dd13',      ),      9 =>       array (        'id' => '9',        'pid' => '1',        'token' => '7b719ab0aca16f75f7ff7d343b636b6b',      ),      10 =>       array (        'id' => '10',        'pid' => '1',        'token' => 'da54b292552224a177e98caa7343bfc4',      ),      11 =>       array (        'id' => '11',        'pid' => '1',        'token' => '05af12b7314e56ec1152df8755f2adad',      ),    ),  ),  2 =>   array (    'id' => '2',    'pid' => '0',    'token' => '22f0f784a4624e34f99464756edfa96d',  ),  3 =>   array (    'id' => '3',    'pid' => '0',    'token' => '35a86d0851e0a58e7586d21e2f35b542',  ),  4 =>   array (    'id' => '4',    'pid' => '0',    'token' => 'd6010381df2741f5fef54f0715bfab9e',  ),  5 =>   array (    'id' => '5',    'pid' => '0',    'token' => '1b0d79a0b84656aee02143b64e1c06cc',    'sons' =>     array (      14 =>       array (        'id' => '14',        'pid' => '5',        'token' => 'eb4595eb1f17a7135367b0d637a095ae',      ),      15 =>       array (        'id' => '15',        'pid' => '5',        'token' => '02622648a1c23d56a1c8d114897841e1',      ),      16 =>       array (        'id' => '16',        'pid' => '5',        'token' => '2ed8c0684e0bc2f6ad7c44c45c0e7008',      ),      17 =>       array (        'id' => '17',        'pid' => '5',        'token' => '3b3376d825dbb47e79b93972df61d87e',      ),      18 =>       array (        'id' => '18',        'pid' => '5',        'token' => '1acdddb596cdcfa1b22778b5fb04d958',      ),      13 =>       array (        'id' => '13',        'pid' => '5',        'token' => '7719c663ccd503f6de4bf039c7e7ddf6',      ),    ),  ),);$token = '7719c663ccd503f6de4bf039c7e7ddf6';foreach($arr as $k=>$v) {  if(isset($v['sons'])) {    foreach($v['sons'] as $k1=>$v1) {      if($v1['token'] == $token) $ret[$k]['sons'][$k1] = $v1;    }  }}print_r($ret);
Array(    [5] => Array        (            [sons] => Array                (                    [13] => Array                        (                            [id] => 13                            [pid] => 5                            [token] => 7719c663ccd503f6de4bf039c7e7ddf6                        )                )        ))

$arr = array (  1 =>   array (    'id' => '1',    'pid' => '0',    'token' => '1b0d714c66096a96c482936486e4c40e',    'sons' =>     array (      12 =>       array (        'id' => '12',        'pid' => '1',        'token' => 'a7dcfa46460ca637fa286c14ee57acdf',      ),      6 =>       array (        'id' => '6',        'pid' => '1',        'token' => '2e7518ec4f5289bff8f3e05116c4929e',        'sons' =>         array (          19 =>           array (            'id' => '19',            'pid' => '6',            'token' => '858713efb349f93af67dadb49b8f6b5f',          ),          20 =>           array (            'id' => '20',            'pid' => '6',            'token' => '7363f6aabd2b3dcf50ccbe88f3c5a8b2',          ),          21 =>           array (            'id' => '21',            'pid' => '6',            'token' => '8a986418ee5c8f713df6e9604005e69f',          ),          22 =>           array (            'id' => '22',            'pid' => '6',            'token' => '6311e80bcbe0d6a85675a31a608c1915',          ),          23 =>           array (            'id' => '23',            'pid' => '6',            'token' => '2624675dba4e5a337c2439e46e3e72e7',          ),          24 =>           array (            'id' => '24',            'pid' => '6',            'token' => '54cb492d5dd42153d4582a477d50801f',          ),        ),      ),      7 =>       array (        'id' => '7',        'pid' => '1',        'token' => 'e805b7b1c80fabb3254a811b03d863a9',      ),      8 =>       array (        'id' => '8',        'pid' => '1',        'token' => 'fa19fc35fc606ea8ee42220988a1dd13',      ),      9 =>       array (        'id' => '9',        'pid' => '1',        'token' => '7b719ab0aca16f75f7ff7d343b636b6b',      ),      10 =>       array (        'id' => '10',        'pid' => '1',        'token' => 'da54b292552224a177e98caa7343bfc4',      ),      11 =>       array (        'id' => '11',        'pid' => '1',        'token' => '05af12b7314e56ec1152df8755f2adad',      ),    ),  ),  2 =>   array (    'id' => '2',    'pid' => '0',    'token' => '22f0f784a4624e34f99464756edfa96d',  ),  3 =>   array (    'id' => '3',    'pid' => '0',    'token' => '35a86d0851e0a58e7586d21e2f35b542',  ),  4 =>   array (    'id' => '4',    'pid' => '0',    'token' => 'd6010381df2741f5fef54f0715bfab9e',  ),  5 =>   array (    'id' => '5',    'pid' => '0',    'token' => '1b0d79a0b84656aee02143b64e1c06cc',    'sons' =>     array (      14 =>       array (        'id' => '14',        'pid' => '5',        'token' => 'eb4595eb1f17a7135367b0d637a095ae',      ),      15 =>       array (        'id' => '15',        'pid' => '5',        'token' => '02622648a1c23d56a1c8d114897841e1',      ),      16 =>       array (        'id' => '16',        'pid' => '5',        'token' => '2ed8c0684e0bc2f6ad7c44c45c0e7008',      ),      17 =>       array (        'id' => '17',        'pid' => '5',        'token' => '3b3376d825dbb47e79b93972df61d87e',      ),      18 =>       array (        'id' => '18',        'pid' => '5',        'token' => '1acdddb596cdcfa1b22778b5fb04d958',      ),      13 =>       array (        'id' => '13',        'pid' => '5',        'token' => '7719c663ccd503f6de4bf039c7e7ddf6',      ),    ),  ),);



多维数组中怎么搜索指定键名,然后返回关联父级

如:我搜索 token = 7719c663ccd503f6de4bf039c7e7ddf6

则:返回
$arr[5][sons][13]=array(	id => '13',	pid => '5',	token => '7719c663ccd503f6de4bf039c7e7ddf6',);




我搜索 token = 858713efb349f93af67dadb49b8f6b5f  搜索不到

$arr = array (  1 =>   array (    'id' => '1',    'pid' => '0',    'token' => '1b0d714c66096a96c482936486e4c40e',    'sons' =>     array (      12 =>       array (        'id' => '12',        'pid' => '1',        'token' => 'a7dcfa46460ca637fa286c14ee57acdf',      ),      6 =>       array (        'id' => '6',        'pid' => '1',        'token' => '2e7518ec4f5289bff8f3e05116c4929e',        'sons' =>         array (          19 =>           array (            'id' => '19',            'pid' => '6',            'token' => '858713efb349f93af67dadb49b8f6b5f',          ),          20 =>           array (            'id' => '20',            'pid' => '6',            'token' => '7363f6aabd2b3dcf50ccbe88f3c5a8b2',          ),          21 =>           array (            'id' => '21',            'pid' => '6',            'token' => '8a986418ee5c8f713df6e9604005e69f',          ),          22 =>           array (            'id' => '22',            'pid' => '6',            'token' => '6311e80bcbe0d6a85675a31a608c1915',          ),          23 =>           array (            'id' => '23',            'pid' => '6',            'token' => '2624675dba4e5a337c2439e46e3e72e7',          ),          24 =>           array (            'id' => '24',            'pid' => '6',            'token' => '54cb492d5dd42153d4582a477d50801f',          ),        ),      ),      7 =>       array (        'id' => '7',        'pid' => '1',        'token' => 'e805b7b1c80fabb3254a811b03d863a9',      ),      8 =>       array (        'id' => '8',        'pid' => '1',        'token' => 'fa19fc35fc606ea8ee42220988a1dd13',      ),      9 =>       array (        'id' => '9',        'pid' => '1',        'token' => '7b719ab0aca16f75f7ff7d343b636b6b',      ),      10 =>       array (        'id' => '10',        'pid' => '1',        'token' => 'da54b292552224a177e98caa7343bfc4',      ),      11 =>       array (        'id' => '11',        'pid' => '1',        'token' => '05af12b7314e56ec1152df8755f2adad',      ),    ),  ),  2 =>   array (    'id' => '2',    'pid' => '0',    'token' => '22f0f784a4624e34f99464756edfa96d',  ),  3 =>   array (    'id' => '3',    'pid' => '0',    'token' => '35a86d0851e0a58e7586d21e2f35b542',  ),  4 =>   array (    'id' => '4',    'pid' => '0',    'token' => 'd6010381df2741f5fef54f0715bfab9e',  ),  5 =>   array (    'id' => '5',    'pid' => '0',    'token' => '1b0d79a0b84656aee02143b64e1c06cc',    'sons' =>     array (      14 =>       array (        'id' => '14',        'pid' => '5',        'token' => 'eb4595eb1f17a7135367b0d637a095ae',      ),      15 =>       array (        'id' => '15',        'pid' => '5',        'token' => '02622648a1c23d56a1c8d114897841e1',      ),      16 =>       array (        'id' => '16',        'pid' => '5',        'token' => '2ed8c0684e0bc2f6ad7c44c45c0e7008',      ),      17 =>       array (        'id' => '17',        'pid' => '5',        'token' => '3b3376d825dbb47e79b93972df61d87e',      ),      18 =>       array (        'id' => '18',        'pid' => '5',        'token' => '1acdddb596cdcfa1b22778b5fb04d958',      ),      13 =>       array (        'id' => '13',        'pid' => '5',        'token' => '7719c663ccd503f6de4bf039c7e7ddf6',      ),    ),  ),);$token = '7719c663ccd503f6de4bf039c7e7ddf6';print_r(foo($arr, $token));$token = '858713efb349f93af67dadb49b8f6b5f';print_r(foo($arr, $token));function foo($arr, $token) {  $ret = array();  foreach($arr as $k=>$v) {    if(isset($v['token']) && $v['token'] == $token) $ret[$k] = $v;    if(isset($v['sons'])) {      $t = foo($v['sons'], $token);      if($t) $ret[$k]['sons'] = $t;    }  }  return $ret;}
Array(    [5] => Array        (            [sons] => Array                (                    [13] => Array                        (                            [id] => 13                            [pid] => 5                            [token] => 7719c663ccd503f6de4bf039c7e7ddf6                        )                )        ))Array(    [1] => Array        (            [sons] => Array                (                    [6] => Array                        (                            [sons] => Array                                (                                    [19] => Array                                        (                                            [id] => 19                                            [pid] => 6                                            [token] => 858713efb349f93af67dadb49b8f6b5f                                        )                                )                        )                )        ))

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn