Bor
Bor

Reputation: 793

Global array in php issues

I've found some very helpful answers but still have some problems.

I want to put different rows in global array, WITHOUT removing the other rows.

<?php

global $global_arr;

function first() {  
        ///some code
        global $global_arr; 

        $global_arr[] = array('first' => 
             array('1' , '1', '1'));


}

function second() { 
        ///some code
        global $global_arr; 

        $global_arr[] = array('second' => 
             array('2' , '2', '2'));

}
function third() { 
        ///some code
        global $global_arr; 

        $global_arr[] = array('third' => 
             array('3' , '3', '3'));

}

first();
second();
third();

print_r($global_arr);

I want every of the functions to index the array and add rows respectevly

Thank you in advance!

Edit :

Thank to your help here is the working version :

function first($arr) { 

    $arr[] = array('first' => 
             array(1, 1, 1));
    return $arr; 
}

function second($arr) { 

    $arr[] = array('second' => 
             array(2, 2, 2));
    return $arr; 
} 

$arr = array();    

$arr = first($arr);
$arr = second($arr); 

print_r($arr);

Output :

Array ( [0] => Array ( [first] => Array ( [0] => 1 [1] => 1 [2] => 1 ) ) [1] => Array ( [second] => Array ( [0] => 2 [1] => 2 [2] => 2 ) ) )

Any ideas how to be only :

Array ( [first] => Array ( [0] => 1 [1] => 1 [2] => 1) , [second] => Array([0] => 2, [1] => 2, [2] => 2))

?

Upvotes: 0

Views: 102

Answers (3)

djot
djot

Reputation: 2947

I don't get it - it's the same in/for all three functions.

BTW, I would use only one function like:

<?php

    // $arg1 = "one", "two" or "three"
    // $arg2 = ARRAY("data1.1", "data1.2", "data1.3") {
    function myfunc($arg1, $arg2) {
      if (!isset($my_arr)) { static $my_arr = ARRAY(); }
      $my_arr[$arg1][] = $arg2;
      return $my_arr; // Or code a getter and setter function
    }


    // Call, as often as you want - like:

    myfunc('one', ARRAY('1.1', '1.2','1.3'));
    myfunc('two', ARRAY('2.1', '2.2','2.3'));
    $arr = myfunc('one', ARRAY('1.4', '1.5','1.6'));

    print '<pre>';
    var_dump($arr);
    print '</pre>';



    /* result:

    array(2) {
      ["one"]=>
      array(2) {
        [0]=>
        array(3) {
          [0]=>
          string(3) "1.1"
          [1]=>
          string(3) "1.2"
          [2]=>
          string(3) "1.3"
        }
        [1]=>
        array(3) {
          [0]=>
          string(3) "1.4"
          [1]=>
          string(3) "1.5"
          [2]=>
          string(3) "1.6"
        }
      }
      ["two"]=>
      array(1) {
        [0]=>
        array(3) {
          [0]=>
          string(3) "2.1"
          [1]=>
          string(3) "2.2"
          [2]=>
          string(3) "2.3"
        }
      }
    }
    */
?>

Upvotes: 0

gaskar
gaskar

Reputation: 113

Why you want to use global variable? You can simply write

function first($arr) { 

    $arr[] = array('first' => 
             array('3' , '3', '3'));
    return $arr; 
}

function second($arr) { 

    $arr[] = array('second' => 
             array('3' , '3', '3'));
    return $arr; 
}

function third($arr) { 

    $arr[] = array('third' => 
             array('3' , '3', '3'));
    return $arr; 
}

$arr = array();    

$arr = first($arr);
$arr = second($arr);
$arr = third($arr);

Upvotes: 0

djot
djot

Reputation: 2947

$global_arr['third'][] = array('3.1' , '3.2', '3.3');

Upvotes: 1

Related Questions