elements from functions that return arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • M. Katz

    elements from functions that return arrays

    I'd like to access a single element from a function that returns an
    array; and I'd like to do it in a single statement.

    Here's an example. This is obvious.
    $a = explode(".", "this.is.some.t ext");
    $b = $a[2];

    So how can I do that in a single statement?
    $a = explode(".", "this.is.some.t ext")[2] // no
    $a = (explode(".", "this.is.some.t ext"))[2] // nope
    $a = [explode(".", "this.is.some.t ext")][2] // no way

    How close am I coming to the final result. My goal is to be able to
    nest them.

    I was looking for an answer in the array functions, but the ones I
    found don't return a scalar value, they return arrays in my cases.

    Thanks,
    M. Katz
  • CountScubula

    #2
    Re: elements from functions that return arrays

    I do not know why it must be in one call, but you can wrap it to look like
    its one call:

    $s = "this.is.some.t ext";
    print strElement($s," .",2);



    function strElement($s,$ d,$i)
    {
    $r = explode($d,$s);
    return $r[$i];
    }


    --
    Mike Bradley
    http://www.gzentools.com -- free online php tools
    "M. Katz" <MKatz843@onebo x.com> wrote in message
    news:4a097d6a.0 401310007.729aa 679@posting.goo gle.com...[color=blue]
    > I'd like to access a single element from a function that returns an
    > array; and I'd like to do it in a single statement.
    >
    > Here's an example. This is obvious.
    > $a = explode(".", "this.is.some.t ext");
    > $b = $a[2];
    >
    > So how can I do that in a single statement?
    > $a = explode(".", "this.is.some.t ext")[2] // no
    > $a = (explode(".", "this.is.some.t ext"))[2] // nope
    > $a = [explode(".", "this.is.some.t ext")][2] // no way
    >
    > How close am I coming to the final result. My goal is to be able to
    > nest them.
    >
    > I was looking for an answer in the array functions, but the ones I
    > found don't return a scalar value, they return arrays in my cases.
    >
    > Thanks,
    > M. Katz[/color]


    Comment

    • M. Katz

      #3
      Re: elements from functions that return arrays

      Thanks, Count,
      The definitely works. But for the future, is there a way to extract
      array elements from a function that returns arrays--in one step?
      In general? a la...

      $element1_2 = (myFunction($a, $b))[1][2];

      Maybe someone knows whether this IS or IS NOT possible in PHP.

      Thanks!
      M. Katz

      "CountScubu la" <me@scantek.hot mail.com> wrote in message news:<RCKSb.182 80$Ba1.9290@new ssvr27.news.pro digy.com>...[color=blue]
      > I do not know why it must be in one call, but you can wrap it to look like
      > its one call:
      >
      > $s = "this.is.some.t ext";
      > print strElement($s," .",2);
      >
      >
      >
      > function strElement($s,$ d,$i)
      > {
      > $r = explode($d,$s);
      > return $r[$i];
      > }
      >
      >
      > --
      > Mike Bradley
      > http://www.gzentools.com -- free online php tools
      > "M. Katz" <MKatz843@onebo x.com> wrote in message
      > news:4a097d6a.0 401310007.729aa 679@posting.goo gle.com...[color=green]
      > > I'd like to access a single element from a function that returns an
      > > array; and I'd like to do it in a single statement.
      > >
      > > Here's an example. This is obvious.
      > > $a = explode(".", "this.is.some.t ext");
      > > $b = $a[2];
      > >
      > > So how can I do that in a single statement?
      > > $a = explode(".", "this.is.some.t ext")[2] // no
      > > $a = (explode(".", "this.is.some.t ext"))[2] // nope
      > > $a = [explode(".", "this.is.some.t ext")][2] // no way
      > >
      > > How close am I coming to the final result. My goal is to be able to
      > > nest them.
      > >
      > > I was looking for an answer in the array functions, but the ones I
      > > found don't return a scalar value, they return arrays in my cases.
      > >
      > > Thanks,
      > > M. Katz[/color][/color]

      Comment

      • Chung Leong

        #4
        Re: elements from functions that return arrays

        It is NOT possible.

        Uzytkownik "M. Katz" <MKatz843@onebo x.com> napisal w wiadomosci
        news:4a097d6a.0 401311202.448a6 1b3@posting.goo gle.com...[color=blue]
        > Thanks, Count,
        > The definitely works. But for the future, is there a way to extract
        > array elements from a function that returns arrays--in one step?
        > In general? a la...
        >
        > $element1_2 = (myFunction($a, $b))[1][2];
        >
        > Maybe someone knows whether this IS or IS NOT possible in PHP.
        >
        > Thanks!
        > M. Katz
        >
        > "CountScubu la" <me@scantek.hot mail.com> wrote in message[/color]
        news:<RCKSb.182 80$Ba1.9290@new ssvr27.news.pro digy.com>...[color=blue][color=green]
        > > I do not know why it must be in one call, but you can wrap it to look[/color][/color]
        like[color=blue][color=green]
        > > its one call:
        > >
        > > $s = "this.is.some.t ext";
        > > print strElement($s," .",2);
        > >
        > >
        > >
        > > function strElement($s,$ d,$i)
        > > {
        > > $r = explode($d,$s);
        > > return $r[$i];
        > > }
        > >
        > >
        > > --
        > > Mike Bradley
        > > http://www.gzentools.com -- free online php tools
        > > "M. Katz" <MKatz843@onebo x.com> wrote in message
        > > news:4a097d6a.0 401310007.729aa 679@posting.goo gle.com...[color=darkred]
        > > > I'd like to access a single element from a function that returns an
        > > > array; and I'd like to do it in a single statement.
        > > >
        > > > Here's an example. This is obvious.
        > > > $a = explode(".", "this.is.some.t ext");
        > > > $b = $a[2];
        > > >
        > > > So how can I do that in a single statement?
        > > > $a = explode(".", "this.is.some.t ext")[2] // no
        > > > $a = (explode(".", "this.is.some.t ext"))[2] // nope
        > > > $a = [explode(".", "this.is.some.t ext")][2] // no way
        > > >
        > > > How close am I coming to the final result. My goal is to be able to
        > > > nest them.
        > > >
        > > > I was looking for an answer in the array functions, but the ones I
        > > > found don't return a scalar value, they return arrays in my cases.
        > > >
        > > > Thanks,
        > > > M. Katz[/color][/color][/color]


        Comment

        • Brad Kent

          #5
          Re: elements from functions that return arrays

          I posted this question back in May. Someone responded with

          $a = = array_slice(exp lode(".", "this.is.some.t ext"), 2, 1);

          unless I somehow goofed, $a should now be 'some'

          MKatz843@onebox .com (M. Katz) wrote in message news:<4a097d6a. 0401310007.729a a679@posting.go ogle.com>...[color=blue]
          > I'd like to access a single element from a function that returns an
          > array; and I'd like to do it in a single statement.
          >
          > Here's an example. This is obvious.
          > $a = explode(".", "this.is.some.t ext");
          > $b = $a[2];
          >
          > So how can I do that in a single statement?
          > $a = explode(".", "this.is.some.t ext")[2] // no
          > $a = (explode(".", "this.is.some.t ext"))[2] // nope
          > $a = [explode(".", "this.is.some.t ext")][2] // no way
          >
          > How close am I coming to the final result. My goal is to be able to
          > nest them.
          >
          > I was looking for an answer in the array functions, but the ones I
          > found don't return a scalar value, they return arrays in my cases.
          >
          > Thanks,
          > M. Katz[/color]

          Comment

          Working...