我有这个数组:
$final[0] = "San"
$final[1] = "Antonio"
$final[2] = "de"
$final[3] = "Areco"
$final[4] = 1200
$final[5] = 1231
$final[6] = "San"
$final[7] = "Martin"
$final[8] = "de"
$final[9] = "Andes"
$final[10] = 1200
$final[11] = 1231
$final[12] = "Carlos"
$final[13] = "Casares"
$final[14] = 1200
$final[15] = 1231
我需要将所有字母数字值合并为一个,并将数字保留在一个值上。
这就是我正在尝试的,但我不能让它工作,如果名字是两个单词长似乎工作正常,但有三个或更多的单词就像我'想念'第一个。
这应该是这样的:
$final[0] = "San Antonio de Areco"
$final[1] = 1200
$final[2] = 1231
$final[3] = "San Martin de Andes"
$final[4] = 1200
$final[5] = 1231
$final[6] = "Carlos Casares"
$final[7] = 1200
$final[8] = 1231
你能给我一个提示吗?
谢谢!
foreach ($final as $c => $v) {
if (ctype_alpha($v)) {
$vuelta1 = ($c+1);
if (ctype_alpha($final[$vuelta1])) {
$final[$vuelta1] = $v." ".$final[$vuelta1];
unset($final[$c]);
} else {}
}
}
答案 0 :(得分:1)
这是一种在代码中使用一些注释的方法。
<?php
$final[0] = "San";
$final[1] = "Antonio";
$final[2] = "de";
$final[3] = "Areco";
$final[4] = 1200;
$final[5] = 1231;
$final[6] = "San";
$final[7] = "Martin";
$final[8] = "de";
$final[9] = "Andes";
$final[10] = 1200;
$final[11] = 1231;
$final[12] = "Carlos";
$final[13] = "Casares";
$final[14] = 1200;
$final[15] = 1231;
$final[16] = "this";
$final[17] = "is";
$final[18] = "a";
$final[19] = "test";
$new = array(); // this will hold the reorderd values
$string_parts = array(); // temp array to store string parts to be combined
foreach ( $final as $c => $v )
{
if(ctype_alpha($v))
{
$string_parts[] = $v; // this is a string, store it for later with other possible strings
}
else
{
if( count( $string_parts ) > 0 ) $new[] = implode( ' ', $string_parts ); // all the parts that were detected as strings will be combined into one value in the new array, spearated by spaces
$string_parts = array(); // reset the array
$new[] = $v; // not a string, then add the number to the new array
}
}
// Fix bug spotted by IMSoP in his comment
if( count( $string_parts ) > 0 ) $new[] = implode( ' ', $string_parts ); // all the parts that were detected as strings will be combined into one value in the new array, spearated by spaces
print_r( $new );
?>
如果要用新的数组替换原始数组:
<?php
$final = $new;
?>
答案 1 :(得分:1)
这是一种方法。它确实填充了不同的数组,但应该完成你想要的。
$aNew = array(); // the new array
$wasAlpha = 0; // just a boolean toggle
foreach($final as $v){
if(is_int($v)){
$wasAlpha = 0; // toggle flag
$aNew[] = $v; // push the number onto the new array
}else{
if($wasAlpha){ // last one was also not a number
$aNew[sizeof($aNew)-1] .= ' ' . $v; // append to last with a space
}else{
$wasAlpha = 1; // last one was a number
$aNew[] = $v; // push new non number value to new array
}
}
}
编辑:您可能更喜欢is_numeric()
答案 2 :(得分:1)
尝试这个非常简单的逻辑:
<?php
$final[0] = "San";
$final[1] = "Antonio";
$final[2] = "de";
$final[3] = "Areco";
$final[4] = 1200;
$final[5] = 1231;
$final[6] = "San";
$final[7] = "Martin";
$final[8] = "de";
$final[9] = "Andes";
$final[10] = 1200;
$final[11] = 1231;
$final[12] = "Carlos";
$final[13] = "Casares";
$final[14] = 1200;
$final[15] = 1231;
$tempArr = array();
$temp_val = '';
$count = 0;
foreach ($final as $Val) {
if (!is_numeric($Val)) {
$temp_val.= $Val . " ";
} else {
if (!empty($temp_val)) {
// Assign String
$tempArr[$count] = $temp_val;
$temp_val = '';
$count++;
// Assign Number
$tempArr[$count] = $Val;
$count++;
} else {
$tempArr[$count] = $Val;
$count++;
}
}
}
var_dump($tempArr);
?>
非常简单&amp;快速执行代码:
答案 3 :(得分:0)
虽然您已将其称为$final
,但您正在为正在循环的阵列和重新格式化的版本使用相同的变量。这几乎总是一个坏主意,很少按预期工作。
如果每次循环转出$v
,您将看到PHP仍在使用数组的原始值,而不是在循环时应用的更改。因此,在'San Antonio'
粘合在一起之后,循环会转到原始数组中的'Antonio'
,'San'
将永远丢失。
更好的方法是根据需要构建一个新元素,并重新格式化元素,正如其他人所建议的那样。这是另一个版本,使用嵌套的while
循环而不是foreach
来“向前看”字符串条目以连接(Live Demo):
$really_final = array();
$k = 0;
while ( $k < count($final) )
{
// Take the value of the next entry regardless
$v = $final[$k];
// Move to next entry
$k++;
// If it's a string, look ahead until you find a non-string
if ( ctype_alpha($v) )
{
while ( $k < count($final) && ctype_alpha($final[$k]) )
{
// append the next string
$v .= ' ' . $final[$k];
// Move to next entry
$k++;
}
}
// Add to the new array
$really_final[] = $v;
}