Current location: Hot Scripts Forums » Programming Languages » PHP » Array subscript...


Array subscript...

Reply
  #1 (permalink)  
Old 08-31-09, 12:10 AM
senthilswing senthilswing is offline
Newbie Coder
 
Join Date: Aug 2009
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Array subscript...

<?php
$test = array('aaa','bbb','aaa','ddd','aaa','ccc','ccc');
$array_uni=array_unique($test);
print_r($array_uni);
?>

this is my coding .. for this my output is..
Array ( [0] => aaa [1] => bbb [3] => ddd [5] => ccc )


but i need output like.. Array ( [0] => aaa [1] => bbb [2] => ddd [3] => ccc ) for my further calculation...
Reply With Quote
  #2 (permalink)  
Old 08-31-09, 05:35 AM
dgreenhouse's Avatar
dgreenhouse dgreenhouse is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: San Francisco
Posts: 457
Thanks: 0
Thanked 3 Times in 3 Posts
There are a number of ways to do it, but the simplest to envision the operation is thus:
PHP Code:

$array_in = array('aaa''bbb','aaa','ddd','aaa','ccc','ccc');

print 
'<pre>';
print_r($array_in);
print 
'</pre>';

$array_in array_unique($array_in);

print 
'<pre>';
print_r($array_in);
print 
'</pre>';

$array_out = array();
$j 0;

foreach (
$array_in as $val)
{
  
$array_out[$j++] = $val;
}

print 
'<pre>';
print_r($array_out);
print 
'</pre>';

// Output is:
Array
(
    [
0] => aaa
    
[1] => bbb
    
[2] => aaa
    
[3] => ddd
    
[4] => aaa
    
[5] => ccc
    
[6] => ccc
)
Array
(
    [
0] => aaa
    
[1] => bbb
    
[3] => ddd
    
[5] => ccc
)
Array
(
    [
0] => aaa
    
[1] => bbb
    
[2] => ddd
    
[3] => ccc

Reply With Quote
  #3 (permalink)  
Old 08-31-09, 06:33 AM
senthilswing senthilswing is offline
Newbie Coder
 
Join Date: Aug 2009
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you very much.. This is wat i expect...
Reply With Quote
  #4 (permalink)  
Old 08-31-09, 07:07 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
This is a little easier:
PHP Code:

<?php
$array_in 
= array('aaa''bbb','aaa','ddd','aaa','ccc','ccc');
$array_out array_merge(array_unique($array_in));
print_r($array_out);
?>
__________________
Jerry Broughton
Reply With Quote
  #5 (permalink)  
Old 08-31-09, 07:40 AM
senthilswing senthilswing is offline
Newbie Coder
 
Join Date: Aug 2009
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Its really amazing...
Reply With Quote
  #6 (permalink)  
Old 08-31-09, 12:20 PM
dgreenhouse's Avatar
dgreenhouse dgreenhouse is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: San Francisco
Posts: 457
Thanks: 0
Thanked 3 Times in 3 Posts
Much better

Quote:
Originally Posted by job0107 View Post
This is a little easier:
PHP Code:

<?php
$array_in 
= array('aaa''bbb','aaa','ddd','aaa','ccc','ccc');
$array_out array_merge(array_unique($array_in));
print_r($array_out);
?>
That's much better and cleaner!

I did not even think of array_merge!

Guess that's what happens when you stay up for too many hours.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove value from array Nikas PHP 1 05-20-09 03:20 AM
Multi-Dimensional Array Help Nikas PHP 17 05-04-09 02:10 AM
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' Dr. Forensics PHP 3 07-15-06 03:54 PM
Serializing a set of arrays dannyallen PHP 2 10-11-04 03:04 AM
linking to iframe not working :( j0d JavaScript 5 01-19-04 08:14 PM


All times are GMT -5. The time now is 07:58 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.