我有一组需要帮助的数据。我需要通过几个多维数组进行困难的计算和数组排序,逻辑和语法让我头疼。
基本上我需要它:
我知道我现在要如何计算相关性,我只是简单地使用了单维数组,而且我不知道如何在语法上用这个例子来表示我需要的东西。谁能帮我一把?
以下是我想要的一个示例 - 不确定这在语法上是否是向您展示的最佳方式,但希望您能了解到这一点:
Array
(
[Keywords] => Array
(
["wax"] => Array
(
[ID] => 1
[Match] => 8
[ID] => 2
[Match] => 10
)
["hard"] => Array
(
[ID] => 1
[Match] => 2
[ID] => 2
[Match] => 37
)
)
然后这个数组需要翻译成:
Array
(
[ID] => Array
(
["1"] => Array
(
[Keyword] => "wax"
[Match] => 8
[Keyword] => "hard"
[Match] => 10
)
["2"] => Array
(
[Keyword] => "wax"
[Match] => 2
[Keyword] => "hard"
[Match] => 37
)
)
答案 0 :(得分:1)
此代码:
$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);
给出:
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,
),
),
)
我希望它有所帮助。
答案 1 :(得分:0)
您可以通过
查询数据库并创建两个数组$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);