How do I "global" an array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • moishy
    New Member
    • Oct 2006
    • 104

    #1

    How do I "global" an array?

    I have a function which adds strings to an array, but outside the function it didn't add the string to the array.

    Example:

    [PHP]function AddStringToArra y($name,$string ) {

    $theArray[$name] = $string;

    print "inside the function: ".$theArray[$name]."<p>";
    }

    AddStringToArra y('abc','hello world!');

    print "outside the function: ".$theArray['abc'];

    {[/PHP]

    The above will result:

    inside the function: hello world!

    outside the function:
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    As follows;
    [php]<?php
    $theArray = array();
    function AddStringToArra y($name,$string ) {
    global $theArray;
    $theArray[$name] = $string;
    print "inside the function: ".$theArray[$name]."<p>";
    }
    AddStringToArra y('abc','hello world!');
    print "outside the function: ".$theArray['abc'];
    ?>[/php]
    Ronald :cool:

    Comment

    • Velhari
      New Member
      • Sep 2006
      • 46

      #3
      Originally posted by moishy
      I have a function which adds strings to an array, but outside the function it didn't add the string to the array.

      Example:

      [PHP]function AddStringToArra y($name,$string ) {

      $theArray[$name] = $string;

      print "inside the function: ".$theArray[$name]."<p>";
      }

      AddStringToArra y('abc','hello world!');

      print "outside the function: ".$theArray['abc'];

      {[/PHP]

      The above will result:

      inside the function: hello world!

      outside the function:
      Hi,
      You can access the arrays from outside the function by declaring the array as global.

      I changed your code as follows to rectify your problem.... you can use this code to acheive your goal....

      [PHP]function AddStringToArra y($name,$string )
      {
      global $thestring;
      $thestring[$name]=$string;
      echo "Inside Function:".$the string[$name]."<br>";
      }
      AddStringToArra y('abc','hello world');
      echo "Outside Function:".$the string['abc'];[/PHP]

      With Regards,
      Velmurugan.H

      Comment

      • moishy
        New Member
        • Oct 2006
        • 104

        #4
        But it doesn't work if I call from another function:

        [PHP]
        // Whatever was said before...

        function LoopArray() {

        foreach ($theArray as $value){
        // Do whatever...
        }
        [/PHP]

        will output:
        Warning: Invalid argument supplied for foreach()

        -even though i GLOBALed the array!

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          Yes it does! You must declare the array in your main program and declare it global in all functions that use it.

          Ronald :cool:

          Comment

          Working...