无法自动将foreach键的值增加

时间:2019-06-12 18:58:55

标签: php

我正在使用一个选择查询组,因为我在列中有重复的值,并且有一个foreach循环来为每个增量生成每个结果。但是,我正确地得到了我的结果,但是没有正确地得到增量,因此我也需要将我的结果与(如果声明)进行比较。

$sqlr = mysql_query ("SELECT lati, COUNT(*) FROM upload GROUP BY lati HAVING COUNT(lati) > 1;"); 

while ($row = mysql_fetch_array($sqlr)) 
{

    $countedlati = array($row['lati']);
    foreach ($countedlati as $key => $lativalue) {
         echo $key ."</br>".$lativalue ."</br>";
    }

}

结果

enter image description here

我想要的结果是什么

0
1.3048
1
3.140583

然后在if语句中比较结果

for( $i=$offset; $i<$end; $i++ )
{
   $data = $res[$i];

   if($data['latitude'] == $lativalue){
    $icon = "upload/yellow.png";
   }

}

1 个答案:

答案 0 :(得分:-1)

当您执行$sqlr = mysql_query ("SELECT lati, COUNT(*) FROM upload GROUP BY lati HAVING COUNT(lati) > 1;"); $latis = array(); // init empty array while ($row = mysql_fetch_array($sqlr)) { $latis[] = $row['lati']; // append the element to the array } 时,您将定义一个具有1个元素的数组-这就是为什么每次获得0的原因。

您应该将元素添加到一个数组中,然后对其进行迭代。考虑以下示例:

foreach

现在,您可以通过以下方式进行foreach($latis as $lati) { if($lati == $lativalue){ $icon = "upload/yellow.png"; } } 循环:

class addanimal extends React.Component{
constructor() {
    this.state = {
      snapshotList: {},
    };
    this.getPickerElements = this.getPickerElements.bind(this);
  }

  componentDidMount() {
    //var sportsRoomRef = firebase.database().ref('/sportsRoomList');
    let userID = f.auth().currentUser.uid; // grab the current users userId
    //var pickerArr = [];

    var sportsRoomRef = database.ref('animals/'+userID).orderByChild('animaltype').equalTo('ram');


    sportsRoomRef.once('value').then(snapshot => {
        var snapshotList = snapshot.val();
        this.setState({snapshotList});
    })
}   
getPickerElements() {
    var pickerArr = [];
    var snapshotList = this.state.snapshotList;
    for (var key in snapshotList) {
        pickerArr.push(<Picker.Item label={snapshotList[key]} value={snapshotList[key]}/>);
    }
    return pickerArr;
}

  render() {
    return (
      <View style={{marginTop: 100}}>
          <Picker
            selectedValue={this.state.pickerSelection}
            style={[{width: 290, height: 50, color: 'black'}, pickerStyle]}
            onValueChange={(itemValue) => this.setState({pickerSelection: itemValue})}>
            <Picker.Item label='Salle de sport' value='default'/>
            {getPickerElements()}
        </Picker>
      </View>
    );
  }
}