Note that there are some explanatory texts on larger screens.

plurals
  1. POSum the entries of items with the same index ID in a multi-dimensional array?
    primarykey
    data
    text
    <p>I have a multidimensional array that contains a variable number of sub arrays.<br> Each sub-array contains a list of numeric keys and values.<br> I have a separate array that is the result of an "array_intersect_key" function against the multidimensional array that contains only the keys that exist in each sub-array.</p> <p>I want to walk through the intersect_key array and for each item in it sum the values associated with the matching keys within the multidimensional array and then take the total and use that to replace the value associated with the key in the intersect_key array while maintaining the same index id.</p> <p>The key in each array relates to the id of an article in the database, the value associated with the key is how many times a certain word appears in that article. I'm trying to add together all the word counts relating to each article so I can then order them by relevance.</p> <p>The code that creates the arrays:</p> <pre><code>$arr_articles = array(); foreach($arr_words as $wordid =&gt; $word) { $query = "SELECT articleid,wordcount FROM index_wordsearch AS iws WHERE iws.keywordid = '$wordid'"; $articlesearch = mysql_query($query); if (!$articlesearch) { die('Unable to search for articles matching the specified words: '.mysql_error()); } else { $arr_ids = array(); while ($row = mysql_fetch_assoc($articlesearch)) { $articleid = $row['articleid']; $wordcount = $row['wordcount']; $arr_ids["$articleid"] = "$wordcount"; } $arr_aticles[] = $arr_ids; } } $arr_matches = call_user_func_array('array_intersect_key',$arr_articles); </code></pre> <p>I've started trying to tackle this by using the <code>call_user_func_array()</code> call again to branch out to a custom function but this approach doesn't feel right.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload