andrewgarn Posted October 16, 2008 Share Posted October 16, 2008 This is probably simple, but i'm not making it work. I have a n array which contains a set of values, each of these values are one of the possible values in another array What i want to do, is for each of the values in the first array, echo the coresponding values in the second. I have this: <?php $result = array_unique($keynb); foreach($result as $value) { $check = array_search($value, $keynb); print_r($check); } This only prints one of the keys of each value. Quote Link to comment https://forums.phpfreaks.com/topic/128730-solved-echo-section-of-array-where-key-value/ Share on other sites More sharing options...
.josh Posted October 16, 2008 Share Posted October 16, 2008 Perhaps you should give an example of the two arrays, but perhaps array_intersect might be of use? Quote Link to comment https://forums.phpfreaks.com/topic/128730-solved-echo-section-of-array-where-key-value/#findComment-667189 Share on other sites More sharing options...
andrewgarn Posted October 16, 2008 Author Share Posted October 16, 2008 and I forgot the $keynb array is sorted by value using: asort($keynb); print_r($keynb); [syktt] => 1 [sewsgc] => 1 [lbdxn] => 1 [ttjklz] => 1 [lrvht] => 1 [tmdec] => 1 [internal] => 2 [structures] => 2 [demands] => 2 [coherent] => 2 [content] => 2 [imposition] => 2 [fourth] => 2 [second] => 2 [third] => 2 [fifth] => 2 [distinguished] => 2 [reason] => 2 [treated] => 2 [unrelated] => 2 [courses] => 2 [different] => 2 [memorised] => 2 [intrinsic] => 2 [deepsurface] => 2 [slide] => 2 [distinguishes] => 2 [conversational] => 2 [nthpr] => 2 [experience] => 2 [signs] => 2 [associated] => 2 [concepts] => 2 [argument] => 2 [unreflectively] => 2 [evidence] => 2 [everyday] => 2 [theoretical] => 2 [master] => 3 [however] => 4 [succeed] => 4 [present] => 4 [provides] => 4 [challenge] => 4 [motivation] => 4 print_r($result); [click] => 1 [internal] => 2 [master] => 3 [however] => 4 [important] => 5 [styles] => 6 [questions] => 7 [skill] => 8 [cognitive] => 9 [groups] => 10 [skills] => 12 [ideas] => 14 [teacher] => 16 [model] => 17 [group] => 20 [within] => 22 [which] => 24 [models] => 25 [behavior] => 28 [based] => 29 [their] => 40 [knowledge] => 42 [learners] => 44 [learning] => 67 For $result, all i want to use is the values, as i am trying to find all the words with the same number of occurrences. I can turn $result into a string by doing: foreach($result as $value) { $stnb = $stnb.' '.$value; } echo $stnb; Gives: 1 2 3 4 5 6 7 8 9 10 12 14 16 17 20 22 24 25 28 29 40 42 44 67 Quote Link to comment https://forums.phpfreaks.com/topic/128730-solved-echo-section-of-array-where-key-value/#findComment-667201 Share on other sites More sharing options...
.josh Posted October 16, 2008 Share Posted October 16, 2008 So...basically you want to somehow link for instance click: syktt sewsgc lbdxn ttjklz lrvht tmdec ? Quote Link to comment https://forums.phpfreaks.com/topic/128730-solved-echo-section-of-array-where-key-value/#findComment-667208 Share on other sites More sharing options...
andrewgarn Posted October 16, 2008 Author Share Posted October 16, 2008 Each word in $keynb has a value associated with it. (This value is the number of times a word has appeared in a document) These values are stored in $result. All I want to do is echo the words that correspond to each value so: Number 1 //all words with value of 1 Number 2 //all words with value of 2 Quote Link to comment https://forums.phpfreaks.com/topic/128730-solved-echo-section-of-array-where-key-value/#findComment-667213 Share on other sites More sharing options...
andrewgarn Posted October 16, 2008 Author Share Posted October 16, 2008 No luck so far. Quote Link to comment https://forums.phpfreaks.com/topic/128730-solved-echo-section-of-array-where-key-value/#findComment-667361 Share on other sites More sharing options...
.josh Posted October 16, 2008 Share Posted October 16, 2008 Sorry bro I really don't understand what you're trying to accomplish. You say that your first array has words as keys and the values are how many times they appear in some document, right? Easy enough to understand. But then you say something about how the values are also somehow stored in your second array. But then you turn around and say that you just want to echo the actual words? So...I really don't understand what you are trying to accomplish here. -What is the difference between these two arrays? -Are you trying to make some kind of comparison between the two? -If all you are trying to do is echo the actual words, use a loop and simply echo them example: foreach($array as $key => $val) { echo "$key <br/>"; } Quote Link to comment https://forums.phpfreaks.com/topic/128730-solved-echo-section-of-array-where-key-value/#findComment-667411 Share on other sites More sharing options...
andrewgarn Posted October 16, 2008 Author Share Posted October 16, 2008 Ignore the second array, I can make that a string, so ignore it. What I want to make is a NEW array with the key being NUMBERS and the values being the words that appeared for that NUMBER of times. So this: [syktt] => 1 [sewsgc] => 1 [lbdxn] => 1 [ttjklz] => 1 would become: [1] => syktt, sewsgc, lbdxn, ttjklz etc Hope thats more clear. Quote Link to comment https://forums.phpfreaks.com/topic/128730-solved-echo-section-of-array-where-key-value/#findComment-667421 Share on other sites More sharing options...
.josh Posted October 16, 2008 Share Posted October 16, 2008 foreach($keynb as $key => $val) { $newlist[$val] .= $key . " "; } Quote Link to comment https://forums.phpfreaks.com/topic/128730-solved-echo-section-of-array-where-key-value/#findComment-667464 Share on other sites More sharing options...
andrewgarn Posted October 16, 2008 Author Share Posted October 16, 2008 Thanks, that works Now to find words that appeared each number of times, i just need to use the numbers from the 2nd array, and echo the new array with that as the key Thanks Quote Link to comment https://forums.phpfreaks.com/topic/128730-solved-echo-section-of-array-where-key-value/#findComment-667486 Share on other sites More sharing options...
Barand Posted October 16, 2008 Share Posted October 16, 2008 do tou mean <?php $ar = array ( 'syktt' => 1 , 'sewsgc' => 1 , 'lbdxn' => 1 , 'ttjklz' => 1 , 'lrvht' => 1 , 'tmdec' => 1 , 'internal' => 2 , 'structures' => 2 , 'demands' => 2 , 'coherent' => 2 , 'content' => 2 , 'imposition' => 2 , 'fourth' => 2 , 'second' => 2 , 'third' => 2 , 'fifth' => 2 , 'distinguished' => 2 , 'reason' => 2 , 'treated' => 2 , 'unrelated' => 2 , 'courses' => 2 , 'different' => 2 , 'memorised' => 2 , 'intrinsic' => 2 , 'deepsurface' => 2 , 'slide' => 2 , 'distinguishes' => 2 , 'conversational' => 2 , 'nthpr' => 2 , 'experience' => 2 , 'signs' => 2 , 'associated' => 2 , 'concepts' => 2 , 'argument' => 2 , 'unreflectively' => 2 , 'evidence' => 2 , 'everyday' => 2 , 'theoretical' => 2 , 'master' => 3 , 'however' => 4 , 'succeed' => 4 , 'present' => 4 , 'provides' => 4 , 'challenge' => 4 , 'motivation' => 4 ); $result = array(); foreach ($ar as $word => $count) { $result[$count][] = $word; } foreach ($result as $count => $words) { sort($words); echo $count, ' : ', join(', ', $words), '<br/>'; } ?> --> 1 : lbdxn, lrvht, sewsgc, syktt, tmdec, ttjklz 2 : argument, associated, coherent, concepts, content, conversational, courses, deepsurface, demands, different, distinguished, distinguishes, everyday, evidence, experience, fifth, fourth, imposition, internal, intrinsic, memorised, nthpr, reason, second, signs, slide, structures, theoretical, third, treated, unreflectively, unrelated 3 : master 4 : challenge, however, motivation, present, provides, succeed Quote Link to comment https://forums.phpfreaks.com/topic/128730-solved-echo-section-of-array-where-key-value/#findComment-667498 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.