$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 ) ) ) ) ))

Laravel使用其直观的闪存方法简化了处理临时会话数据。这非常适合在您的应用程序中显示简短的消息,警报或通知。 默认情况下,数据仅针对后续请求: $请求 -

PHP客户端URL(curl)扩展是开发人员的强大工具,可以与远程服务器和REST API无缝交互。通过利用Libcurl(备受尊敬的多协议文件传输库),PHP curl促进了有效的执行

Laravel 提供简洁的 HTTP 响应模拟语法,简化了 HTTP 交互测试。这种方法显着减少了代码冗余,同时使您的测试模拟更直观。 基本实现提供了多种响应类型快捷方式: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

您是否想为客户最紧迫的问题提供实时的即时解决方案? 实时聊天使您可以与客户进行实时对话,并立即解决他们的问题。它允许您为您的自定义提供更快的服务

Laravel框架的Storage::download方法提供了一个简洁的API,用于安全地处理文件下载,同时管理文件存储的抽象。 以下是一个在示例控制器中使用Storage::download()的例子:

文章讨论了PHP 5.3中引入的PHP中的晚期静态结合(LSB),从而允许静态方法的运行时分辨率调用以获得更灵活的继承。 LSB的实用应用和潜在的触摸

PHP日志记录对于监视和调试Web应用程序以及捕获关键事件,错误和运行时行为至关重要。它为系统性能提供了宝贵的见解,有助于识别问题并支持更快的故障排除

Laravel的服务容器和服务提供商是其架构的基础。 本文探讨了服务容器,详细信息服务提供商创建,注册,并通过示例演示了实际用法。 我们将从OVE开始


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3汉化版
中文版,非常好用

WebStorm Mac版
好用的JavaScript开发工具

禅工作室 13.0.1
功能强大的PHP集成开发环境

SublimeText3 Linux新版
SublimeText3 Linux最新版

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。