LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-21-2006, 10:45 AM   #1
rblampain
Senior Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 9.2
Posts: 1,066

Rep: Reputation: 48
what php function


I have little knowledge of PHP.
I have files of plain text for which I need to create an index. This is an alphabetical sorted index of single words used in the text.

I've had the content of the file put into an array, then sorted the array with "natcasesort()" then used "array_unique()" to obtain the result I wanted, all that worked but I don't know how to get a list of single words out of the resulting array from "array_unique()".

Print_r($result) shows a correct list of the words with the keys but trying to access the array elements by number gives a strange result.

What should I do?

Thank you for your help.
 
Old 01-21-2006, 12:10 PM   #2
Boby
Member
 
Registered: Feb 2004
Location: Sighisoara/Cluj-Napoca (Romania)
Distribution: CentOS 4, Fedora Core 6
Posts: 781

Rep: Reputation: 32
Let's say you have this:
PHP Code:
/* This is your array */
$result = array("green""red""green""blue""white""red""yellow""magneta""red""black""orange""test" => "white"); 
Output 1:
PHP Code:
echo "<pre>";
   
print_r($result);
   echo 
"</pre>"
Code:
Array
(
    [0] => green
    [1] => red
    [2] => green
    [3] => blue
    [4] => white
    [5] => red
    [6] => yellow
    [7] => magneta
    [8] => red
    [9] => black
    [10] => orange
    [test] => white
)
Now you sort and remove duplicates of your array:
PHP Code:
/* Sort array */
natcasesort($result);
/* remove duplicate values */
$input array_unique($result); 
Output 2:
PHP Code:
echo "<pre>";
   
print_r($result);
   echo 
"</pre>"
Code:
Array
(
    [9] => black
    [3] => blue
    [0] => green
    [7] => magneta
    [10] => orange
    [8] => red
    [test] => white
    [6] => yellow
)
Access array values by array key.
Output 3:
PHP Code:
   echo $result[6];
   echo 
"<br />";
   echo 
$result[10];
   echo 
"<br />";
   echo 
$result['test']; 
Code:
yellow
orange
white
You access array values by using $array[key], if the key is a string $array['key']

Hope you understand now.
Good luck!

If you still have problems or you don't understand something, please post the errors.

Boby
 
Old 01-22-2006, 12:52 AM   #3
rblampain
Senior Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 9.2
Posts: 1,066

Original Poster
Rep: Reputation: 48
Thank you very much for your very detailed explanation.
 
Old 01-22-2006, 09:19 AM   #4
Boby
Member
 
Registered: Feb 2004
Location: Sighisoara/Cluj-Napoca (Romania)
Distribution: CentOS 4, Fedora Core 6
Posts: 781

Rep: Reputation: 32
Your welcome!

..and if you want to use multidimensional arrays...
PHP Code:
$result = array("green""red""green"=> array(
                                                      
"test" => "orange",
                                                      
=> "black",
                                                      
"blah" => "white",
                                                      
"deeper" => array(
                                                                     
"magneta" => "magneta",
                                                                     
=> "random color",
                                                                     
123 => array(
                                                                                            
"key" => "black and white"
                                                                                 
)
                                                                  )
                                                      ),
                                          
"another key" => "another color"
   
);

echo 
"<pre>"print_r($result); echo "</pre>";
echo 
"<hr />Let's print some values:<br /><br />";
echo 
$result[2]['deeper'][123]['key']; 
Boby
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP / VideoLAN / Fedora Core Question - how can I get the PHP "exec" function to work gtrawoger Linux - Software 3 12-21-2005 07:51 AM
PHP Variable = function? wh33t Programming 2 01-22-2005 07:28 AM
php HTTP_RAW_POST_DATA no value in function dinges Programming 1 12-27-2004 12:59 PM
php HTTP_RAW_POST_DATA na value in function dinges Linux - Software 1 12-26-2004 05:11 AM
Passing one php function result as a parameter to another php function davee Programming 13 09-12-2004 01:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration