0

I have this code in php.

 public function display_children($parent,$level){
 try {
          $cmd = $this->connection->prepare('SELECT mem,pid from mytree where pid = ?');

          $cmd->execute(array($parent));



          while ( $row =  $cmd->fetch(PDO::FETCH_ASSOC)) {


               $rec[] = [['v' => $row['mem'], 'f' => $row['mem']], (string)$row['pid'], $row['mem']];


              $this->display_children($row['mem'], $level + 1);

          }

               echo json_encode(rec);
      }
      catch(PDOException $ex){
          return $ex->getMessage();
      }

  }

And this is the result in my ajax

[][][[{"v":"9","f":"9"},"7","9"],[{"v":"10","f":"10"},"7","10"]][][[{"v":"7","f":"7"},"5","7"],[{"v":"8","f":"8"},"5","8"]]

I want to remove those empty array to be like this.is it possible ?

[[{"v":"9","f":"9"},"7","9"],[{"v":"10","f":"10"},"7","10"],[{"v":"7","f":"7"},"5","7"],[{"v":"8","f":"8"},"5","8"]]

I tried to use this to remove the empty array but it failed to remove.

 $rec = array_filter($rec);

Thank you in advance.

1
  • I don't see anything in your code that would create those empty elements at the beginning of the array. Commented Dec 15, 2014 at 6:36

2 Answers 2

2

you can use array_filter to do this.

function filter($var){
  return !empty($var);
}

$array1 = array("a"=>null, "b"=>2, "c"=>3, "d"=>4, "e"=>5);

$newarray = array_filter($array1, "filter"));

result: Array ( [b] => 2 [c] => 3 [d] => 4 [e] => 5 )

Sign up to request clarification or add additional context in comments.

Comments

1

Try something like this:

public function display_children($parent,$level, $rec = array()){
    try {
        $cmd = $this->connection->prepare('SELECT mem,pid from mytree where pid = ?');

        $cmd->execute(array($parent));

        while ( $row =  $cmd->fetch(PDO::FETCH_ASSOC)) {

            $rec[] = [['v' => $row['mem'], 'f' => $row['mem']], (string)$row['pid'], $row['mem']];

            $rec = $this->display_children($row['mem'], $level + 1, $rec);
        }
    }
    catch(PDOException $ex){
        //return $ex->getMessage();
        return $rec;
    }

    return $rec;
}

First call:

echo json_encode(display_children(5, 0));

11 Comments

this cannot work, cause as soon as the while loop passes only once $exists is true
'echo json_encode' starts after the loop in topic. so it will display empty array in all cases. so we need to skip 'echo json_encode' if loop body is skipped.
almost got it [[{"v":"9","f":"9"},"7","9"],[{"v":"10","f":"10"},"7","10"]][[{"v":"7","f":"7"},"5","7"],[{"v":"8","f":"8"},"5","8"]] but there is ][ near 10,it should like this [[{"v":"9","f":"9"},"7","9"],[{"v":"10","f":"10"},"7","10"],[{"v":"7","f":"7"},"5","7"],[{"v":"8","f":"8"},"5","8"]]
yes, but if i understand the question correctly the filds form the db can be empty and this results in empty array parts. You only checking if is ther any result from the db. And for this you can simply use if(!empty($rec)) instead of using a variable
I have added "if ($level > 1) echo ',';" to the answer if you use $level = 0 in first call. Please check.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.