Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Best way to break apart this string?
Old 01-02-2012, 06:14 PM Best way to break apart this string?
Super Talker

Posts: 113
Trades: 0
I have a string given in the following format:

Quote:
a:1:{i:2;s:1:"0";}
I don't know why it's like that, but it is and I can't change it.

The letters do not change, but the numbers (i.e. the 1, 2, 1, and 0) do change and can be any positive value. I need to extract the numbers from the string and put them in an array.

How can I do this?

Thanks
Learnin' n00b is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-02-2012, 06:30 PM Re: Best way to break apart this string?
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
That string is a serialized array.

PHP Code:
print_runserialize('a:1:{i:2;s:1:"0";}') ); 
yields:
Code:
Array
(
    [2] => 0
)
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 01-02-2012, 06:54 PM Re: Best way to break apart this string?
Super Talker

Posts: 113
Trades: 0
Hi, thanks so much for your reply!

I'm a little confused by something. Once I unserialize the string and get this:

Code:
Array
(
    [2] => 0
)
How is the first item in the array at index 2? Shouldn't there be something at index 0 and 1? Like this:

Code:
Array
(
    [0] => 0
    [1] => 0
    [2] => 0
)
Learnin' n00b is offline
Reply With Quote
View Public Profile
 
Old 01-02-2012, 07:23 PM Re: Best way to break apart this string?
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
PHP arrays are actually maps. Indexing is always done by key, not position.

PHP Code:
$foo = array( => 1=> 2=> );

print_r($foo); 
Code:
Array
(
    [2] => 1
    [0] => 2
    [1] => 3
)
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 01-02-2012 at 07:25 PM..
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 01-03-2012, 08:44 AM Re: Best way to break apart this string?
Super Talker

Posts: 113
Trades: 0
Thanks
Learnin' n00b is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Best way to break apart this string?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.77773 seconds with 12 queries