简体   繁体   English

显示数组时,我遇到了不在数组中的重复值

[英]When displaying array, I am having repeated values that are not in the array

Ok, I am beating my head on my PC…. 好的,我在电脑上跳动着头……。 I do not know what is going on. 我不知道发生了什么。 I'm using PHP to make a soap call to a remote database… I am running 4 queries, merging two multidimensional arrays and returning them. 我正在使用PHP对远程数据库进行肥皂调用…我正在运行4个查询,合并两个多维数组并返回它们。

The array looks clean, but when I display the results, they are repeating. 该数组看起来很干净,但是当我显示结果时,它们正在重复。

Here is one example after call: 这是调用后的一个示例:

$row = $response;
$count = count($row);
for($i=0;$i<=$count-1;$i++){
$product = $row[$i]['Product'];
$color = $row[$i]['Color'];
$type = $row[$i]['Type'];
$length = $row[$i]['Length'];
$render = '<li><div id="somediv">strong>' . $product . '</strong><br />Color: ' . $color . '<br />' .  ‘ Size: ' . $length .  '<input type="button" value="value" onclick="someaction(' . $i . ')" />' . '</div></li>';
}

Here is a repeated problem from the array returned : 这是返回的数组重复出现的问题:

[11] => Array ( 
                    [Product] => Some Product 
                    [Color] => Some Color 
                    [Type] => C 
                    [Length] => 150 
              )

However the it repeats the same occurrence.. it only does this with a handful of products: 但是,它重复相同的情况..仅对少数产品执行此操作:


  • Some Product 一些产品
    Color: some color 颜色:一些颜色
    Size: 150 大小:150
    Price: 4.79 价格:4.79

     </li> 


  • Some Product 一些产品
    Color: some color 颜色:一些颜色
    Size: 150 大小:150
    Price: 4.79 价格:4.79

     </li> 


  • Some Product 一些产品
    Color: some color 颜色:一些颜色
    Size: 150 大小:150
    Price: 4.79 价格:4.79

     </li> 


  • Some Product 一些产品
    Color: some color 颜色:一些颜色
    Size: 150 大小:150
    Price: 4.79 价格:4.79

     </li> 
  • Do a print_r($row) which will show you what is in your array. 进行print_r($row) ,它将向您显示数组中的内容。 If the values are there, it's not an issue with the printing, it's with generating the array. 如果有值,则打印不是问题,而是生成数组。

    抱歉,我是个假人...我忘了我隐藏了一些值,所以它一直重复到显示下一个取消隐藏的值为止

    Ok I was going to update my answer. 好的,我要更新我的答案。 I had put to if statements at the end of the foreach, and it would repeat an element for the elements that were not there. 我在foreach末尾使用了if语句,它将为不存在的元素重复一个元素。 I had two original if statements, so I moved the final if statements to the the if statements they basically represented and it worked. 我有两个原始的if语句,因此我将最终的if语句移到了它们基本上表示的if语句上,并且可以正常工作。

    here was first code example: 这是第一个代码示例:

    <?php 
    $row = $response;
    
    $count = count($row);
    
    
    
    
    for($i=0;$i<=$count-1;$i++){
    
    @$type = $row[$i]['Type'];  
    
    if ($type == 'something') {
    $someproduct = $row[$i]['someproduct'];
    $somecolor = $row[$i]['somecolor'];
    $length = $row[$i]['Length'];
    
    
    
    
    if ($type == 'this') {
    
    
    
    $img = 'myimage';
    
    $this = $img . '<div id="somediv"><br /><strong>' . $someproduct . 'somecolor: ' . $somecolor . '<input type="button" value="value" onclick="somefunction(' . $i . ')" />' . '</div></div></li>';
    
    
    
    
    }
    if ($type == 'that') {
    
    
    
    $img = 'myimage';
    
    $that = $img . '<div id="somediv"><br /><strong>' . $someproduct . 'somecolor: ' . $somecolor . '<input type="button" value="value" onclick="somefunction(' . $i . ')" />' . '</div></div></li>';
    
    
    
    
    }
    
    
    
    
    if (!isset($whatever) || $whatever == 'that') {
    
    
    echo $that;
    
    
    }
    
    if (!isset($whatever) || $whatever == 'this') {
    
    
    echo $this;
    
    
    }
    
    
    
    
    }
    }
    
    
    
    ?>
    

    Here is the change.. I've been working on this project so long, I'm overlooking small stuff that I should know. 这是更改。.我从事该项目已有很长时间了,我忽略了一些我应该知道的小知识。

    <?php 
    $row = $response;
    
    $count = count($row);
    
    
    
    
    for($i=0;$i<=$count-1;$i++){
    
    @$type = $row[$i]['Type'];  
    
    if ($type == 'something') {
    $someproduct = $row[$i]['someproduct'];
    $somecolor = $row[$i]['somecolor'];
    $length = $row[$i]['Length'];
    
    
    
    
    if ($type == 'this') {
    
    
    
    $img = 'myimage';
    
    $this = $img . '<div id="somediv"><br /><strong>' . $someproduct . 'somecolor: ' . $somecolor . '<input type="button" value="value" onclick="somefunction(' . $i . ')" />' . '</div></div></li>';
    
    
    if (!isset($whatever) || $whatever == 'this') {
    
    
    echo $this;
    
    
    }
    
    }
    if ($type == 'that') {
    
    
    
    $img = 'myimage';
    
    $that = $img . '<div id="somediv"><br /><strong>' . $someproduct . 'somecolor: ' . $somecolor . '<input type="button" value="value" onclick="somefunction(' . $i . ')" />' . '</div></div></li>';
    
    
    if (!isset($whatever) || $whatever == 'that') {
    
    
    echo $that;
    
    
    }
    
    }
    
    
    
    
    
    
    
    
    
    }
    }
    
    
    
    ?>
    

    Thanks for the advice and help 感谢您的建议和帮助

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM