三行代码  ›  专栏  ›  技术社区  ›  moody1208

检查值位于PHP中的对象数组中

php
  •  0
  • moody1208  · 技术社区  · 5 天前

    当前,如果用户输入“1234”,则返回false,即使该值存在,但如果用户输入“5678”,则返回true并发回与该项相关的所有数据。

    $entries = array(
      (object) [
        "uid" => "1234",
        "item" => "x",
        "text_prefix" => "x",
        "text_suffix" => "x",
        "prize_link" => "x",
        "data_captcher" => true
      ],
      (object) [
        "uid" => "5678",
        "item" => "x",
        "text_prefix" => "x",
        "text_suffix" => "x",
        "prize_link" => "x",
        "data_captcher" => false
        ],
    );
    
    // $data = json_encode($entries);
    // echo $data;
    
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    
      $code = isset($_POST['code']) ? $_POST['code'] : '';
    
      var_dump($code);
    
      // foreach ($entries as $entry => $entry_Value) {
      for ($x = 0; $x < count($entries); $x++) {
        // var_dump($entry->uid);
    
        if ($entries[$x]->uid == $code) {
          $value = [
            "uid" => $entries[$x]->uid,
            "item" => $entries[$x]->item,
            "text_prefix" => $entries[$x]->text_prefix,
            "text_suffix" => $entries[$x]->text_suffix,
            "prize_link" => $entries[$x]->prize_link,
            "data_captcher" => $entries[$x]->data_captcher,
          ];
        }else {
          $value = 'false';
        }
    
        // var_dump($entries[$x]);
      }
    
      $data = json_encode($value);
      echo $data;
    
    }
    
    3 回复  |  直到 5 天前
        1
  •  2
  •   guyaloni    5 天前

    很简单,你错过了一个停止条件。
    循环迭代所有条目,因此最后一个条目将决定 value

    你应该做的是添加 break 在你的 if

    if ($entries[$x]->uid == $code) {
      $value = [
        "uid" => $entries[$x]->uid,
        "item" => $entries[$x]->item,
        "text_prefix" => $entries[$x]->text_prefix,
        "text_suffix" => $entries[$x]->text_suffix,
        "prize_link" => $entries[$x]->prize_link,
        "data_captcher" => $entries[$x]->data_captcher,
      ];
    
      break;  // <== this will stop the "for" loop
    }else {
      $value = 'false';
    }
    

    foreach 出于可读性原因,循环:

    foreach ($entries as $entry) {
       :
       :
    }
    
        2
  •  0
  •   RiggsFolly    5 天前

    当你找到匹配的时候,你不会停下来。下一次迭代是不匹配的,因此最终返回一个false。当您找到匹配项时,您需要停止循环,您可以使用 break;

    还有 foreach for

    此外,当你找到合适的对象时,你不需要做所有这些赋值,只要做一个 $value = $entry_obj; 一切都结束了。

    $entries = array(
        (object) [
          "uid" => "1234",
          "item" => "x",
          "text_prefix" => "x",
          "text_suffix" => "x",
          "prize_link" => "x",
          "data_captcher" => true
        ],
        (object) [
          "uid" => "5678",
          "item" => "x",
          "text_prefix" => "x",
          "text_suffix" => "x",
          "prize_link" => "x",
          "data_captcher" => false
          ],
    );
    
    $data = json_encode($entries);
    
    $_POST['code'] = '1234';
    $code = isset($_POST['code']) ? $_POST['code'] : '';
      
    var_dump($code);
      
    foreach ($entries as $entry_obj) {
        if ($entry_obj->uid == $code) {
            $value = $entry_obj;
            break;  // found it so stop otherwise we dont find it on next iteration
        }else {
            $value = 'false';
        }
    }
      
    $data = json_encode($value);
    echo $data;
    

    后果

    {
        "uid":"1234",
        "item":"x",
        "text_prefix":"x",
        "text_suffix":"x",
        "prize_link":"x",
        "data_captcher":true
    }
    
        3
  •  0
  •   mark_b    5 天前

    对于更简单的解决方案,您可以使用 array_column null coalescing operator

    // First re-key the array so that uid is the key
    $entries = array_column($entries, null, 'uid');
    
    // Then you can check if the array key exists directly, and assign a default value if not
    $value = $entries[$code] ?? 'false';