douzi8916 2014-12-12 13:24
浏览 31
已采纳

PHP - 在多维数组中查找以前的值

I have this array:

$test = array( "a" => "b",
                "c" => array("foo" => "bar",
                                "3" => "4",
                            ),
              "e" => "f",); 

I want to create a function that finds the previous value in this array, given a certain value, for example find("bar", $test); should return "b".

This is what I got:

function find($needle, $array, $parent = NULL)
{
//moves the pointer until it reaches the desired value
  while (current($array) != $needle){
    //if current value is an array, apply this function recursively
    if (is_array(current($array))){
      $subarray = current($array);
    //passes the previous parent array 
      find($needle, $subarray, prev($array));
    }
    //once it reaches the end of the array, end the execution
    if(next($array) == FALSE){
      return;
    }
  }
  //once the pointer points to $needle, run find_prev()
  find_prev(prev($array), $parent);
}

function find_prev($prev, $parent = NULL)
{
  // in case there is no previous value in array and there is a superior level 
  if (!$prev && $parent) {
    find_prev($parent);
    return;
  }

  // in case previous value is an array 
  // find last value of that array

  if (is_array($prev) && $prev){
    find_prev(end($prev), $parent));
    return;
  } else {
  $GLOBALS['pre'] = $prev;
  }
}

For pedagogical reasons and since I have devoted some time to this function, it would be great if you could provide any hints about why this isn't working rather than any other simpler solution that you might have.

  • 写回答

2条回答 默认 最新

  • dongzhansong5785 2014-12-12 14:15
    关注

    You have an infinite loop. Your algorithm is a bit complicated for what you want to do. Maybe you should just keep the previous value in a variable and return it when you find the $needle. Here is the corresponding code. I tried to not modify your code as much as I could:

    function find($needle, $array, $lastValue = NULL)
    {
      $previousValue = null;
    
      //moves the pointer until it reaches the desired value
      while (current($array) != FALSE) {
        $value = current($array);
    
        //if current value is an array, apply this function recursively
        if (is_array($value)) {
          $subarray = $value;
          //passes the previous value as the last value for the embedded array 
          $value = find($needle, $subarray, $previousValue);
          if ($value !== NULL) {
            return $value;
          }
        } else if ($value === $needle) {
          //returns the previous value of the current array
          if ($previousValue !== NULL) {
            return $previousValue;
          //returns the last checked value of the parent array
          } else if ($lastValue !== NULL) {
            return $lastValue;
          } else {
            return;
          }
        } else {
          $previousValue = $value;
        }
    
        next($array);
      }
    }
    
    $test = array(
      "a" => "b",
      "c" => array(
        "foo" => "bar",
        "3" => "4"
      ),
      "e" => "f"
    );
    
    $result = find("bar", $test);
    
    if ($result === null) {
      print('no previous value');
    } else {
      $GLOBALS['pre'] = $result;
      print($GLOBALS['pre']);
    }
    

    You may try TDD to code this kind of algorithm. It could help you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误