使用 delim 将表中的多个值转换为字符串

问题描述 投票:0回答:1

感谢任何帮助。我在几个小时内尝试了多种方法但没有任何进展,希望有人可以提供帮助。

以下代码

    // If the table doe not exist Let just make sure records are not already in the system
    $startdelim = '"code":"';
    $enddelim = '"';
    
    $options = $userModel->getGiftVoucher_codes();
    
    foreach ($options as $rowx) {
            /* Lets clean the file up and pull out the codes */
            $rowx = $userModel->getCodes($rowx['options'], $startdelim, $enddelim );
            echo "<br /><br />";
            print_r($rowx);
    }

产生以下输出

Array ( [0] => 181752185202259 )

Array ( [0] => 1567088718285246 ) 

我一直在尝试找到一种方法将生成的值合并到单个字符串中,如下所示

181752185202259,1567088718285246 

我尝试将 foreach 封装在一个数组中 我尝试过 array_merge

如有任何帮助,我们将不胜感激。 问候 零

php
1个回答
0
投票

太棒了,来自 ChatGPT 的有益且友善的回复,这个地方已经失踪了一段时间了。

// If the table does not exist, let's just make sure records are not already in the system
$startdelim = '"code":"';
$enddelim = '"';

$options = $userModel->getGiftVoucher_codes();

$mergedCodes = []; // Initialize an array to store the codes

foreach ($options as $rowx) {
    /* Lets clean the file up and pull out the codes */
    $codes = $userModel->getCodes($rowx['options'], $startdelim, $enddelim);
    $mergedCodes = array_merge($mergedCodes, $codes); // Merge the codes into the array
}

$mergedString = implode(',', $mergedCodes); // Join the codes with commas

echo $mergedString; // Output the merged string
© www.soinside.com 2019 - 2024. All rights reserved.