Yesideez Posted March 18, 2007 Share Posted March 18, 2007 If I have an array looking like this: <?php $long=array("len" => 0,"word" => ""); ?> How can I add a word onto the end of that list? I need to add the word itself and its length. Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted March 18, 2007 Share Posted March 18, 2007 you mean: <?php $long["add"] = "3"; ?> Ted Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted March 18, 2007 Share Posted March 18, 2007 If you want to add the word and its length then you do this: $long['len'] = 5; $long['word'] = 'myword'; Quote Link to comment Share on other sites More sharing options...
Barand Posted March 18, 2007 Share Posted March 18, 2007 <?php include 'db2.php'; $words = array ('apple', 'banjo', 'ceiling', 'duck', 'electric'); $long = array(); // add words and lengths to long array foreach ($words as $w) { $long[] = array( 'len' => strlen($w), 'word' => $w ); } // view the array echo '<pre>', print_r($long, true), '</pre>'; ?> Quote Link to comment Share on other sites More sharing options...
Yesideez Posted March 18, 2007 Author Share Posted March 18, 2007 This is a small sample of the output I received, how can I sort this list by the length of the word? Array ( [0] => Array ( [len] => 1 [word] => A ) [1] => Array ( [len] => 3 [word] => DAM ) [2] => Array ( [len] => 3 [word] => ARK ) [3] => Array ( [len] => 4 [word] => ZERO ) [4] => Array ( [len] => 3 [word] => NOR ) [5] => Array ( [len] => 3 [word] => OR ) Quote Link to comment Share on other sites More sharing options...
Yesideez Posted March 18, 2007 Author Share Posted March 18, 2007 Fixed by adding this: <?php asort($long); ?> Thanks to all those who have helped - gratefully apprecated Quote Link to comment Share on other sites More sharing options...
Barand Posted March 18, 2007 Share Posted March 18, 2007 Word = "OR" Length = 3 ??? Quote Link to comment Share on other sites More sharing options...
Yesideez Posted March 18, 2007 Author Share Posted March 18, 2007 OK I'm really confused here now. Just reverse sorted the list using this: <?php arsort($long); ?> This is what I'm using to get the top 10 words in the list: [code]<?php $topten=""; for ($i=0;$i<10;$i++) { $topten.=$long[$i]['word'].'<br />'; } ?> This is what I'm getting returned: A DAM ARK ZERO NOR OR RE ORZO ERR ON I'm confused that although the list shown using print_r has been sorted by the length of the word I can't figure out how to access the sorted data [/code] Quote Link to comment Share on other sites More sharing options...
Barand Posted March 18, 2007 Share Posted March 18, 2007 Just use rsort(). arsort() retains the original array indexes. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted March 18, 2007 Author Share Posted March 18, 2007 Many thanks Quote Link to comment Share on other sites More sharing options...
Yesideez Posted March 18, 2007 Author Share Posted March 18, 2007 Word = "OR" Length = 3 ??? Just added my output to show an asterisx either side of the word and found some had a trailing space so I'm going to add trim() so finish it off Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.