从另一个 php 文件调用函数时返回数组的最后一项
Last item of array is returned when a function is called from another php file
我是PHP的新手。我想要 return 来自一个 php 文件中的函数的数组,并在另一个 php 文件中访问数组的元素。
我面临的问题是,当我 运行 源文件并打印数组元素时,我能够在控制台中看到所有标签,但是当我 return 数组并在另一个中打印它时php 文件,则只有一项被 returned 或没有项目被 returned。
来源file.php
<?php
// Load the Google API PHP Client Library.
require_once 'C:/xxxx/google-api-php-client-2.2.4/vendor/autoload.php';
function getTags(){
$KEY_FILE_LOCATION = 'C:/xxx/service-account-credentials.json';
$params=array(
'dimensions' => 'ga:dimension5,ga:dimension2',
'filters' => 'ga:dimension5==123456'
);
$results = $analytics->data_ga->get(
'ga:' . 123456,
'2019-08-29',
'yesterday',
'ga:sessions',
$params
);
$rows = $results->getRows();
$StoreTags = array();
for($i=0;$i<sizeof($rows);$i++){
$ExtractTag_arr = explode(',',$rows[$i][1]);
for($j=0;$j<count($ExtractTag_arr);$j++){
$StoreTags[]=$ExtractTag_arr[$j];
}
$ExtractTag_arr='';
}
$unique_tags = array_unique($StoreTags);
$Unique_tags_Values = array_values(array_filter($unique_tags));
$Count_UniqueTags = count($Unique_tags_Values);
$Count_StoreTags=count($StoreTags);
for($a=0;$a<$Count_UniqueTags;$a++){
$Count_Occurances = 0;
$UniqueTags_Value = $Unique_tags_Values[$a];
for ($k=0;$k<$Count_StoreTags;$k++){
if ($UniqueTags_Value == $StoreTags[$k]){
$Count_Occurances = $Count_Occurances+1;
}
else{
$Count_Occurances = $Count_Occurances+0;
}
}
$Display_Tags=array($Unique_tags_Values[$a], $Count_Occurances);
return $Display_Tags;
}
}
目标文件:
<?php
include 'Source.php';
get_tags = getTags();
Print_r(getTags());
当我 运行 target.php return 什么都没有并且如果我 运行 source.php 没有 return 语句并且可以打印所有项目在 $Display_Tags
1.get_tags = getTags();
不正确,需要$get_tags = getTags();
.
2.Print_r(getTags());
需要 print_r(getTags());
3.To 解决您的问题,将 return $Display_Tags;
放在函数 getTags()
.
的 for()
循环之外
<?php
include 'Source.php';
$get_tags = getTags();
print_r($get_tags);
我是PHP的新手。我想要 return 来自一个 php 文件中的函数的数组,并在另一个 php 文件中访问数组的元素。
我面临的问题是,当我 运行 源文件并打印数组元素时,我能够在控制台中看到所有标签,但是当我 return 数组并在另一个中打印它时php 文件,则只有一项被 returned 或没有项目被 returned。
来源file.php
<?php
// Load the Google API PHP Client Library.
require_once 'C:/xxxx/google-api-php-client-2.2.4/vendor/autoload.php';
function getTags(){
$KEY_FILE_LOCATION = 'C:/xxx/service-account-credentials.json';
$params=array(
'dimensions' => 'ga:dimension5,ga:dimension2',
'filters' => 'ga:dimension5==123456'
);
$results = $analytics->data_ga->get(
'ga:' . 123456,
'2019-08-29',
'yesterday',
'ga:sessions',
$params
);
$rows = $results->getRows();
$StoreTags = array();
for($i=0;$i<sizeof($rows);$i++){
$ExtractTag_arr = explode(',',$rows[$i][1]);
for($j=0;$j<count($ExtractTag_arr);$j++){
$StoreTags[]=$ExtractTag_arr[$j];
}
$ExtractTag_arr='';
}
$unique_tags = array_unique($StoreTags);
$Unique_tags_Values = array_values(array_filter($unique_tags));
$Count_UniqueTags = count($Unique_tags_Values);
$Count_StoreTags=count($StoreTags);
for($a=0;$a<$Count_UniqueTags;$a++){
$Count_Occurances = 0;
$UniqueTags_Value = $Unique_tags_Values[$a];
for ($k=0;$k<$Count_StoreTags;$k++){
if ($UniqueTags_Value == $StoreTags[$k]){
$Count_Occurances = $Count_Occurances+1;
}
else{
$Count_Occurances = $Count_Occurances+0;
}
}
$Display_Tags=array($Unique_tags_Values[$a], $Count_Occurances);
return $Display_Tags;
}
}
目标文件:
<?php
include 'Source.php';
get_tags = getTags();
Print_r(getTags());
当我 运行 target.php return 什么都没有并且如果我 运行 source.php 没有 return 语句并且可以打印所有项目在 $Display_Tags
1.get_tags = getTags();
不正确,需要$get_tags = getTags();
.
2.Print_r(getTags());
需要 print_r(getTags());
3.To 解决您的问题,将 return $Display_Tags;
放在函数 getTags()
.
for()
循环之外
<?php
include 'Source.php';
$get_tags = getTags();
print_r($get_tags);