Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I've encountered the following problem:

private function getMyThemeIds($collection){
    $result = [];
    ...
      foreach ($results as $doc) {
        file_put_contents('2.txt', $doc->getUnid()); //everything is fine here

        $result[] = $doc->getUnid();

        file_put_contents('3.txt', print_r($result,true)); //again, array is just fine, barely 4000 entries
      }

    file_put_contents('4.txt', print_r($result,true)); // but here we see what was in this array right after initialization. Nothing in this case.
    return $result;
  }

I've tried different approaches - changed foreach to for, $result[] to array_push, etc with no avail. Anybody knows what the cause of this may be?

share|improve this question
    
where id your $results ?? – Rahautos Oct 21 at 6:19
    
Not sure but you can use like $result = array(); for $result = []; while declaration.. – Maha Dev Oct 21 at 6:22
    
What is your php version? If you are below php-5.4 than change $result = []; to $result = array(); – Chayan Oct 21 at 6:24
    
Does $doc->getUnid(); prints proper output? It must not return empty. – sandeepsure Oct 21 at 6:27
    
@Rahautos, beyond that "..." – Moorindal Oct 21 at 6:27

2 Answers 2

up vote 2 down vote accepted

You can initialize array using 'array()'.Please follow below statment to initialize array

$result = array();

After initialize $result you can append data to it. You can refer following link for array initialization- http://www.w3schools.com/php/func_array.asp

share|improve this answer
    
Variable was initialized with $result = []; as of php-5.4 – Chayan Oct 21 at 6:43
    
Somehow... It worked. I can swear I already tried this! Well, thanks everybody. – Moorindal Oct 21 at 6:47
    
array_push($result,$doc->getUnid()); – WisdmLabs Oct 21 at 6:47
    
@Moorindal Just curious to know, what is your php version? – Silent_Coder Oct 21 at 6:49
    
I am using PHP 5 – WisdmLabs Oct 21 at 6:51

Please see the file_put_contents for details. You can try this.

private function getMyThemeIds($collection){
    $result = array();
    ...
      foreach ($results as $doc) {
        file_put_contents('2.txt', $doc->getUnid()); //everything is fine here

        $result[] = $doc->getUnid();

        file_put_contents('3.txt', serialize($result)); //again, array is just fine, barely 4000 entries
      }

    file_put_contents('4.txt', serialize($result)); // but here we see what was in this array right after initialization. Nothing in this case.
    return $result;
  }
share|improve this answer

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.