gpt4 book ai didi

php - 在循环 foreach 中内爆数组,但结果是双 php

转载 作者:行者123 更新时间:2023-11-29 04:33:00 25 4
gpt4 key购买 nike

我想根据上面项目的代码从数组中获取数据。但是我制作的数组实际上产生了双重结果。我只能适合的数据数组是项目

$query       = "SELECT IDBRG,Disc FROM $RTL.tmasterbarang1 WHERE IDBRG IN (1167646,1170635,1170634)";
// echo $query;
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_array($result))
{

$a = $row['IDBRG'];
$b = $row['Disc'];
$arr1 = str_split($b);

foreach($arr1 as $x=>$x_value)
{
echo "<br>";

$queryz = "SELECT CAT_DISC_NBR FROM $RTL.cat_disc_test WHERE FLR_NBR = 1 AND CAT_DISC_CD = '".$x_value."' ";
// echo $queryz;
$resultz = mysqli_query($connect, $queryz);
$rowz = mysqli_fetch_array($resultz);
$elements[] = $rowz['CAT_DISC_NBR'];

}
echo $all = implode(',', $elements);
}


result of print_r($arr1)."<br>";

1. Array ( [0] => Y [1] => 4 [2] => S [3] => V )
2. Array ( [0] => Y [1] => 4 [2] => S [3] => V )
3. Array ( [0] => Y [1] => G [2] => 3 )

// and the each of array have value Y=69, 4=39, S=66, V=66, G=51, 3=38

echo $all = implode(',', $elements);
69,39,63,66
69,39,63,66,69,39,63,66
69,39,63,66,69,39,63,66,69,51,38

// i want it should be
69,39,63,66
69,39,63,66
69,51,38

最佳答案

当你添加数据时

$elements[] = $rowz['CAT_DISC_NBR'];

这只是继续添加数据,$elements 数组永远不会被清除,因此下一个循环将继续向上一个循环的结果添加数据。

你需要像...

$a      = $row['IDBRG'];
$b = $row['Disc'];
$arr1 = str_split($b);
$elements = []; // Reset list

关于php - 在循环 foreach 中内爆数组,但结果是双 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54331749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com