0

I have set of data that I need help with. I need to make a difficult calculation and array sort through several multidimensional arrays and the logic and syntax is giving me a headache.

Basically what I need it:

  • an array of keywords
  • I need to query a database and store some results for each keyword. (ID and Count of keyword matches)
  • I need to then flip the array around so instead of keywords being the parent element, it's IDs with the count of matches for each keyword within
  • I need to perform a calculation to get a single number from these number of matches
  • I need to sort the array so I can see which ID yielded the highest match value in the database, so I can query and output the relevant data.

I know how I am going to do the calculation for the relevancy now, it's just I've only briefly worked with single dimension arrays and I don't know how to syntactically represent what I need with this example. Could anyone give me a hand?

Here is a sample of what I want - not sure if this is syntactically the best way to show you, but hopefully you'll get the picture:

Array
    (
        [Keywords] => Array
        (
            ["wax"] => Array
            (
                [ID] => 1
                [Match] => 8

                [ID] => 2
                [Match] => 10
            )
            ["hard"] => Array
            (
                [ID] => 1
                [Match] => 2

                [ID] => 2
                [Match] => 37
            )
        )

Then this array would need to be translated to:

Array
    (
        [ID] => Array
        (
            ["1"] => Array
            (
                [Keyword] => "wax"
                [Match] => 8

                [Keyword] => "hard"
                [Match] => 10
            )
            ["2"] => Array
            (
                [Keyword] => "wax"
                [Match] => 2

                [Keyword] => "hard"
                [Match] => 37
            )
        )    
2
  • A sample of this multidimensional array? Commented Mar 2, 2011 at 11:10
  • try changing MySQL query to bring out array closer to what you are expecting. this is the first thing i would have done before diving in php to solve complex array manipulation. Commented Mar 2, 2011 at 11:45

2 Answers 2

1

This code:

$arr =  array
        (
            "wax" => array
            (
                array(
                    'ID' => 11,
                    'Match' => 8
                ),
                array(
                    'ID' => 21,
                    'Match' => 10
                )
            ),
            "hard" => array
            (
                array(
                    'ID' => 11,
                    'Match' => 2
                ),
                array(
                    'ID' => 21,
                    'Match' => 37
                )
            )
        );

$byid = array();
foreach($arr as $kw => $res) {
    foreach($res as $r) {
        $byid[$r['ID']][] = array('Keyword' => $kw, 'Match' => $r['Match']);
    }
}

var_export($byid);

gives:

array (
    11 => 
        array (
            0 => 
                array (
                    'Keyword' => 'wax',
                    'Match' => 8,
                ),
            1 => 
                array (
                    'Keyword' => 'hard',
                    'Match' => 2,
                ),
        ),
    21 => 
        array (
            0 => 
                array (
                    'Keyword' => 'wax',
                    'Match' => 10,
                ),
            1 => 
                array (
                    'Keyword' => 'hard',
                    'Match' => 37,
                ),
        ),
)

I hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

0

You can query the db and create both the arrays by,

$query = "select id, keyword, count(*) as keyword_count from [your table] group by keyword";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
  $keyword = $row['keyword'];
  $id = $row['id'];
  $count = $row['keyword_count'];
  $first_array["keywords"]["$keyword"] = array('id' => $id, 'match' => $count);
  $second_array["id"]["$id"] = array('keyword' => $keyword, 'match' => $count);
}
print_r($first_array);
print_r($second_array);

1 Comment

the keywords are entered by the user via a HTML form and then those keywords are split by space delimiter and the query looks at fields Title and Content from the database to find matches to the keywords. How can I amend the mysql query to reflect this, I'm not great at anything more than simple SELECT, INSERT and UPDATES in mySQL, so any help would be appreciated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.