What is the meaning of [] [closed]
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 i开发者_JS百科nto 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...
}
It's an array, but the output you have shows that your nested arrays do not have keys. I'm sure they have the default keys of 0,1,2,3,4...and so on, but if you want to identify them by any meaningful way I'd suggest adjusting the keys of the data within the array.
print_r($ACTIVITYGROUPS); //this should output the whole array
print_r($ACTIVITYGROUPS[0]); //this should output just the first element of the array, which is also an array
//now you have an 'address' for the array that you understand, now lets output one of the values
foreach($ACTIVITYGROUPS as $key => $item){
echo $item['ARG_NAME']; // this will output the ARG NAME from each of the arrays, in order
}
精彩评论