> 백엔드 개발 > PHP 튜토리얼 > 如何搜索多维数组中指定键名,然后返回关联父级

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

WBOY
풀어 주다: 2016-06-23 13:16:23
원래의
996명이 탐색했습니다.

$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                                        )                                )                        )                )        ))
로그인 후 복사

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿