Solved

explode an array

Posted on 2008-06-09
11
242 Views
Last Modified: 2013-12-13
I have a variable like this

$var = $_get['value'];

so echo $var is number_1234 or number_5467 and so on
I wanna get rid of number_
so i tried
$newvar = explode('_',$var);
and it just echo out Array
can someone help
Thanks
0
Comment
Question by:syedasimmeesaq
  • 5
  • 3
  • 3
11 Comments
 
LVL 24

Expert Comment

by:glcummins
Comment Utility
list($firstpart, $newvar) = explode('-', $var);
0
 
LVL 24

Accepted Solution

by:
glcummins earned 500 total points
Comment Utility
Sorry, I typed a hypen instead of an underscore:

 list($firstpart, $newvar) = explode('_', $var);

The explode() function returns an array. Each element of the array will contain the part of the string between the delimiter.

The list() function allows you to take an array and break it into individual variables. In this case, you will only use the $newvar variable, and can simply ignore the $firstpart variable.
0
 
LVL 4

Expert Comment

by:albuitra
Comment Utility
with explode in newvar you have an array
$newvar[0] = 'number_'
$newvar[0] = 'XXXX'
0
 
LVL 9

Author Comment

by:syedasimmeesaq
Comment Utility
hmm I tried that and it still echos Array instead of values.
Thanks
0
 
LVL 4

Expert Comment

by:albuitra
Comment Utility
excuseme:
$newvar[1] = 'XXXX'
if you have 3 parts, for example
number_123_456
with explode you obtain:
$newvar[0] = 'number_'
$newvar[1] = '123_'
$newvar[2] = '456'
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 24

Expert Comment

by:glcummins
Comment Utility
Are you sure that $var is indeed a string containing 'number_xxxx'? Can you post the output of:

var_dump($var);
0
 
LVL 9

Author Comment

by:syedasimmeesaq
Comment Utility
albuitra, I didn't get you. Could you expalin a bit more.
Thanks
0
 
LVL 9

Author Comment

by:syedasimmeesaq
Comment Utility
var_dump($var) gives me this

1234756 string(15) "numbers_123456"

Thanks
0
 
LVL 9

Author Comment

by:syedasimmeesaq
Comment Utility
aah I think it worked by glcummins's method.
Thanks!!
0
 
LVL 9

Author Closing Comment

by:syedasimmeesaq
Comment Utility
Thanks!!
0
 
LVL 4

Expert Comment

by:albuitra
Comment Utility
With explode, you obtain an array, that contains the original string splits by the character you choose
For example:
$original_string = 'This_is_a_test'
$parts = explode ('_', 'This_is_a_test');

So you now have that $parts is an array
$parts[0] = 'This'
$parts[1] = 'is'
$parts[2] = 'a'
$parts[3] = 'test'
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now