is there a way to leave a certain field out?

for instance, I'm using http_build_query() to build a new query and I have page_num=2&color_id=5&start=5 and all I want to do is keep color_id=5&start=5 .. I don't want/need the page_num info

How can I take that out of the array?

    unset the elements, or create an array with only the keys you want to keep and either use one of the array merging functions to keep the ones you need, or loop through with foreach and filter the ones you don't want.

      An example of using the unset() way:

      $ary = array('page_num' => 2, 'color_id' => 5, 'start' => 5); // If have this defined
      
      $new_array = $ary;  // Make a copy of it so not to destroy the original
      
      if (isSet($new_array['page_num']))   // The key you want to remove is there?
          unset($new_array['page_num']);  // Remove it
      
      $query= http_build_query($new_array);  // PHP 5 function
      

      🙂

        so, unset is what I need to take something out?

        awesome.. thanks a lot.

          .

          🙂 Hey!
          halojoy here
          Just a little addition, as this is resolved.

          To unset and delete one value from an indexed array.

          And fix indexes so that they match set values
          and be able to use for() loop listing and/or address any indexed values directly without looping.

          Indexed array is a numbered array, like:
          $my_arr[0], $my_arr[1], $my_arr[2], $my_arr[3], $my_arr[4]

          Note: corrupted indexes, after unset, does not effect foreach() loops
          as foreach() only includes index/element that isset.

          I have run and tested this script.
          It shows what can happen and howto fix it.

          /yours halojoy
          in late northern swedish summer

          <?php
          error_reporting( E_ALL &~ E_NOTICE );
          
          // This is not a problem when using foreach() loop
          // But with foreach you can not address 1 value
          // without looping to find it ( takes time )
          
          // Task: to delete "jack" and make an array with 5 elements
          // Each value can be addressed with its index
          // and for() loop can be used to list without problem
          // indexes will finally be [0]  [1]  [2]  [3]  [4],  as "0" is used for first index
          
          // Create array with 5 elements + one disturbing "jack"
          $my_arr = array( "zero","one","jack","two","three","four" );
          
          $num = count($my_arr);
          echo "Step1<br>
          Count returns $num set values<br>
          and same number of indexes exist<br>";
          // display original values and find index of "jack"
          for ($i=0;$i<$num;$i++) {
             echo "$i $my_arr[$i] <br>";
             if( $my_arr[$i] == "jack" ) $idx = $i;
          }
          
          // unset index with "jack"
          unset( $my_arr[$id ] );
          
          $num = count($my_arr);
          echo "<hr>Step2<br>'jack' was unset ( index = $idx )<br>
          Count returns $num set values<br>
          but original number of indexes still exist<br>
          <font color='red'>So last value is NOT within for() loop</font><br>";
          for ($i=0;$i<$num;$i++) {
             echo "$i $my_arr[$i] <br>";
          }
          
          // Make a new array, of indexed elements that have VALUE,
          // that is, any element that isset ( not unset )
          // function for this is:      array_values( $arr_name )
          // To fix index numbering after unset,
          // instead of 2 lines like here, you can make it:
          //    $my_arr = array_values( $my_arr );
          
          $new_arr = array_values( $my_arr );
          $my_arr = $new_arr;
          
          $num = count($my_arr);
          echo "<hr>Step3<br>
          function  array_values() was run<br>
          Count returns $num set values<br>
          and number of indexes match again<br>
          <font color='green'>All values are within for() loop</font><br>";
          for ($i=0;$i<$num;$i++) {
             echo "$i $my_arr[$i] <br>";
          }
          
          echo "<hr><b><i>THE END</i></b>";
          exit;
          
          ?>

            Yes, I know about array_values() and I didn't use it because http_build_querry() is not affected by "corrupted indexes" as you call it.

            These lines:

            $new_arr = array_values( $my_arr );
            $my_arr = $new_arr;

            can be reduced to simply this:

            $my_arr = array_values($my_arr);

            And if you really wanted to assign it to $new_arr as well, then this is a short cut:

            $new_arr = $my_arr = array_values($my_arr);

            .

              yes

              toplay
              you are right in all you say

              [right]As would be understood of my reply
              this was ment more as a little additional sidenote that targets
              topic of taking 1 or 2 elements or out of an array - using unset( $var["x"] )
              4/5 normal knowledge level php users might not be aware of this
              and it is not unlikely some could get problem
              when using [man]unset[/man] function like in this case

              More knowledge
              not only for halojoy & toplay but for everyone
              is our goal.[/right]

              <?php
              
              // Make a new array, of indexed elements that have VALUE, 
              // that is, any element that isset ( not unset ) 
              // function for this is:      array_values( $arr_name ) 
              // To fix index numbering after unset, 
              // instead of 2 lines like here, you can make it: 
              //    $my_arr = array_values( $my_arr );
              
              ?>

              🆒 [right] 🙂 🙂 🙂 🙂 [/right]

                Why do you keep referring to yourself in the third person? Why not "I" or "me".

                .

                  Write a Reply...