0

我有这个数组:

$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 {}
    }
}
4

4 回答 4

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()

于 2013-10-13T01:56:35.850 回答
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);
        ?>

非常简单快速的执行代码:

于 2013-10-14T12:54:27.320 回答
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;
?>
于 2013-10-13T01:46:41.907 回答
0

尽管您已调用它$final,但您对循环遍历的数组和重新格式化的版本都使用了相同的变量。这几乎总是一个坏主意,并且很少按预期工作。

如果您$v每次循环都转储,您将看到 PHP 仍在使用数组的原始值,而不是您在循环时应用的更改。因此,'San Antonio'在粘合在一起之后,循环继续到'Antonio'原始数组中,并且'San'永远丢失了。

正如其他人所建议的那样,更好的方法是构建一个新数组,其中的元素根据需要重新格式化。这是另一个版本,使用嵌套while循环而不是 aforeach来“向前看”要连接的字符串条目(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;
}
于 2013-10-13T02:18:59.860 回答