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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?