How do I remove empty string elements from my array?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • laredotornado@zipmail.com

    How do I remove empty string elements from my array?

    Hi,

    I'm using PHP 5. I have an array of strings. What is the simplest
    way to remove the elements that are empty, i.e. where the expression
    "empty($elt )" returns true?

    Thanks, - Dave
  • =?UTF-8?B?SXbDoW4gU8OhbmNoZXogT3J0ZWdh?=

    #2
    Re: How do I remove empty string elements from my array?

    laredotornado@z ipmail.com wrote:
    I'm using PHP 5. I have an array of strings. What is the simplest
    way to remove the elements that are empty, i.e. where the expression
    "empty($elt )" returns true?
    It depends on your definition of "simple". For example, this single line
    will do:

    foreach($array as &$value) $value || unset($value);

    However, you should ask yourself why you have empty elements in the first
    place, if you don't want them.

    Cheers,
    --
    ----------------------------------
    Iván Sánchez Ortega -ivan-algarroba-sanchezortega-punto-es-

    286+100=386,+10 0=486,+100=586. 00000011. Bueno... llamémosle Pentium.

    Comment

    • Jerry Stuckle

      #3
      Re: How do I remove empty string elements from my array?

      Iván Sánchez Ortega wrote:
      laredotornado@z ipmail.com wrote:
      >
      >I'm using PHP 5. I have an array of strings. What is the simplest
      >way to remove the elements that are empty, i.e. where the expression
      >"empty($elt) " returns true?
      >
      It depends on your definition of "simple". For example, this single line
      will do:
      >
      foreach($array as &$value) $value || unset($value);
      >
      However, you should ask yourself why you have empty elements in the first
      place, if you don't want them.
      >
      Cheers,
      Better is:

      foreach ($array as $key=>$value)
      if (empty($value))
      unset($array[$key]);

      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstucklex@attgl obal.net
      =============== ===

      Comment

      • internetwebthing

        #4
        Re: How do I remove empty string elements from my array?

        On Mon, 03 Nov 2008 13:00:30 -0500, Jerry Stuckle
        <jstucklex@attg lobal.netwrote:
        >Iván Sánchez Ortega wrote:
        >laredotornado@z ipmail.com wrote:
        >>
        >>I'm using PHP 5. I have an array of strings. What is the simplest
        >>way to remove the elements that are empty, i.e. where the expression
        >>"empty($elt )" returns true?
        >>
        >It depends on your definition of "simple". For example, this single line
        >will do:
        >>
        >foreach($arr ay as &$value) $value || unset($value);
        >>
        >However, you should ask yourself why you have empty elements in the first
        >place, if you don't want them.
        >>
        >Cheers,
        >
        >Better is:
        >
        >foreach ($array as $key=>$value)
        if (empty($value))
        unset($array[$key]);
        or:

        $array = array_filter($a rray);

        from the php docs:

        --------------------------------------------------------------------------------
        array array_filter ( array $input [, callback $callback ] )

        If no callback is supplied, all entries of input equal to FALSE (see
        converting to boolean) will be removed.
        --------------------------------------------------------------------------------

        Therefore empty strings will be filtered out, because they equate to
        FALSE.

        I have come across a situation where I wanted to remove empty strings
        from an array (reading contents of a text file) and have encountered
        stings of whitespace which I also wanted filtered out. In that
        instance I used:

        $array = array_filter(ar ray_map('trim', $array));

        HTH


        Comment

        • Jerry Stuckle

          #5
          Re: How do I remove empty string elements from my array?

          internetwebthin g wrote:
          On Mon, 03 Nov 2008 13:00:30 -0500, Jerry Stuckle
          <jstucklex@attg lobal.netwrote:
          >
          >Iván Sánchez Ortega wrote:
          >>laredotornado@z ipmail.com wrote:
          >>>
          >>>I'm using PHP 5. I have an array of strings. What is the simplest
          >>>way to remove the elements that are empty, i.e. where the expression
          >>>"empty($elt) " returns true?
          >>It depends on your definition of "simple". For example, this single line
          >>will do:
          >>>
          >>foreach($arra y as &$value) $value || unset($value);
          >>>
          >>However, you should ask yourself why you have empty elements in the first
          >>place, if you don't want them.
          >>>
          >>Cheers,
          >Better is:
          >>
          >foreach ($array as $key=>$value)
          > if (empty($value))
          > unset($array[$key]);
          >
          or:
          >
          $array = array_filter($a rray);
          >
          from the php docs:
          >
          --------------------------------------------------------------------------------
          array array_filter ( array $input [, callback $callback ] )
          >
          If no callback is supplied, all entries of input equal to FALSE (see
          converting to boolean) will be removed.
          --------------------------------------------------------------------------------
          >
          Therefore empty strings will be filtered out, because they equate to
          FALSE.
          >
          I have come across a situation where I wanted to remove empty strings
          from an array (reading contents of a text file) and have encountered
          stings of whitespace which I also wanted filtered out. In that
          instance I used:
          >
          $array = array_filter(ar ray_map('trim', $array));
          >
          HTH
          >
          >
          >
          Yes, but it could also get other entries, i.e. those which contain 0.
          You need to beware of possible side effects!

          --
          =============== ===
          Remove the "x" from my email address
          Jerry Stuckle
          JDS Computer Training Corp.
          jstucklex@attgl obal.net
          =============== ===

          Comment

          • internetwebthing

            #6
            Re: How do I remove empty string elements from my array?

            On Mon, 03 Nov 2008 14:28:06 -0500, Jerry Stuckle
            <jstucklex@attg lobal.netwrote:
            >internetwebthi ng wrote:
            >On Mon, 03 Nov 2008 13:00:30 -0500, Jerry Stuckle
            ><jstucklex@att global.netwrote :
            >>
            >>Iván Sánchez Ortega wrote:
            >>>laredotornado@z ipmail.com wrote:
            >>>>
            >>>>I'm using PHP 5. I have an array of strings. What is the simplest
            >>>>way to remove the elements that are empty, i.e. where the expression
            >>>>"empty($elt )" returns true?
            >>>It depends on your definition of "simple". For example, this single line
            >>>will do:
            >>>>
            >>>foreach($arr ay as &$value) $value || unset($value);
            >>>>
            >>>However, you should ask yourself why you have empty elements in the first
            >>>place, if you don't want them.
            >>>>
            >>>Cheers,
            >>Better is:
            >>>
            >>foreach ($array as $key=>$value)
            >> if (empty($value))
            >> unset($array[$key]);
            >>
            >or:
            >>
            >$array = array_filter($a rray);
            >>
            >from the php docs:
            >>
            >--------------------------------------------------------------------------------
            >array array_filter ( array $input [, callback $callback ] )
            >>
            >If no callback is supplied, all entries of input equal to FALSE (see
            >converting to boolean) will be removed.
            >--------------------------------------------------------------------------------
            >>
            >Therefore empty strings will be filtered out, because they equate to
            >FALSE.
            >>
            >I have come across a situation where I wanted to remove empty strings
            >from an array (reading contents of a text file) and have encountered
            >stings of whitespace which I also wanted filtered out. In that
            >instance I used:
            >>
            >$array = array_filter(ar ray_map('trim', $array));
            >>
            >HTH
            >>
            >>
            >>
            >
            >Yes, but it could also get other entries, i.e. those which contain 0.
            >You need to beware of possible side effects!
            True, but not an issue for my purposes.

            Comment

            • Jerry Stuckle

              #7
              Re: How do I remove empty string elements from my array?

              internetwebthin g wrote:
              On Mon, 03 Nov 2008 14:28:06 -0500, Jerry Stuckle
              <jstucklex@attg lobal.netwrote:
              >
              >internetwebthi ng wrote:
              >>On Mon, 03 Nov 2008 13:00:30 -0500, Jerry Stuckle
              >><jstucklex@at tglobal.netwrot e:
              >>>
              >>>Iván Sánchez Ortega wrote:
              >>>>laredotornado@z ipmail.com wrote:
              >>>>>
              >>>>>I'm using PHP 5. I have an array of strings. What is the simplest
              >>>>>way to remove the elements that are empty, i.e. where the expression
              >>>>>"empty($el t)" returns true?
              >>>>It depends on your definition of "simple". For example, this single line
              >>>>will do:
              >>>>>
              >>>>foreach($ar ray as &$value) $value || unset($value);
              >>>>>
              >>>>However, you should ask yourself why you have empty elements in the first
              >>>>place, if you don't want them.
              >>>>>
              >>>>Cheers,
              >>>Better is:
              >>>>
              >>>foreach ($array as $key=>$value)
              >>> if (empty($value))
              >>> unset($array[$key]);
              >>or:
              >>>
              >>$array = array_filter($a rray);
              >>>
              >>from the php docs:
              >>>
              >>--------------------------------------------------------------------------------
              >>array array_filter ( array $input [, callback $callback ] )
              >>>
              >>If no callback is supplied, all entries of input equal to FALSE (see
              >>converting to boolean) will be removed.
              >>--------------------------------------------------------------------------------
              >>>
              >>Therefore empty strings will be filtered out, because they equate to
              >>FALSE.
              >>>
              >>I have come across a situation where I wanted to remove empty strings
              >>from an array (reading contents of a text file) and have encountered
              >>stings of whitespace which I also wanted filtered out. In that
              >>instance I used:
              >>>
              >>$array = array_filter(ar ray_map('trim', $array));
              >>>
              >>HTH
              >>>
              >>>
              >>>
              >Yes, but it could also get other entries, i.e. those which contain 0.
              >You need to beware of possible side effects!
              >
              True, but not an issue for my purposes.
              >
              >
              Just because it's not an issue for your purposes does not mean it's a
              good idea to recommend it to someone else.

              --
              =============== ===
              Remove the "x" from my email address
              Jerry Stuckle
              JDS Computer Training Corp.
              jstucklex@attgl obal.net
              =============== ===

              Comment

              • internetwebthing

                #8
                Re: How do I remove empty string elements from my array?

                On Mon, 03 Nov 2008 20:42:06 -0500, Jerry Stuckle
                <jstucklex@attg lobal.netwrote:
                >internetwebthi ng wrote:
                >On Mon, 03 Nov 2008 14:28:06 -0500, Jerry Stuckle
                ><jstucklex@att global.netwrote :
                >>
                >>internetwebth ing wrote:
                >>>On Mon, 03 Nov 2008 13:00:30 -0500, Jerry Stuckle
                >>><jstucklex@a ttglobal.netwro te:
                >>>>
                >>>>Iván Sánchez Ortega wrote:
                >>>>>laredotornado@z ipmail.com wrote:
                >>>>>>
                >>>>>>I'm using PHP 5. I have an array of strings. What is the simplest
                >>>>>>way to remove the elements that are empty, i.e. where the expression
                >>>>>>"empty($e lt)" returns true?
                >>>>>It depends on your definition of "simple". For example, this single line
                >>>>>will do:
                >>>>>>
                >>>>>foreach($a rray as &$value) $value || unset($value);
                >>>>>>
                >>>>>However, you should ask yourself why you have empty elements in the first
                >>>>>place, if you don't want them.
                >>>>>>
                >>>>>Cheers,
                >>>>Better is:
                >>>>>
                >>>>foreach ($array as $key=>$value)
                >>>> if (empty($value))
                >>>> unset($array[$key]);
                >>>or:
                >>>>
                >>>$array = array_filter($a rray);
                >>>>
                >>>from the php docs:
                >>>>
                >>>--------------------------------------------------------------------------------
                >>>array array_filter ( array $input [, callback $callback ] )
                >>>>
                >>>If no callback is supplied, all entries of input equal to FALSE (see
                >>>converting to boolean) will be removed.
                >>>--------------------------------------------------------------------------------
                >>>>
                >>>Therefore empty strings will be filtered out, because they equate to
                >>>FALSE.
                >>>>
                >>>I have come across a situation where I wanted to remove empty strings
                >>>from an array (reading contents of a text file) and have encountered
                >>>stings of whitespace which I also wanted filtered out. In that
                >>>instance I used:
                >>>>
                >>>$array = array_filter(ar ray_map('trim', $array));
                >>>>
                >>>HTH
                >>>>
                >>>>
                >>>>
                >>Yes, but it could also get other entries, i.e. those which contain 0.
                >>You need to beware of possible side effects!
                >>
                >True, but not an issue for my purposes.
                >>
                >>
                >
                >Just because it's not an issue for your purposes does not mean it's a
                >good idea to recommend it to someone else.
                But it /is/ good, in my opinion, to suggest alternatives and discuss
                them without .dismissing them out-of-hand.

                YMMV


                Comment

                • Jerry Stuckle

                  #9
                  Re: How do I remove empty string elements from my array?

                  internetwebthin g wrote:
                  On Mon, 03 Nov 2008 20:42:06 -0500, Jerry Stuckle
                  <jstucklex@attg lobal.netwrote:
                  >
                  >internetwebthi ng wrote:
                  >>On Mon, 03 Nov 2008 14:28:06 -0500, Jerry Stuckle
                  >><jstucklex@at tglobal.netwrot e:
                  >>>
                  >>>internetwebt hing wrote:
                  >>>>On Mon, 03 Nov 2008 13:00:30 -0500, Jerry Stuckle
                  >>>><jstucklex@ attglobal.netwr ote:
                  >>>>>
                  >>>>>Iván Sánchez Ortega wrote:
                  >>>>>>laredotornado@z ipmail.com wrote:
                  >>>>>>>
                  >>>>>>>I'm using PHP 5. I have an array of strings. What is the simplest
                  >>>>>>>way to remove the elements that are empty, i.e. where the expression
                  >>>>>>>"empty($ elt)" returns true?
                  >>>>>>It depends on your definition of "simple". For example, this single line
                  >>>>>>will do:
                  >>>>>>>
                  >>>>>>foreach($ array as &$value) $value || unset($value);
                  >>>>>>>
                  >>>>>>However , you should ask yourself why you have empty elements in the first
                  >>>>>>place, if you don't want them.
                  >>>>>>>
                  >>>>>>Cheers,
                  >>>>>Better is:
                  >>>>>>
                  >>>>>foreach ($array as $key=>$value)
                  >>>>> if (empty($value))
                  >>>>> unset($array[$key]);
                  >>>>or:
                  >>>>>
                  >>>>$array = array_filter($a rray);
                  >>>>>
                  >>>>from the php docs:
                  >>>>>
                  >>>>--------------------------------------------------------------------------------
                  >>>>array array_filter ( array $input [, callback $callback ] )
                  >>>>>
                  >>>>If no callback is supplied, all entries of input equal to FALSE (see
                  >>>>convertin g to boolean) will be removed.
                  >>>>--------------------------------------------------------------------------------
                  >>>>>
                  >>>>Therefore empty strings will be filtered out, because they equate to
                  >>>>FALSE.
                  >>>>>
                  >>>>I have come across a situation where I wanted to remove empty strings
                  >>>>from an array (reading contents of a text file) and have encountered
                  >>>>stings of whitespace which I also wanted filtered out. In that
                  >>>>instance I used:
                  >>>>>
                  >>>>$array = array_filter(ar ray_map('trim', $array));
                  >>>>>
                  >>>>HTH
                  >>>>>
                  >>>>>
                  >>>>>
                  >>>Yes, but it could also get other entries, i.e. those which contain 0.
                  >>>You need to beware of possible side effects!
                  >>True, but not an issue for my purposes.
                  >>>
                  >>>
                  >Just because it's not an issue for your purposes does not mean it's a
                  >good idea to recommend it to someone else.
                  >
                  But it /is/ good, in my opinion, to suggest alternatives and discuss
                  them without .dismissing them out-of-hand.
                  >
                  YMMV
                  >
                  >
                  >
                  Not unless you also discuss the problems they might cause.

                  --
                  =============== ===
                  Remove the "x" from my email address
                  Jerry Stuckle
                  JDS Computer Training Corp.
                  jstucklex@attgl obal.net
                  =============== ===

                  Comment

                  • internetwebthing

                    #10
                    Re: How do I remove empty string elements from my array?

                    On Tue, 04 Nov 2008 17:24:55 -0500, Jerry Stuckle
                    <jstucklex@attg lobal.netwrote:
                    >>Just because it's not an issue for your purposes does not mean it's a
                    >>good idea to recommend it to someone else.
                    >>
                    >But it /is/ good, in my opinion, to suggest alternatives and discuss
                    >them without .dismissing them out-of-hand.
                    >
                    >Not unless you also discuss the problems they might cause.
                    Which has happened. This is a "GOOD THING".

                    What's not so good, IMHO, is that you also shoot me down in flames for
                    daring to suggest an alternative which, in your opinion, is
                    unsuitable.


                    Comment

                    • 703designs

                      #11
                      Re: How do I remove empty string elements from my array?

                      Point to Jerry. InternetWebThin g said:
                      True, but not an issue for my purposes.
                      Jerry gave a solid reason for not using your solution, and it can end
                      there...no need for bickering.

                      Thomas

                      On Nov 4, 7:02 pm, internetwebthin g <s...@spam.spam wrote:
                      On Tue, 04 Nov 2008 17:24:55 -0500, Jerry Stuckle
                      >
                      <jstuck...@attg lobal.netwrote:
                      >Just because it's not an issue for your purposes does not mean it's a
                      >good idea to recommend it to someone else.
                      >
                      But it /is/ good, in my opinion, to suggest alternatives and discuss
                      them without .dismissing them out-of-hand.
                      >
                      Not unless you also discuss the problems they might cause.
                      >
                      Which has happened. This is a "GOOD THING".
                      >
                      What's not so good, IMHO, is that you also shoot me down in flames for
                      daring to suggest an alternative which, in your opinion, is
                      unsuitable.

                      Comment

                      • internetwebthing

                        #12
                        Re: How do I remove empty string elements from my array?

                        On Tue, 4 Nov 2008 16:21:50 -0800 (PST), 703designs
                        <thomasmallen@g mail.comwrote:
                        >Point to Jerry. InternetWebThin g said:
                        >
                        >True, but not an issue for my purposes.
                        >
                        >Jerry gave a solid reason for not using your solution,
                        Jerry gave a good and valid reason why my solution may not be
                        suitable, but not a definitive argument as to why it should never be
                        used. His admonishment of me for suggesting a solution which he finds
                        unsuitable is what I have an issue with here, not his argument against
                        its use.
                        >and it can end there...no need for bickering.
                        Oh I'm sorry.. I didn't realise I was keeping you awake.

                        Comment

                        • 703designs

                          #13
                          Re: How do I remove empty string elements from my array?

                          On Nov 4, 7:42 pm, internetwebthin g <s...@spam.spam wrote:
                          On Tue, 4 Nov 2008 16:21:50 -0800 (PST), 703designs
                          >
                          <thomasmal...@g mail.comwrote:
                          Point to Jerry. InternetWebThin g said:
                          >
                          True, but not an issue for my purposes.
                          >
                          Jerry gave a solid reason for not using your solution,
                          >
                          Jerry gave a good and valid reason why my solution may not be
                          suitable, but not a definitive argument as to why it should never be
                          used. His admonishment of me for suggesting a solution which he finds
                          unsuitable is what I have an issue with here, not his argument against
                          its use.
                          >
                          and it can end there...no need for bickering.
                          >
                          Oh I'm sorry.. I didn't realise I was keeping you awake.
                          "Yes, but it could also get other entries, i.e. those which contain
                          0.
                          You need to beware of possible side effects!"

                          Still hunting for the adminishment.

                          Thomas

                          Comment

                          • internetwebthing

                            #14
                            Re: How do I remove empty string elements from my array?

                            On Tue, 4 Nov 2008 16:49:26 -0800 (PST), 703designs
                            <thomasmallen@g mail.comwrote:
                            >"Yes, but it could also get other entries, i.e. those which contain
                            >0.
                            >You need to beware of possible side effects!"
                            >
                            >Still hunting for the adminishment.
                            Just because it's not an issue for your purposes does not mean it's a
                            good idea to recommend it to someone else.
                            This looks (and feels) like an admonishment to me.

                            I have two further issues with Jerry's statement:

                            1. I was not recommending anything, merely suggesting.

                            2. It is a ridiculous thing to say.

                            In support of issue 2 I would say that this newsgroup would not exist
                            if people did not suggest (or 'recommend', as Jerry seems to cosider
                            any suggestion) solutions which have worked for them in the past.

                            Good night.

                            Comment

                            • Jerry Stuckle

                              #15
                              Re: How do I remove empty string elements from my array?

                              internetwebthin g wrote:
                              On Tue, 4 Nov 2008 16:49:26 -0800 (PST), 703designs
                              <thomasmallen@g mail.comwrote:
                              >
                              >"Yes, but it could also get other entries, i.e. those which contain
                              >0.
                              >You need to beware of possible side effects!"
                              >>
                              >Still hunting for the adminishment.
                              >
                              Just because it's not an issue for your purposes does not mean it's a
                              good idea to recommend it to someone else.
                              >
                              This looks (and feels) like an admonishment to me.
                              >
                              Only after you argued this was a good solution.
                              I have two further issues with Jerry's statement:
                              >
                              1. I was not recommending anything, merely suggesting.
                              >
                              2. It is a ridiculous thing to say.
                              >
                              In support of issue 2 I would say that this newsgroup would not exist
                              if people did not suggest (or 'recommend', as Jerry seems to cosider
                              any suggestion) solutions which have worked for them in the past.
                              >
                              Good night.
                              >
                              >
                              I only take issue with bad solutions. And this is one of them.

                              But you don't see that, despite the reasons I gave.

                              Don't get so stuck on your own solution as to think it's the best way to
                              go.

                              --
                              =============== ===
                              Remove the "x" from my email address
                              Jerry Stuckle
                              JDS Computer Training Corp.
                              jstucklex@attgl obal.net
                              =============== ===

                              Comment

                              Working...