我需要在我的平面陣列中對值進行分組,以便每個非整數值在我的結果陣列中開始一個新的組/行,并且在下一個出現的非整數之前遇到的每個整數值都應該被推入相應組/行中的子陣列中.
輸入:
$unitsList = [
"Aulas Gratuitas",
"149",
"151",
"153",
"Módulo 0",
"964",
"989",
"Módulo 1",
"985",
"1079",
"1001",
"1003"
"Módulo 2",
"1009"
];
當前代碼:
$newArr = array();
foreach( $unitsList as $item ) {
if( !is_numeric($item) ) {
$title = array( "title" => $item, "units" => "" );
array_push( $newArr, $title);
} elseif ( is_numeric($item) ) {
$number = $item;
array_push( $newArr, $number);
}
}
當前結果:
[
[
"title" => "Aulas Gratuitas",
"units" => ""
]
"149",
"151",
"153",
[
"title" => "Módulo 0",
"units" => ""
],
"964",
"989",
[
"title" => 'Módulo 1',
"units" => ""
],
"985",
"1079",
"1001",
"1003"
[
"title" => 'Módulo 2',
"units" => ''
],
"1009"
]
如您所見,我很難將數字添加到“單位”鍵中。
期望的結果:
[
[
"title" => "Aulas Gratuitas",
"units" => ["149", "151", "153"]
],
[
"title" => 'Módulo 0',
"units" => ["964", "989"]
],
[
"title" => 'Módulo 1',
"units" => ["985", "1079", "1001", "1003"]
],
[
"title" => "Módulo 2",
"units" => ["1009"]
]
]
uj5u.com熱心網友回復:
因此,您不需要跟蹤第一級索引,請使用參考變數并將相關資料推送到該變數中,然后將新行推送到結果陣列中。
代碼:(演示)
$result = [];
foreach ($array as $value) {
if (!ctype_digit($value)) {
unset($units);
$units = [];
$result[] = ['title' => $value, 'units' => &$units];
} else {
$units[] = $value;
}
}
var_export($result);
unset()用于銷毀對前一組的參考,否則新推送的資料將被發送到結果陣列中的兩個(或更多)位置。參考變數的默認值為
null。如果title保證您的值后跟整數/單位值,則$units = [];可以洗掉。如果您有一個title然后另一個,則前一組title將沒有資料。title使用顯式陣列宣告,該組將有一個空units陣列而不是null.
使用相同的通用技術回答的相關問題:
- 將物件陣列分組到更深的父子結構中
- 將平面陣列拆分為包含輸入陣列中連續鍵值的分組子陣列
- 如何通過PHP中的重復字符拆分字串?
如果您正在運行 PHP7.3 或更高版本(此時您絕對應該是)并且您被使用參考變數嚇倒/迷惑,那么您可以利用array_key_last()來定位最新創建的陣列行。
代碼:(演示)
$result = [];
foreach ($array as $value) {
if (!ctype_digit($value)) {
$result[] = ['title' => $value, 'units' => []];
} else {
$result[array_key_last($result)]['units'][] = $value;
}
}
var_export($result);
uj5u.com熱心網友回復:
給定串列中的專案不規則。第一項有三個單位,第二項有兩個單位。如果不控制每個專案的型別,我們就無法將它們轉換為預期的結構。我的解決方案如下。我添加了解釋作為注釋行。
$values = array(
"string",
11111,
22222,
33333,
"string_2",
44444,
55555
);
$formattedArray = [];
$index = -1;
foreach ($values as $value) {
// If the value is a string, create the new array structure item and assign the title
if (is_string($value)) {
$index ;
$formattedArray[$index]['title'] = $value;
$formattedArray[$index]['units'] = [];
// The rest of the code in "foreach scope" is for integer values, so skip the remaining part
continue;
}
// If the following line is executing, the value is an integer
// Push the value to the current item's units
$formattedArray[$index]['units'][] = $value;
}
var_dump($formattedArray);
uj5u.com熱心網友回復:
$originalArray = ['a', 1, 2, 3, 'b', 4, 5, 6];
function formatArray($input) {
$output = [];
foreach($input as $inputRow) {
if (is_string($inputRow)) {
$output[] = ['title' => $inputRow, 'units' => []];
} elseif (count($output)) {
$output[count($output)-1]['units'][] = $inputRow;
}
}
return $output;
}
var_dump(formatArray($originalArray));
請注意,標題后面的數字不能是字串,否則此函式會將其識別為新標題。
此代碼將輸出:
array(2) {
[0]=>
array(2) {
["title"]=>
string(1) "a"
["units"]=>
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
}
[1]=>
array(2) {
["title"]=>
string(1) "b"
["units"]=>
array(3) {
[0]=>
int(4)
[1]=>
int(5)
[2]=>
int(6)
}
}
}
uj5u.com熱心網友回復:
方法:遍歷陣列中的值,檢查值是否轉換int為等于0,因此按該鍵分組,否則累積下一個元素。
<?php
$unitsList = [ "Aulas Gratuitas", "149", "151", "153", "Módulo 0", "964", "989", "Módulo 1", "985", "1079", "1001", "1003", "Módulo 2", "1009" ];
$result = [];
foreach ($unitsList as $key => $value) {
if(intval($value)==0){
$result[] = ['title' => $value, 'units' => []];
}else{
$result[count($result)-1]['units'][] = $value;
}
}
print_r(array_values($result));
?>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/514929.html
上一篇:如何迭代陣列以計算元素PHP?
下一篇:從c中的struct發出列印字串
