Solved

explode an array

Posted on 2008-06-09
11
250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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
Industry Leaders: 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

Independent Software Vendors: 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!

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

689 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