Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3636264
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:00:33+00:00 2026-05-19T01:00:33+00:00

How can I get the following code to work? $a = explode(‘s’, $str)[0]; I

  • 0

How can I get the following code to work?

$a = explode(‘s’, $str)[0];

I only see solutions looking like this:

$a = explode(‘s’, $str); $a=$a[0];

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T01:00:34+00:00Added an answer on May 19, 2026 at 1:00 am

    As others have said, PHP is unlike JavaScript in that it can’t access array elements from function returns.
    The second method you listed works. You can also grab the first element of the array with the current(), reset(), or array_pop() functions like so:

    $a = current( explode( 's', $str ) ); //or
    $a = reset( explode( 's', $str ) ); //or
    $a = array_pop ( explode( 's', $str ) );
    

    If you would like to remove the slight overhead that explode may cause due to multiple separations, you can set its limit to 2 by passing two after the other arguments. You may also consider using str_pos and strstr instead:

    $a = substr( $str, 0, strpos( $str, 's' ) );
    

    Any of these choices will work.

    EDIT Another way would be to use list() (see PHP doc). With it you can grab any element:

    list( $first ) = explode( 's', $str ); //First
    list( ,$second ) = explode( 's', $str ); //Second
    list( ,,$third ) = explode( 's', $str ); //Third
    //etc.
    

    That not your style? You can always write a small helper function to grab elements from functions that return arrays:

    function array_grab( $arr, $key ) { return( $arr[$key] ); }
    
    $part = array_grab( explode( 's', $str ), 0 ); //Usage: 1st element, etc.
    

    EDIT: PHP 5.4 will support array dereferencing, so you will be able to do:

    $first_element = explode(',','A,B,C')[0];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code, but I can't get it to work. As soon
I can't get the following code to work. It's working with normal select or
I am trying to get the following code to work so I can call
How can I make the following code work? During compilation I get an error
Can anyone figure out a nice way to get the following code to work?
i can't get the following code to work: $('#clicked_package').css({background-image : url(img/head_2.png)}).fadeOut(slow); $('#clicked_package').css({background-image : url(img/head_2_normal.png)}).fadeIn(slow);
I'm trying to get the following code to work, but I can't find good-enough
I can't seem to get the following code to work? $playerfileLocation = ../wp-content/gallery/playerphotos/' .
I'm new to Javascript and for some reason I can't get the following code
In the following code, the variadic constructor is called twice. How can I get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.