1

I wonder if they disabled array_shift in > PHP 5.2.6

$realid = array_shift(explode("-", $id));

Because this code was working fine on my server PHP Version 5.2.6, while is not working in another server with higher PHP version.

If so, is there anyway I can do the following:

For a URL like this 87262-any-thing-here.html how can I get only the number, 87262, so that I will use it to call any entry from database:

$qryrec="select * from mytable where id='$realid'";
$resultrec=mysql_query($qryrec) or die($qryrec);
$linerec=mysql_fetch_array($resultrec);

Is there any way to do the same without array_shift?

7
  • It works fine on 5.3. Anyway you can do the same using regexp Commented Dec 28, 2011 at 22:32
  • 3
    sigh "Not working" is never a good error description. What happens or doesn't happen? What errors do you get? Commented Dec 28, 2011 at 22:32
  • 1
    You should rather use current(explode(..)) anyway, or better yet strtok($id, "-") if you only want the first part. Commented Dec 28, 2011 at 22:35
  • 1
    php -r 'var_dump(array_shift(explode("-", "87262-any-thing-here.html")));' Prints string(5) "87262". Update your question and provide full inputs and expected outputs. Commented Dec 28, 2011 at 22:40
  • 1
    strtok($id, "-") works perfect ~ thanks everybody Commented Dec 28, 2011 at 23:07

2 Answers 2

3

Use

$realid = explode("-", $id);
$realid = $realid[0];
Sign up to request clarification or add additional context in comments.

1 Comment

1.) I use the $realid = $realid[0]; construct allways if I don't need the rest of the array, as it frees the memory used by the rest. 2.) From my reading the construct to reading the first element of an int-based array is $array[0]. 3.) AFAIK array_shift() needs a reference parameter. The output of a function is not one. This might have easily become stricter or less strict in different PHP versions. 4.) Not trusting in PHP "working as I intend, not as I write" seems like a good idea.
2

Edit: To obtain the decimal value at the beginning of a string, you can use sscanf:

$url = '87262-any-thing-here.html';
list($realid) = sscanf($url, '%d');

In case the URL has no decimal number at the beginning, $realid will be NULL. With explode you will get an undefined result depending on URL.


array_shift­Docs by it's function reference needs a variable:


enter image description here


(see as well: Passing by Reference)

But you give it a function:

 $realid = array_shift(explode("-", $id));

I would not expect it to always properly work because of that. Additionally this can trigger warnings and errors on some installations.

Instead use a variable:

 $ids = explode("-", $id);
 $realid = array_shift($ids);
 unset($ids);

Or in your case:

 list($realid) = explode("-", $id);

which will assing the first element of the array returned by explode to $realid. See list­Docs.

11 Comments

What are you talking about? array_shift(explode("-", $id)); works fine (I think it throws a E_STRICT warning in PHP 5.3 though).
php will simply evaluate the explode() before passing it as an argument. and explode returns an array so it is fine.
@Rocket: Now as you know the problem, here is a PHP hack that prevents the warning ;) (but I discourage using it for production code because this might not work in the future): codepad.viper-7.com/BOtWEU
@David Chan: But the returned array is not in variable context. See my previous comment how you can get into that context somehow, if you dislike to use a variable.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.