Solved

explode an array

Posted on 2008-06-09
11
244 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 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
php ssh2_scp_send 1 49
PHP - AJAX and MySQL it works only if the value is a number 12 36
get domain with php 7 20
WP_Query multiple custom taxonomies 5 28
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to count occurrences of each item in an array.
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.

920 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

16 Experts available now in Live!

Get 1:1 Help Now