Solved

explode an array

Posted on 2008-06-09
11
248 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
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!

 
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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
if(empty) validation fires when trying to select 0 from select list 21 59
php error 27 56
Survey branching tutorial 11 39
MySQL-Design Help 12 41
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

756 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