各位大侠,请看如下代码:
我要实现的功能是,利用一个多维数组输出一个树状结构,下面的参数是多维数组。
//递归树状输出格式一
public function accountTreeType1($arrData){ $this->strLable = $this->strLable.'<ul>'; foreach($arrData as $val){ if(is_array($val['child'])){ $this->strLable = $this->strLable.'<li>'.$val['acc_code'].$val['acc_name']; $this->accountTreeType1($val['child']); }else{ $this->strLable = $this->strLable.'<li>'.$val['acc_code'].$val['acc_name'].'</li>'; if($val[id]=='最后一个ID'){ return $this->strLable; //在这里没有返回值,不过用echo $this->strLable;是可以打印出来,但是返回值为空。 } } } $this->strLable = $this->strLable.'</ul>'; }
回复讨论(解决方案)
第 9 行的 $this->accountTreeType1 没有承接第 14 行的返回
第 9 行的 $this->accountTreeType1 没有承接第 14 行的返回
那为什么echo 时,能打印呢
你是如何调用这个方法的?
function return了之后表示该方法已经运行完了,后面的代码都不会执行了。
$str = D('Account')->accountTreeType1($Data);
方法的最后加上
return $this->strLable;
1、class 里strLable定义了没?
2、return了 你不接收返回值你是要做什么?
3、echo 当然可以输出,你是不是没搞清楚什么是递归函数?
加了之后,有返回值了。但是不是很理解为什么这样写。
public function accountTreeType1($arrData){
$strLable .= '
- ';
- '.$val['acc_code'].$val['acc_name'].' ';
foreach($arrData as $val){
if(is_array($val['child'])){
$this->accountTreeType1($val['child']);
}else{
$strLable .= '
}
}
return $strLable.'
}
你都不return 怎么获取值
你都不return 怎么获取值
我刚刚代码有return。只是搞不明白,什么时候return。而且在方法最后面加return,那它递归,每次调用都会return不是吗?
$val[id],id是什么?应该是$id或者"id"吧...所以根本不会返回$this->strLabel
return 是跳出当前方法 你刚才其实调用了当前function好几次 你递归没搞懂
你最好搞懂
return back continue 和递归
public function accountTreeType1($arrData){
$strLable .= '
- ';
- '.$val['acc_code'].$val['acc_name'].' ';
- '.$val['acc_code'].$val['acc_name'].' ';
- 这个可以实现。谢谢你们,我有点明白,我错在哪了。
foreach($arrData as $val){
if(is_array($val['child'])){
$this->accountTre……
这个方法不能返回所有的值
$arrData 打印出来看看
Array
(
[1] => Array
(
[id] => 1
[acc_code] => 1001
[acc_name] => 库存现金
[acc_level] => 1
[acc_detail] => 0
[acc_parent_id] => 0
[acc_root_id] => 1
[acc_state] => 1
[child] => Array
(
[38] => Array
(
[id] => 38
[acc_code] => 100101
[acc_name] => 深圳现钞
[acc_level] => 2
[acc_detail] => 0
[acc_parent_id] => 1
[acc_root_id] => 1
[acc_state] => 1
[child] => Array
(
[39] => Array
(
[id] => 39
[acc_code] => 10010101
[acc_name] => 詹军涛
[acc_level] => 3
[acc_detail] => 0
[acc_parent_id] => 38
[acc_root_id] => 1
[acc_state] => 1
[child] => Array
(
[804] => Array
(
[id] => 804
[acc_code] => 1001010101
[acc_name] => fdsaf
[acc_level] => 4
[acc_detail] => 1
[acc_parent_id] => 39
[acc_root_id] => 1
[acc_state] => 1
)
)
)
[40] => Array
(
[id] => 40
[acc_code] => 10010102
[acc_name] => 林全茂
[acc_level] => 3
[acc_detail] => 1
[acc_parent_id] => 38
[acc_root_id] => 1
[acc_state] => 1
)
[41] => Array
(
[id] => 41
[acc_code] => 10010103
[acc_name] => 吴雪如
[acc_level] => 3
[acc_detail] => 1
[acc_parent_id] => 38
[acc_root_id] => 1
[acc_state] => 1
)
[42] => Array
(
[id] => 42
[acc_code] => 10010104
[acc_name] => 关蓉
[acc_level] => 3
[acc_detail] => 1
[acc_parent_id] => 38
[acc_root_id] => 1
[acc_state] => 1
)
[43] => Array
(
[id] => 43
[acc_code] => 10010105
[acc_name] => 胡振明
[acc_level] => 3
[acc_detail] => 1
[acc_parent_id] => 38
[acc_root_id] => 1
[acc_state] => 1
)
[44] => Array
(
[id] => 44
[acc_code] => 10010106
[acc_name] => 黄锦雄
[acc_level] => 3
[acc_detail] => 1
[acc_parent_id] => 38
[acc_root_id] => 1
[acc_state] => 1
)
[45] => Array
(
[id] => 45
[acc_code] => 10010107
[acc_name] => 陈培怀
[acc_level] => 3
[acc_detail] => 1
[acc_parent_id] => 38
[acc_root_id] => 1
[acc_state] => 1
)
[46] => Array
(
[id] => 46
[acc_code] => 10010108
[acc_name] => 陈镇江
[acc_level] => 3
[acc_detail] => 1
[acc_parent_id] => 38
[acc_root_id] => 1
[acc_state] => 1
)
[789] => Array
(
[id] => 789
[acc_code] => 435435
[acc_name] => fdsaf
[acc_level] => 3
[acc_detail] => 1
[acc_parent_id] => 38
[acc_root_id] => 1
[acc_state] => 1
)
)
)
[47] => Array
(
[id] => 47
[acc_code] => 100102
[acc_name] => 广州现钞
[acc_level] => 2
[acc_detail] => 0
[acc_parent_id] => 1
[acc_root_id] => 1
[acc_state] => 1
[child] => Array
(
[48] => Array
(
[id] => 48
[acc_code] => 10010201
[acc_name] => 吴昆伦
[acc_level] => 3
[acc_detail] => 1
[acc_parent_id] => 47
[acc_root_id] => 1
[acc_state] => 1
)
)
)
)
)
)
public function accountTreeType1($arrData){
$strLable .= '
- ';
foreach($arrData as $val){
if(is_array($val['child'])){
$strLable .= '
$strLable .= $this->accountTreeType1($val['child']);
}else{
$strLable .= '
}
}
return $strLable.'
}
public function accountTreeType1($arrData){
$strLable .= '
- ';
foreach($arrData as $val){
if(is_array($val['child'])){
$strLable .= '

여전히 인기있는 것은 사용 편의성, 유연성 및 강력한 생태계입니다. 1) 사용 편의성과 간단한 구문은 초보자에게 첫 번째 선택입니다. 2) 웹 개발, HTTP 요청 및 데이터베이스와의 우수한 상호 작용과 밀접하게 통합되었습니다. 3) 거대한 생태계는 풍부한 도구와 라이브러리를 제공합니다. 4) 활성 커뮤니티와 오픈 소스 자연은 새로운 요구와 기술 동향에 맞게 조정됩니다.

PHP와 Python은 웹 개발, 데이터 처리 및 자동화 작업에 널리 사용되는 고급 프로그래밍 언어입니다. 1.PHP는 종종 동적 웹 사이트 및 컨텐츠 관리 시스템을 구축하는 데 사용되며 Python은 종종 웹 프레임 워크 및 데이터 과학을 구축하는 데 사용됩니다. 2.PHP는 Echo를 사용하여 콘텐츠를 출력하고 Python은 인쇄를 사용합니다. 3. 객체 지향 프로그래밍을 지원하지만 구문과 키워드는 다릅니다. 4. PHP는 약한 유형 변환을 지원하는 반면, 파이썬은 더 엄격합니다. 5. PHP 성능 최적화에는 Opcache 및 비동기 프로그래밍 사용이 포함되며 Python은 Cprofile 및 비동기 프로그래밍을 사용합니다.

PHP는 주로 절차 적 프로그래밍이지만 객체 지향 프로그래밍 (OOP)도 지원합니다. Python은 OOP, 기능 및 절차 프로그래밍을 포함한 다양한 패러다임을 지원합니다. PHP는 웹 개발에 적합하며 Python은 데이터 분석 및 기계 학습과 같은 다양한 응용 프로그램에 적합합니다.

PHP는 1994 년에 시작되었으며 Rasmuslerdorf에 의해 개발되었습니다. 원래 웹 사이트 방문자를 추적하는 데 사용되었으며 점차 서버 측 스크립팅 언어로 진화했으며 웹 개발에 널리 사용되었습니다. Python은 1980 년대 후반 Guidovan Rossum에 의해 개발되었으며 1991 년에 처음 출시되었습니다. 코드 가독성과 단순성을 강조하며 과학 컴퓨팅, 데이터 분석 및 기타 분야에 적합합니다.

PHP는 웹 개발 및 빠른 프로토 타이핑에 적합하며 Python은 데이터 과학 및 기계 학습에 적합합니다. 1.PHP는 간단한 구문과 함께 동적 웹 개발에 사용되며 빠른 개발에 적합합니다. 2. Python은 간결한 구문을 가지고 있으며 여러 분야에 적합하며 강력한 라이브러리 생태계가 있습니다.

PHP는 현대화 프로세스에서 많은 웹 사이트 및 응용 프로그램을 지원하고 프레임 워크를 통해 개발 요구에 적응하기 때문에 여전히 중요합니다. 1.PHP7은 성능을 향상시키고 새로운 기능을 소개합니다. 2. Laravel, Symfony 및 Codeigniter와 같은 현대 프레임 워크는 개발을 단순화하고 코드 품질을 향상시킵니다. 3. 성능 최적화 및 모범 사례는 응용 프로그램 효율성을 더욱 향상시킵니다.

phphassignificallyimpactedwebdevelopmentandextendsbeyondit

PHP 유형은 코드 품질과 가독성을 향상시키기위한 프롬프트입니다. 1) 스칼라 유형 팁 : PHP7.0이므로 int, float 등과 같은 기능 매개 변수에 기본 데이터 유형을 지정할 수 있습니다. 2) 반환 유형 프롬프트 : 기능 반환 값 유형의 일관성을 확인하십시오. 3) Union 유형 프롬프트 : PHP8.0이므로 기능 매개 변수 또는 반환 값에 여러 유형을 지정할 수 있습니다. 4) Nullable 유형 프롬프트 : NULL 값을 포함하고 널 값을 반환 할 수있는 기능을 포함 할 수 있습니다.


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SecList
SecLists는 최고의 보안 테스터의 동반자입니다. 보안 평가 시 자주 사용되는 다양한 유형의 목록을 한 곳에 모아 놓은 것입니다. SecLists는 보안 테스터에게 필요할 수 있는 모든 목록을 편리하게 제공하여 보안 테스트를 더욱 효율적이고 생산적으로 만드는 데 도움이 됩니다. 목록 유형에는 사용자 이름, 비밀번호, URL, 퍼징 페이로드, 민감한 데이터 패턴, 웹 셸 등이 포함됩니다. 테스터는 이 저장소를 새로운 테스트 시스템으로 간단히 가져올 수 있으며 필요한 모든 유형의 목록에 액세스할 수 있습니다.

PhpStorm 맥 버전
최신(2018.2.1) 전문 PHP 통합 개발 도구

Atom Editor Mac 버전 다운로드
가장 인기 있는 오픈 소스 편집기

ZendStudio 13.5.1 맥
강력한 PHP 통합 개발 환경
