2

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?

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

2 Answers 2

2

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

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

5 Comments

Variable was initialized with $result = []; as of php-5.4
Somehow... It worked. I can swear I already tried this! Well, thanks everybody.
array_push($result,$doc->getUnid());
@Moorindal Just curious to know, what is your php version?
@Silent_Coder, 5.5.9-1ubuntu4.5 (cli) (built: Oct 29 2014 11:59:10)
0

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;
  }

Comments

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.