将包含整数的变量解构为数组

Deconstruct an variable containing integers into an array

我是 PHP 的新手,我似乎遇到了障碍 code.I 有一个变量,其中包含由 space 分隔的数字序列。我打算将每个数字作为单独的记录提交到数据库中。我的问题是如何将变量解构为单个整数,以便将每个整数作为新记录提交。这是我的代码:

foreach ($domains as $domain)
{         
    $clicked_domains = array($form_entry->{"domain_$domain->id"});         

    if($clicked_domains){

        foreach($clicked_domains as $final_selection){
            if ($final_selection != 0){
             echo  $final_selection." "; // $final_selection now has the value of  17 20 12
            }
        }
    }  
 }

我现在想从 $final_selection 中提取每个整数(不包括 spaces)以提交给数据库

您可以使用 explode 创建一个整数数组,然后循环遍历每个整数。

$integer_array = explode(" ", $final_selection);

foreach($integer_array as $integer) {
    //submit $integer
}

查看 explode()

Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter.