Skip adding to array if it exists in PHP









up vote
2
down vote

favorite












I am using in_array function to skip adding the same addresses to an array.



Code:



$addresses_list = array();
$stmt_select_address_result = $databaseManager->connect()->prepare("SELECT lat,lng,address FROM api_order where userid=683;");
$stmt_select_address_result->execute();

while ($row = $stmt_select_address_result->fetch(PDO::FETCH_ASSOC))
if (!in_array($row, $addresses_list))
array_push($addresses_list, $row);




I want to skip adding to the arrays if $row['address'] is the same as an address of $addresses_list['address'].










share|improve this question



























    up vote
    2
    down vote

    favorite












    I am using in_array function to skip adding the same addresses to an array.



    Code:



    $addresses_list = array();
    $stmt_select_address_result = $databaseManager->connect()->prepare("SELECT lat,lng,address FROM api_order where userid=683;");
    $stmt_select_address_result->execute();

    while ($row = $stmt_select_address_result->fetch(PDO::FETCH_ASSOC))
    if (!in_array($row, $addresses_list))
    array_push($addresses_list, $row);




    I want to skip adding to the arrays if $row['address'] is the same as an address of $addresses_list['address'].










    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I am using in_array function to skip adding the same addresses to an array.



      Code:



      $addresses_list = array();
      $stmt_select_address_result = $databaseManager->connect()->prepare("SELECT lat,lng,address FROM api_order where userid=683;");
      $stmt_select_address_result->execute();

      while ($row = $stmt_select_address_result->fetch(PDO::FETCH_ASSOC))
      if (!in_array($row, $addresses_list))
      array_push($addresses_list, $row);




      I want to skip adding to the arrays if $row['address'] is the same as an address of $addresses_list['address'].










      share|improve this question















      I am using in_array function to skip adding the same addresses to an array.



      Code:



      $addresses_list = array();
      $stmt_select_address_result = $databaseManager->connect()->prepare("SELECT lat,lng,address FROM api_order where userid=683;");
      $stmt_select_address_result->execute();

      while ($row = $stmt_select_address_result->fetch(PDO::FETCH_ASSOC))
      if (!in_array($row, $addresses_list))
      array_push($addresses_list, $row);




      I want to skip adding to the arrays if $row['address'] is the same as an address of $addresses_list['address'].







      php arrays






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 11:45









      William Jones

      6110




      6110










      asked Nov 10 at 10:20









      Pardis Ak

      288




      288






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          You can create a temporary variable to store the value of address already stored in the $addresses_list array. Whenever, you are storing a $row in the $addresses_list array, you can store the address key value in the temporary variable.



          Now, everytime you need to check against the temporary variable, whether the key value already exists or not.



          Note that even PHP documentation recommends using operator against array_push(). So I have changed your code to use instead.




          Note: If you use array_push() to add one element to the array, it's
          better to use $array = because in that way there is no overhead of
          calling a function.




          Try below (explanation in comments):



          $addresses_list = array();
          $temp_addresses = array(); // Create a temp array

          $stmt_select_address_result = $databaseManager->connect()->prepare("SELECT lat,lng,address FROM api_order where userid=683;");
          $stmt_select_address_result->execute();

          while ($row = $stmt_select_address_result->fetch(PDO::FETCH_ASSOC))

          // check in temporary variable instead
          if (!in_array($row['address'], $temp_addresses))

          // add to temp array to avoid duplicate entry in next loop
          $temp_addresses = $row['address'];

          $addresses_list = $row;





          If you really want unique address values only, and there is no other use of the duplicate rows. You can rather solve this problem at the SQL query end itself. You can simply use GROUP BY on address. This will optimize the data packet being transferred from database server to PHP application. Moreover, additional array operations can be done way with.



          $addresses_list = array();

          // It is a good practice to have query string in a separate variable
          $sql = "SELECT lat,lng,address
          FROM api_order
          where userid=683
          GROUP BY address;"

          $stmt_select_address_result = $databaseManager->connect()->prepare($sql);

          $stmt_select_address_result->execute();

          while ($row = $stmt_select_address_result->fetch(PDO::FETCH_ASSOC))
          // No need for any checks, as you are getting unique addresses only
          $addresses_list = $row;






          share|improve this answer






















          • Hello again, Can you help me to show the number of repeats?
            – Pardis Ak
            Nov 17 at 8:39










          • @PardisAk what do you mean ? Can you post a new question for the same.
            – Madhur Bhaiya
            Nov 17 at 8:45










          • I mean: I want to show the number of repetitions of an address. for example, if an address repeats 4 times I show a row of like this: Paris-4
            – Pardis Ak
            Nov 19 at 5:29











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237974%2fskip-adding-to-array-if-it-exists-in-php%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          You can create a temporary variable to store the value of address already stored in the $addresses_list array. Whenever, you are storing a $row in the $addresses_list array, you can store the address key value in the temporary variable.



          Now, everytime you need to check against the temporary variable, whether the key value already exists or not.



          Note that even PHP documentation recommends using operator against array_push(). So I have changed your code to use instead.




          Note: If you use array_push() to add one element to the array, it's
          better to use $array = because in that way there is no overhead of
          calling a function.




          Try below (explanation in comments):



          $addresses_list = array();
          $temp_addresses = array(); // Create a temp array

          $stmt_select_address_result = $databaseManager->connect()->prepare("SELECT lat,lng,address FROM api_order where userid=683;");
          $stmt_select_address_result->execute();

          while ($row = $stmt_select_address_result->fetch(PDO::FETCH_ASSOC))

          // check in temporary variable instead
          if (!in_array($row['address'], $temp_addresses))

          // add to temp array to avoid duplicate entry in next loop
          $temp_addresses = $row['address'];

          $addresses_list = $row;





          If you really want unique address values only, and there is no other use of the duplicate rows. You can rather solve this problem at the SQL query end itself. You can simply use GROUP BY on address. This will optimize the data packet being transferred from database server to PHP application. Moreover, additional array operations can be done way with.



          $addresses_list = array();

          // It is a good practice to have query string in a separate variable
          $sql = "SELECT lat,lng,address
          FROM api_order
          where userid=683
          GROUP BY address;"

          $stmt_select_address_result = $databaseManager->connect()->prepare($sql);

          $stmt_select_address_result->execute();

          while ($row = $stmt_select_address_result->fetch(PDO::FETCH_ASSOC))
          // No need for any checks, as you are getting unique addresses only
          $addresses_list = $row;






          share|improve this answer






















          • Hello again, Can you help me to show the number of repeats?
            – Pardis Ak
            Nov 17 at 8:39










          • @PardisAk what do you mean ? Can you post a new question for the same.
            – Madhur Bhaiya
            Nov 17 at 8:45










          • I mean: I want to show the number of repetitions of an address. for example, if an address repeats 4 times I show a row of like this: Paris-4
            – Pardis Ak
            Nov 19 at 5:29















          up vote
          2
          down vote



          accepted










          You can create a temporary variable to store the value of address already stored in the $addresses_list array. Whenever, you are storing a $row in the $addresses_list array, you can store the address key value in the temporary variable.



          Now, everytime you need to check against the temporary variable, whether the key value already exists or not.



          Note that even PHP documentation recommends using operator against array_push(). So I have changed your code to use instead.




          Note: If you use array_push() to add one element to the array, it's
          better to use $array = because in that way there is no overhead of
          calling a function.




          Try below (explanation in comments):



          $addresses_list = array();
          $temp_addresses = array(); // Create a temp array

          $stmt_select_address_result = $databaseManager->connect()->prepare("SELECT lat,lng,address FROM api_order where userid=683;");
          $stmt_select_address_result->execute();

          while ($row = $stmt_select_address_result->fetch(PDO::FETCH_ASSOC))

          // check in temporary variable instead
          if (!in_array($row['address'], $temp_addresses))

          // add to temp array to avoid duplicate entry in next loop
          $temp_addresses = $row['address'];

          $addresses_list = $row;





          If you really want unique address values only, and there is no other use of the duplicate rows. You can rather solve this problem at the SQL query end itself. You can simply use GROUP BY on address. This will optimize the data packet being transferred from database server to PHP application. Moreover, additional array operations can be done way with.



          $addresses_list = array();

          // It is a good practice to have query string in a separate variable
          $sql = "SELECT lat,lng,address
          FROM api_order
          where userid=683
          GROUP BY address;"

          $stmt_select_address_result = $databaseManager->connect()->prepare($sql);

          $stmt_select_address_result->execute();

          while ($row = $stmt_select_address_result->fetch(PDO::FETCH_ASSOC))
          // No need for any checks, as you are getting unique addresses only
          $addresses_list = $row;






          share|improve this answer






















          • Hello again, Can you help me to show the number of repeats?
            – Pardis Ak
            Nov 17 at 8:39










          • @PardisAk what do you mean ? Can you post a new question for the same.
            – Madhur Bhaiya
            Nov 17 at 8:45










          • I mean: I want to show the number of repetitions of an address. for example, if an address repeats 4 times I show a row of like this: Paris-4
            – Pardis Ak
            Nov 19 at 5:29













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          You can create a temporary variable to store the value of address already stored in the $addresses_list array. Whenever, you are storing a $row in the $addresses_list array, you can store the address key value in the temporary variable.



          Now, everytime you need to check against the temporary variable, whether the key value already exists or not.



          Note that even PHP documentation recommends using operator against array_push(). So I have changed your code to use instead.




          Note: If you use array_push() to add one element to the array, it's
          better to use $array = because in that way there is no overhead of
          calling a function.




          Try below (explanation in comments):



          $addresses_list = array();
          $temp_addresses = array(); // Create a temp array

          $stmt_select_address_result = $databaseManager->connect()->prepare("SELECT lat,lng,address FROM api_order where userid=683;");
          $stmt_select_address_result->execute();

          while ($row = $stmt_select_address_result->fetch(PDO::FETCH_ASSOC))

          // check in temporary variable instead
          if (!in_array($row['address'], $temp_addresses))

          // add to temp array to avoid duplicate entry in next loop
          $temp_addresses = $row['address'];

          $addresses_list = $row;





          If you really want unique address values only, and there is no other use of the duplicate rows. You can rather solve this problem at the SQL query end itself. You can simply use GROUP BY on address. This will optimize the data packet being transferred from database server to PHP application. Moreover, additional array operations can be done way with.



          $addresses_list = array();

          // It is a good practice to have query string in a separate variable
          $sql = "SELECT lat,lng,address
          FROM api_order
          where userid=683
          GROUP BY address;"

          $stmt_select_address_result = $databaseManager->connect()->prepare($sql);

          $stmt_select_address_result->execute();

          while ($row = $stmt_select_address_result->fetch(PDO::FETCH_ASSOC))
          // No need for any checks, as you are getting unique addresses only
          $addresses_list = $row;






          share|improve this answer














          You can create a temporary variable to store the value of address already stored in the $addresses_list array. Whenever, you are storing a $row in the $addresses_list array, you can store the address key value in the temporary variable.



          Now, everytime you need to check against the temporary variable, whether the key value already exists or not.



          Note that even PHP documentation recommends using operator against array_push(). So I have changed your code to use instead.




          Note: If you use array_push() to add one element to the array, it's
          better to use $array = because in that way there is no overhead of
          calling a function.




          Try below (explanation in comments):



          $addresses_list = array();
          $temp_addresses = array(); // Create a temp array

          $stmt_select_address_result = $databaseManager->connect()->prepare("SELECT lat,lng,address FROM api_order where userid=683;");
          $stmt_select_address_result->execute();

          while ($row = $stmt_select_address_result->fetch(PDO::FETCH_ASSOC))

          // check in temporary variable instead
          if (!in_array($row['address'], $temp_addresses))

          // add to temp array to avoid duplicate entry in next loop
          $temp_addresses = $row['address'];

          $addresses_list = $row;





          If you really want unique address values only, and there is no other use of the duplicate rows. You can rather solve this problem at the SQL query end itself. You can simply use GROUP BY on address. This will optimize the data packet being transferred from database server to PHP application. Moreover, additional array operations can be done way with.



          $addresses_list = array();

          // It is a good practice to have query string in a separate variable
          $sql = "SELECT lat,lng,address
          FROM api_order
          where userid=683
          GROUP BY address;"

          $stmt_select_address_result = $databaseManager->connect()->prepare($sql);

          $stmt_select_address_result->execute();

          while ($row = $stmt_select_address_result->fetch(PDO::FETCH_ASSOC))
          // No need for any checks, as you are getting unique addresses only
          $addresses_list = $row;







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 10:57

























          answered Nov 10 at 10:41









          Madhur Bhaiya

          18.8k62236




          18.8k62236











          • Hello again, Can you help me to show the number of repeats?
            – Pardis Ak
            Nov 17 at 8:39










          • @PardisAk what do you mean ? Can you post a new question for the same.
            – Madhur Bhaiya
            Nov 17 at 8:45










          • I mean: I want to show the number of repetitions of an address. for example, if an address repeats 4 times I show a row of like this: Paris-4
            – Pardis Ak
            Nov 19 at 5:29

















          • Hello again, Can you help me to show the number of repeats?
            – Pardis Ak
            Nov 17 at 8:39










          • @PardisAk what do you mean ? Can you post a new question for the same.
            – Madhur Bhaiya
            Nov 17 at 8:45










          • I mean: I want to show the number of repetitions of an address. for example, if an address repeats 4 times I show a row of like this: Paris-4
            – Pardis Ak
            Nov 19 at 5:29
















          Hello again, Can you help me to show the number of repeats?
          – Pardis Ak
          Nov 17 at 8:39




          Hello again, Can you help me to show the number of repeats?
          – Pardis Ak
          Nov 17 at 8:39












          @PardisAk what do you mean ? Can you post a new question for the same.
          – Madhur Bhaiya
          Nov 17 at 8:45




          @PardisAk what do you mean ? Can you post a new question for the same.
          – Madhur Bhaiya
          Nov 17 at 8:45












          I mean: I want to show the number of repetitions of an address. for example, if an address repeats 4 times I show a row of like this: Paris-4
          – Pardis Ak
          Nov 19 at 5:29





          I mean: I want to show the number of repetitions of an address. for example, if an address repeats 4 times I show a row of like this: Paris-4
          – Pardis Ak
          Nov 19 at 5:29


















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237974%2fskip-adding-to-array-if-it-exists-in-php%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo