亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在PHP遞歸函數中返回

在PHP遞歸函數中返回

PHP
婷婷同學_ 2023-04-02 10:23:07
我正在編寫一個 PHP 遞歸函數來使用它們的值從數組中獲取數據。所以這是我要構建的功能:  function test($menu) {    $url = "test.com/accounts/overview/";    foreach($menu as $data) {      if( is_array($data) and array_key_exists("t_link", $data) and $data["t_link"] === $url ) {        return $data["t_icon"];       } else if(is_array($data)) {        test($data);      }    }  }echo test($menu);從我的數組開始,第一個條件只有一次為真,并且必須return返回值并終止函數,不是嗎?但為什么它沒有返回?另外,如果我使用echo $data["t_icon"];而不是return $data["t_icon"];它顯示正確的結果:fa fa-book。這是我試圖t_icon根據值獲取值的數組t_link。條件是如果t_link值有test.com/accounts/overview/那么它將返回fa fa-book$menu = array ();$menu["Dashboard"] = array (    "t_link"    => "test.com",    "t_icon"    => "fa fa-dashboard"  );  $menu["Accounts"] = array (    "t_link"    => "test.com/accounts",    "t_icon"    => "fa fa-books"  );  $menu["Accounts"]["Overview"] = array (    "t_link"    => "test.com/accounts/overview/",    "t_icon"    => "fa fa-book"  <<-- This value I want to get  );我搜索了很多并得到這個我應該像這樣在第二個條件內返回函數return test($data);。但它也不起作用。謝謝。
查看完整描述

2 回答

?
慕標5832272

TA貢獻1966條經驗 獲得超4個贊

您的代碼不起作用,因為函數應該始終具有返回值 - 在遞歸中它是必需的:


function test($menu) {


    $url = "test.com/accounts/overview/";


    foreach($menu as $data) {


      if( is_array($data) && 

      isset($data["t_link"]) && 

      $data["t_link"] === $url ) {        

          return $data["t_icon"]; 

      } 

      else if (is_array($data)) {

          $result = test($data); //Get result back from function


          //If it's NOT NULL call the function again (test($data))

          //

          //(down below returns null when looped through 

          //everything recursively)

          //

          if ($result !== null) { 

              return $result;

          }

      }


    }


    return null;

}

這是實現您想要的 OOP 風格的另一種方法:


該解決方案背后的想法是創建一個兩級數組(不多也不少)并從該新數組中獲取數據。


class Searcher {

    private $new_arr = array();

    private $icon = '';


    //array_walk_recursive goes through your array recursively

    //and calls the getdata-method in the class. This method creates

    //a new array with strings (not arrays) from supplied $array ($menu in your case)

    public function __construct($array, $search_url) {

        array_walk_recursive($array, array($this, 'getdata'));     

        $key = array_search($search_url, $this->new_arr['t_link']);

        $this->icon = $this->new_arr['t_icon'][$key];

    }


    public function geticon() {

        return $this->icon;

    }


    public function getdata($item, $key) {

        if (!is_array($item)) {

            $this->new_arr[$key][] = $item;    

        }        

    }


}


//Implementation (usage) of above class

$search = new Searcher($menu, 'test.com/accounts/overview/');

$icon = $search->geticon();

echo 'ICON=' . $icon; //Would echo out 'fa fa-book'

進一步說明:


基于你的$menu數組,該類將創建一個像這樣的數組($this->new_arr):


Array

(

    [t_link] => Array

        (

            [0] => test.com

            [1] => test.com/accounts

            [2] => test.com/accounts/overview/

        )


    [t_icon] => Array

        (

            [0] => fa fa-dashboard

            [1] => fa fa-books

            [2] => fa fa-book

        )


)

新數組中的所有鍵都相互關聯。這是$this->new_arr[$key][] = $item; 在getdata()方法中設置的。這是基于這樣一個事實,即數組中的t_link-keys 和-keys的數量必須相等。t_icon


因為這:


$this->new_arr[0]['t_link'] is related to $this->new_arr[0]['t_icon'];

$this->new_arr[1]['t_link'] is related to $this->new_arr[1]['t_icon'];

$this->new_arr[2]['t_link'] is related to $this->new_arr[2]['t_icon'];

etc.. (not more in your example)

有此代碼時:


$key = array_search($search_url, $this->new_arr['t_link']);

如果您已提供給test.com/accounts/overview/,它將提供密鑰2$search_url


所以:


$this->icon = $this->new_arr['t_icon'][$key];

設置為fa fa-book


查看完整回答
反對 回復 2023-04-02
?
慕桂英3389331

TA貢獻2036條經驗 獲得超8個贊

因為你在函數內部調用函數,你必須返回函數的值,這里是修復:(在 test($data); 之前添加“return”)


 function test($menu) {


    $url = "test.com/accounts/overview/";


    foreach($menu as $data) {


      if( is_array($data) and array_key_exists("t_link", $data) and $data["t_link"] == $url ) {

        return $data["t_icon"]; 

      } else if(isset($data["Overview"])) {

        return test($data);

      }

    }

  }

此外,所有這三種情況,它們都是數組,因此return test($data);將停止 foreach 然后$menu["Accounts"]["Overview"]永遠不會被檢查。


查看完整回答
反對 回復 2023-04-02
  • 2 回答
  • 0 關注
  • 171 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號