Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a function, and it returns an array.

class myarray
{
 public function getAr()
   {
      // mysql query
      while($dd= $database->fetch(PDO::FETCH_ASSOC))
           {
               $data[] = $dd; //there's values in the array
           }
               return $data;
   }
 public function get3()
   {
      // mysl query
      while($dd= $database->fetch(PDO::FETCH_ASSOC))
           {
               $data[] = $dd; //there's values in the array
           }
               return $data;
   }    

}

How come I tried to merge together the array:

$get = new myarray();
$arrayAr = $get->getAr();
$array3 = $get->get3();
$new_array = array_merge($arrayAr ,$array3);

It says that its not an array?

array_merge() [function.array-merge]: Argument #1 is not an array 

But I can print_r($arrayAr); and its like an array?

Help?

Thanks

share|improve this question
    
I don't think you'd really name your class array - that could cause some confusion... Is this the code you are using or some pseudo code to simplify the problem? –  Lix Sep 3 '12 at 5:35
    
$d gets out of nowhere too. –  Shikiryu Sep 3 '12 at 5:36
1  
I'm not even sure how you could run this the class array should give you a PHP Parse error. But besides that your code returns an array containing NULL two times. Which is to be expected since $d, like @Shikiryu stated, comes out of nothing. –  Michael Sep 3 '12 at 5:40

2 Answers 2

array is the name of a builtin; you must change the name of your class. Additionally, you should define $d (live demo):

<?php
class MyArray {
  public function getAr() {
    $d = 42;
    $data = array($d);
    return $data;
  }
  public function get3() {
    $d = 43;
    $data = array($d);
    return $data;
  }
}
$get = new MyArray();
$arrayAr = $get->getAr();
$array3 = $get->get3();
var_export(array_merge($arrayAr ,$array3)); // [0 => 42, 1 => 43]

In response to the edited version: What if the query doesn't return anything? And note you $database is undefined. Moreover, it is not actually a database, but a PDOStatement object. Assuming that it is in fact a valid PDO object, you should simply use fetchAll:

class MyArray {
  protected $statement;
  public function query($pdo, $sql) {
    $this->statement = $pdo->query($sql);
  }

  // Don't forget to call query before this method.
  public function getAr() {
    return $this->statement->fetchAll(PDO::FETCH_ASSOC);
  }
}
share|improve this answer
    
I need to have $data[] instead of array($d); there's supposed to have populated values in there, I just left it blank for simplicity –  andrewliu Sep 3 '12 at 5:51
    
@andrewliu Well, I can't regard code or requirements you haven't included in the question. But sure, if you define $data right after $d, you can use $data[] = $d;. –  phihag Sep 3 '12 at 5:54
    
I edited my OP, I wonder if it makes sense what my problem is now? –  andrewliu Sep 3 '12 at 5:56
    
@andrewliu In the future, please don't make drastic edits to your question. That will leave all future visitors confused. Instead, simply ask a new question (and maybe link to it in the comments or so). Your problem is unrelated to array_merge. Instead, your query either returns an empty result or $database is not, well, a database (calling fetch strongly suggests so). I have amended this answer. –  phihag Sep 3 '12 at 7:50

It seems that the problem is that you are using a saved name for your class. You really shouldn't use keywords with other meanings in the language as variable names. Just like you wouldn't call a variable array_merge because there is already a function called that...

This code seems to work for me -

class customArray{
  public function getAr(){
    $data[] = 'Stack';
    return $data;
  }

  public function get3(){
    $data[] = 'Overflow';
    return $data;
  } 
}

$get = new customArray();
$arrayAr = $get->getAr();
$array3 = $get->get3();
print_r(array_merge($arrayAr ,$array3))

Outputs -

Array
(
    [0] => Stack
    [1] => Overflow
) 

However if I use the class name array, I'll get this error -

PHP Parse error: syntax error, unexpected T_ARRAY, expecting T_STRING...

share|improve this answer
    
sorry, I guess I should've used other pseudo code names... –  andrewliu Sep 3 '12 at 5:50
    
I edited my OP, if it makes sense now? –  andrewliu Sep 3 '12 at 5:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.