Jump to content

[SOLVED] Echo section of array where key = value


andrewgarn

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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/>";
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.