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 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!