我有下面的数组类型,现在我想获得它的子数组
例如,我想获取计数键7和8。该怎么做?有什么解决办法吗?我尝试了但没有成功:(
Array
(
[0] => stdClass Object
(
[id] => 4
[Blogdata] => stdClass Object
(
[7] => Array
(
[0] => stdClass Object
(
[blog_id] => 105
)
)
[8] => Array
(
[0] => stdClass Object
(
[blog_id] => 101
)
)
)
)
)
$date_count = array();
foreach($FeaturedBlogs as $Key=>$date) {
foreach($date as $d) {
$key = array_keys($d); // get our date
// echo $key;echo "<br>";
print_r($d);
$date_count[$key[0]]++;
}
}
答案 0 :(得分:1)
尝试一下。...
//Count Sub Array
$final_array = [];
$x = 0;
function countSubArray($data)
{
global $final_array;
global $x;
foreach($data as $key)
{
if(is_array($key))
{
$final_array[$x][0] = "1";
$final_array[$x][1] = json_encode($key);
$final_array[$x][2] = count((array)$key);
$x++;
countSubArray($key);
}
if(is_object($key))
{
$final_array[$x][0] = "2";
$final_array[$x][1] = json_encode($key);
$final_array[$x][2] = count((array)$key);
$x++;
countSubArray($key);
}
}
}
// Call Function...
countSubArray($arr); // what array you count..
// Display Sub Array Count...
$t_count = 0;
foreach($final_array as $d)
{
if($d[0] == 1)
{
echo "Array Count :".$d[2]." Array : ".$d[1]."<br>";
$t_count++;
}
}
echo "Total Array Count :".$t_count;
此示例的输出是...
Array
(
[0] => stdClass Object
(
[id] => 4
[Blogdata] => stdClass Object
(
[7] => Array
(
[0] => stdClass Object
(
[blog_id] => 135
)
)
[8] => Array
(
[0] => stdClass Object
(
[blog_id] => 101
)
)
)
)
)
Array Count :1 Array : [{"blog_id":135}]
Array Count :1 Array : [{"blog_id":101}]
Total Array Count :2
答案 1 :(得分:0)
更新后的答案
这将使用一个函数,因为它是自己的回调函数。该函数将循环遍历一个对象或数组,并测试每个元素以查看它是否是另一个对象或数组。
如果确实找到了新的对象或数组,则该函数将再次调用自身以对元素执行相同的操作,从而有效地遍历整个数组。
当找到每个元素的底部时,它将返回计数器的值,该计数器一直在跟踪它遇到了多少个数组。
像这样:
;With departements as
(
select DZ.Zone_ID, Zone_RecursID, Zone_label, Zone_level, Zone_Active
from Dim_Zone DZ
where Zone_Level = 5 and zone_active=1
),
regions as
(
select DZ.Zone_ID, Zone_RecursID, Zone_label, Zone_level, Zone_Active
from Dim_Zone DZ
where Zone_Level = 4 and zone_active=1
),
superRegions as
(
select DZ.Zone_ID, Zone_RecursID, Zone_label, Zone_level, Zone_Active
from Dim_Zone DZ
where Zone_Level = 3 and zone_active=1
),
Country as(
select DZ.Zone_ID, Zone_RecursID, Zone_label, Zone_level, Zone_Active
from Dim_Zone DZ
where Zone_Level = 2 and zone_active=1
),
continents as
(
select DZ.Zone_ID, Zone_RecursID, Zone_label, Zone_level, Zone_Active
from Dim_Zone DZ
where Zone_Level = 1 and zone_active=1
)
-- Departments
Select
D.Zone_id as departement_ID,
R.Zone_ID as region_ID,
SR.Zone_ID as SuperRegion_ID,
P.Zone_ID as Country_ID,
C.Zone_ID as continent_id,
D.Zone_label as departement_label,
R.Zone_label as region_label,
SR.Zone_label as SuperRegion_label,
P.Zone_label as Country_label,
C.Zone_label as continent_label,
case
when D.Zone_ID is not null then 5
when R.Zone_ID is not null then 4
when SR.Zone_ID is not null then 3
when P.Zone_ID is not null then 2
when C.Zone_ID is not null then 1
else 0
end as dimitem_level
from
departements AS D
INNER JOIN regions AS R ON D.Zone_recursID = R.ZoneID
INNER JOIN superRegions AS SR ON R.Zone_recursID = SR.ZoneID
INNER JOIN Country AS P ON SR.Zone_recursID = P.ZoneID
INNER JOIN continents AS C ON P.Zone_recursID = C.ZoneID
UNION ALL
-- Regions
Select
NULL as departement_ID, -- Department data is NULL
R.Zone_ID as region_ID,
SR.Zone_ID as SuperRegion_ID,
P.Zone_ID as Country_ID,
C.Zone_ID as continent_id,
NULL as departement_label, -- Department data is NULL
R.Zone_label as region_label,
SR.Zone_label as SuperRegion_label,
P.Zone_label as Country_label,
C.Zone_label as continent_label,
case -- Department case is omitted
when R.Zone_ID is not null then 4
when SR.Zone_ID is not null then 3
when P.Zone_ID is not null then 2
when C.Zone_ID is not null then 1
else 0
end as dimitem_level
from
regions AS R
INNER JOIN superRegions AS SR ON R.Zone_recursID = SR.ZoneID
INNER JOIN Country AS P ON SR.Zone_recursID = P.ZoneID
INNER JOIN continents AS C ON P.Zone_recursID = C.ZoneID
-- UNION ALL
-- superRegions
-- Country
-- continents
希望有帮助!