What is the meaning of []

I have a code as below and I am not sure what type of data variable $ACTIVITYGROUPS[] has and how do I read it ?

$ACTIVITYGROUPS[] = saprfc_table_read ($fce, "ACTIVITYGROUPS", $i);

When I did print_r(saprfc_table_read ($fce, "ACTIVITYGROUPS", $i); I got bunch of arrays without any seperator and not sure how to exactract the data. can someone tell me what does it do in above sentences ?

Here is what print_r(saprfc_table_read ($fce, "ACTIVITYGROUPS", $i); result gives me:

Array (
    [AGR_NAME] => Y:SECURITY_DISPLAY
    [FROM_DAT] => 20080813
    [TO_DAT] => 99991231
    [AGR_TEXT] => Security Display - Users & Roles
    [ORG_FLAG] => C
)

Array (
    [AGR_NAME] => Y:SECURITY_ADMIN_COMMON
    [FROM_DAT] => 20080813
    [TO_DAT] => 99991231
    [AGR_TEXT] => Security Administrator
    [ORG_FLAG] => C
)

Array (
    [AGR_NAME] => Y:LOCAL_TRANSPORT
    [FROM_DAT] => 20090810
    [TO_DAT] => 99991231
    [AGR_TEXT] => Transport into target client - DEV system only
    [ORG_FLAG] =>
)

[] means push - put the given argument as a new element on the end of the array. That means that $ACTIVITYGROUPS is an array*.

$arr = array();
$arr[] = 1;       // Put 1 in position 0
$arr[] = "a";     // Put "a" in position 1
$arr[] = array()  // Put a new, empty array in position 2

As stated by the PHP docs, array_push has the same effect as [] .


* If it's not an array, using [] will give you a syntax error:

Warning: Cannot use a scalar value as an array in test.php on line 4


In many languages the [] notation stands for an array. Is the same as php's array_push() : it pushes an element in the variable that has [] at the end.

If the variable is null, you can consider the square brackets like a declaration of an array.

The same notation of push applies to Javascript, for example. When using it like $var[] = 'a'; what happens is the same as array_push() I was talking above. Just finds the next position in the array and adds there your value.

An array can be walked with for , foreach , while , do while and you can check it's contents with print_r() or var_dump() functions.


how do I read it ?.

Since saprfc_table_read already returns an array, $ACTIVITYGROUPS will be an array of arrays ( [] creates a new array element in array $ACTIVITYGROUPS ). To read it, you can iterate over it with foreach:

$ACTIVITYGROUPS[] = saprfc_table_read ($fce,"ACTIVITYGROUPS",$i);

foreach ($ACTIVITYGROUPS as $group) {
    echo $group['AGR_NAME']; // echos Y:SECURITY_DISPLAY on first iteration
    echo $group['FROM_DAT']; // echos 20080813 on first iteration
    // and so on...
}
链接地址: http://www.djcxy.com/p/1854.html

上一篇: 在PHP $ array [] = $ value或array中使用哪个更好

下一篇: 是什么意思 []