?
Solved

explode an array

Posted on 2008-06-09
11
Medium Priority
?
254 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
ID: 21746465
list($firstpart, $newvar) = explode('-', $var);
0
 
LVL 24

Accepted Solution

by:
glcummins earned 2000 total points
ID: 21746480
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
ID: 21746493
with explode in newvar you have an array
$newvar[0] = 'number_'
$newvar[0] = 'XXXX'
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Author Comment

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

Expert Comment

by:albuitra
ID: 21746512
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
 
LVL 24

Expert Comment

by:glcummins
ID: 21746524
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
ID: 21746527
albuitra, I didn't get you. Could you expalin a bit more.
Thanks
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 21746548
var_dump($var) gives me this

1234756 string(15) "numbers_123456"

Thanks
0
 
LVL 9

Author Comment

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

Author Closing Comment

by:syedasimmeesaq
ID: 31465552
Thanks!!
0
 
LVL 4

Expert Comment

by:albuitra
ID: 21746593
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

840 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