我如何一次只获取一个键值对? PHP

时间:2015-10-22 00:13:40

标签: php mysql arrays loops object

我是PHP的新手,所以需要一些帮助。

我似乎几乎得到了我所需要的东西。但是,下面的代码将完全按照我的要求执行,它会遍历每条记录并获取所有键值对。然而,一旦条件命中“其他”,我不想打印所有剩余的$ key $ val对。相反,我想要的是抓住下一个最关键=> val对并重复,直到每条记录完成。

例如现在,此代码将按照我的要求打印出每条记录的LIST_1,LIST_87。我希望它也能抓住下一对$ key =>只有$ val,而不是剩下的。这样我可以注入每个$ key => $ val对成mySql中的一行。

但是现在,它给了我LIST_1,LIST_87和所有剩余的$ key => $ VAL的。 它看起来像这样:

Record: 0 *************************

LIST_124 => 0.00
GF20121126194243819854000000 => Entry Lvl Lvng Area,Fire Sprinkler,Foyer,Pantry,Volume Ceiling,Walk-in Closet
LIST_125 => 161.14
LIST_122 => 
LIST_123 => 
LIST_126 => 154.18
ROOM_BR2_room_level => 1
VOWAddr => 1
ROOM_PR_room_width => 10.6
GF20121126194243879313000000 => Gate - Manned,Security Patrol
listing_office_phone => (561) 622-5000
GF20121128203448419411000000 => Dining Family,Snack Bar
selling_member_email => jafeldman38@gmail.com
LIST_87 : 2015-01-01T00:01:52
LIST_1 : 20141118214124325535000000

I want it to do this:

Record: 0 *************************

INSERT ---
LIST_87 : 2015-01-01T00:01:52
LIST_1 : 20141118214124325535000000
LIST_124 => 0.00

INSERT ---
LIST_87 : 2015-01-01T00:01:52
LIST_1 : 20141118235552432553500000
LIST_125 => 161.14

INSERT ---
LIST_87 : 2015-01-01T00:01:52
LIST_1 : 20141445635552432553500000
GF20121126194243819854000000 => Entry Lvl Lvng Area,Fire, Sprinkler,Foyer,Pantry,Volume Ceiling,Walk-in Closet

CODE:

$prop_fields  = array("");
$prop_vals    = array("");
$LIST_1      = "";
$LIST_87     = "";
$count;

class Insert_Data
{

    public function create_insert_sql_from_search(){
      Global $results, $prop_fields, $prop_vals, $LIST_1, $LIST_87, $count ;
      for ($i=0; $i < count($results) ; $i++) { 
          // print "$results[$i]<br/><br/>";
          echo "<br/><br/>Record: $i *************************<br/><br/>";
          $myData = json_decode($results[$i]);
          foreach ($myData as $key => $val) {
              // We append the key and a comma to the end for every key inside the dbkeys array
              if($key == 'LIST_1'){
               $LIST_1 = $val;
               echo "LIST_1 : $LIST_1<br/>";
              }elseif($key == 'LIST_87'){
                $LIST_87 = $val;
                echo "LIST_87 : $LIST_87<br/>";
              }else{
                    echo "$key => $val<br/>";
              }
          }
      } 
    }
}
// create an object
$update = new Insert_Data();

// // show object properties
$update->create_insert_sql_from_search();
// echo "</pre>";

3 个答案:

答案 0 :(得分:1)

Continue();

您想使用“继续”,但也请参阅Break并直接按键访问数组值。

答案 1 :(得分:1)

  1. 您正在使用JSON,为什么不只是使用密钥检索数据而不是再次循环通过JSON数组?
  2. $myData = json_decode($results[$i]);
    $List_1 = $myData->List_1;
    echo "List 1 : $List_1";
    $List_87 = $myData->List_87;
    echo "List 87 : $List_87";
    
    1. 我会选择1,因为它可以帮助您避免循环使用所有内容。如果你还想要随机配对,你可以;
    2. $randomKey = null;
      while($randomKey != "List_1" || $randomKey != "List_87"){
      $keys = array_keys($myData);
      $randomKey = $keys[ rand(0, count($keys)-1) ];
      }
      
      echo "$randomKey : $myData->$randomKey";
      

      另一种方式(你的方式)是在显示记录时A为真(找到LIST_1)且B为真(找到LIST_87)时在下一条记录上使用Break

      foreach ($myData as $key => $val) {
      $a = false,  $b = false;
      if($key == 'LIST_1'){
          $LIST_1 = $val;
          $a =  true;
                     echo "LIST_1 : $LIST_1<br/>";
      }elseif($key == 'LIST_87'){
          $LIST_87 = $val;
          $b = true
          echo "LIST_87 : $LIST_87<br/>";
      }else{
          if($a && $b){
          echo "$key => $val<br/>";
          break;
      }
      }
      }
      

答案 2 :(得分:0)

This the current workable solution and can be refactored further.

require_once('login.php');
$prop_fields  = array("");
$prop_vals    = array("");

class Insert_Data
{

    public function create_insert_sql_from_search(){
      Global $results, $prop_fields, $prop_vals, $LIST_1, $LIST_87;
      for ($i=0; $i < count($results) ; $i++) { 
          // print "$results[$i]<br/><br/>";
          echo "<br/><br/>Record: $i *************************<br/><br/>";
          $myData = json_decode($results[$i]);
          $count = 0;
          $LIST_1      = "";
          $LIST_87     = "";
          $elements    = array();
          // iterate over myData to extract LIST_1 AND LIST_87 vals
          foreach ($myData as $key => $val) {
              // We append the key and a comma to the end for every key inside the dbkeys array
              if($key == 'LIST_1') {
                $LIST_1 = $val;
                // echo "$key : $LIST_1 <br/>";
              } elseif($key == 'LIST_87'){
                $LIST_87 = $val;
                // echo "$key : $LIST_87 <br/>";
              } else {
                continue;
              }
          }
          // iterate over myData to extract everything else
          foreach ($myData as $key => $val) {
              // We append the key and a comma to the end for every key inside the dbkeys array
              if (($key == 'LIST_1') || ($key == 'LIST_87')) {
                continue;
              } else {
                // add to our array 
                array_push($elements, array($LIST_1, $key, $val, $LIST_87));
              }
          }
          // done 
          // display our results
          //print_r($elements);

          foreach ($elements as $key => $value) {
            $query = "INSERT INTO [TABLE] (`LIST_1`, `KEY`, `VALUE`, 'LIST_87') VALUES ({$elements[$key][0]}, {$elements[$key][1]}, {$elements[$key][2]}, {$elements[$key][3]});";
            print_r($query); echo "<br>";  
          }

       } // $conn->close();
    }
}
// create an object
$update = new Insert_Data();

// // show object properties
$update->create_insert_sql_from_search();
// echo "</pre>";