PHP多维数组-获取最后一个索引中元素的值



不确定我的措辞是否正确,但下面的内容可以说明我正在努力实现的目标。。

我有一个多维数组,我想从每个的数组中的最后一个索引中捕获一个特定元素

阵列:

[Something] => Array
(
[Something1] => Array
(
[0] => Array
(
[@attributes] => Array
(
[DataID] => Data
[DataID] => Data
[DataID] => Data
[DataID] => Data
)
[Something1.1] => Array
(
[Something1.1.1] => Array
(
[0] => Array
(
[DataID] => Data
[Date] => YYYY-MM-DD
[DataID] => Data
[DataID] => Data
[DataID] => Data
[DataID] => Data
)
[1] => Array
(
[DataID] => Data
[Date] => YYYY-MM-DD
[DataID] => Data
[DataID] => Data
[DataID] => Data
[DataID] => Data
)
[2] => Array (last returned)
(
[DataID] => Data
[DateLASTRETURNED] => YYYY-MM-DD
[DataID] => Data
[DataID] => Data
[DataID] => Data
[DataID] => Data
)
)
)
[Something1.2] => Array
(
[Something.1.2.1] => Array
(
[0] => Array
(
[DataID] => Data
[Date] => YYYY-MM-DD
[DataID] => Data
[DataID] => Data
[DataID] => Data
[DataID] => Data
)
[1] => Array
(
[DataID] => Data
[Date] => YYYY-MM-DD
[DataID] => Data
[DataID] => Data
[DataID] => Data
[DataID] => Data
)
[2] => Array (last returned)
(
[DataID] => Data
[DateLASTRETURNED] => YYYY-MM-DD
[DataID] => Data
[DataID] => Data
[DataID] => Data
[DataID] => Data
)
)
)
)
)
)

正如你在括号中看到的(最后一次返回(,我需要foreach中最后一次的指定元素中的值";Something1.*(-具体来说,此字段包含日期YYYY-MM-DD,并希望存储到$中以在其他地方重复使用。

希望这是有意义的,期待一些帮助!

您可以这样做。首先访问,直到达到所需的级别,然后选择每个数组的最后一个数组,检查它是否是数组,然后打印值";日期";。

假设示例中的多数组存储在名为$array:的变量中

foreach($array["something"]["Something1"] as $value) {
foreach($value as $value1){
if(is_array(end($value1))) {
echo(end($value1)["Date"]);
}
}
}

如果您的多数组偏离了您所指示的示例,也许您应该添加一些额外的验证。


编辑以显示将值保存到变量中而不是打印值的示例。

// First declare an array to store all values
$var = array();
// Then extract all values and save them into $var
foreach($array["something"]["Something1"] as $value) {
foreach($value as $value1){
if(is_array(end($value1))) {
array_push($var, end($value1)["Date"]); // Add the value at the end of the $var array
}
}
}
// Finally you have all values stored into the $var array
// If you know the number of values, you can access them one by one
$var[num]; // Where 'num' is the position of the value stored into the array (0,1,2...)
// Or you can loop it
foreach ($var as $date) {
// do whatever with each $date value
}

如果您想从"有些事情;。你可以试试这样的东西:

$array = [
'Something' => [
'Something1' => [
[
"@attributes" => array(
'DataID_1' => "Data",
"DataID_2" => "Data",
"DataID_3" => "Data",
),
"Something1.1" => array(
"Something1.1.1" => array(
array(
"DataID_1" => "data",
"Date" => "YYYY-MM-DD",
"DataID_2" => "data",
"DataID_3" => "data",
),
array(
"DataID_1" => "data",
"Date" => "YYYY-MM-DD",
"DataID_2" => "data",
"DataID_3" => "data",
),
array(
"DataID_1" => "data",
"Date" => "YYYY-MM-DD last",
"DataID_2" => "data",
"DataID_3" => "data",
),
),
),
"Something1.2" => array(
"Something1.2.1" => array(
array(
"DataID_1" => "data",
"Date" => "YYYY-MM-DD",
"DataID_2" => "data",
"DataID_3" => "data",
),
array(
"DataID_1" => "data",
"Date" => "YYYY-MM-DD",
"DataID_2" => "data",
"DataID_3" => "data",
),
array(
"DataID_1" => "data",
"Date" => "YYYY-MM-DD last",
"DataID_2" => "data",
"DataID_3" => "data",
),
),
),
],
],
],
];
$dates = [];
$something1 = $array['Something']['Something1'];
foreach ($something1 as $something_1) {                 // loop through 'Something1'
foreach ($something_1 as $key => $something_1_1) {  // loop through each element of 'Something1'.
if (str_starts_with($key, 'Something1.')) {     // if key starts with 'Something1.' PHP >= 8.0
foreach ($something_1_1 as $key => $something_1_1_1) {
$last_element = end($something_1_1_1);  // get last element of array
$dates[] = $last_element['Date'];       // OUTPUT: YYYY-MM-DD last
}
}
}
}
var_dump($dates); // This array will contain your dates.
// You can also use the following code to loop through dates:
foreach ($dates as $date) {
echo $date; // OUTPUT: YYYY-MM-DD last
}
//OR access the dates in the following way:
$first_date = $dates[0];                    // get first date. and so on..
$last_date = $dates[count($dates) - 1];     // get last date.

请注意这些函数:
str_starts_with((自PHP8.0起可用
end((将数组的内部指针设置为其最后一个元素。

相关内容

  • 没有找到相关文章

最新更新