0

我正在尝试将值推送到这样的数组中:

$scoreValues[$i][] = $percent ;
$scoreValues[$i][] = '<span id="item'.$i.'" class="suggestElement" data-entityid="'.$row['id'].'" data-match="'.$percent.'">'.rawurldecode($row['name']).'</span>' ;

我基本上想将 $percent 与字符串链接在一起,所以我得到如下输出:

array (0 > array('46.5', '<span etc etc')

然后我计划按百分比子数组排序,这样我的得分最高的字符串就在顶部。

4

3 回答 3

1

最简单的方法是使用两个数组:

$percents[$i] = $percent;
$scores[$i] = "<span....>");

或一个数组,但索引如下

$data = new arrray('percents' => array(), 'scores' => array());
$data['percents'][$i] = $percent;
$data['scores'][$i] = "<span....>");

完成此操作后,您可以使用以下命令对数组进行排序array_multisort

array_multisort(
   $data['percents'], SORT_DESC,
   $data['scores']);
于 2012-07-17T08:31:14.657 回答
1

在第二行中,您需要指定第二个数组的索引:

$scoreValues[$i][$j] = '<span id="item'.$i.'" class="suggestElement" data-entityid="'.$row['id'].'" data-match="'.$percent.'">'.rawurldecode($row['name']).'</span>' ;

所以你基本上需要 2 个计数器,一个用于外部数组 ($i),一个用于内部数组 ($j)。

编辑:

你让我对这个问题有点困惑,似乎你需要的不是多维数组,而是一个简单的数组:

$scoreValues[$percent] = '<span id="item'.$i.'" class="suggestElement" data-entityid="'.$row['id'].'" data-match="'.$percent.'">'.rawurldecode($row['name']).'</span>' ;

请注意,这需要$percent是唯一的。

于 2012-07-17T08:30:16.290 回答
0

尝试这个:

$val = array(
    'percent' => $percent,
    'html' => '<span id="item' . $i .
              '" class="suggestElement" data-entityid="'.$row['id'].
              '" data-match="'.$percent.'">'.rawurldecode($row['name']).
              '</span>'
);
// This just pushes it onto the end of the array
$scoreValues[] = $val ;
// Or you can insert it at an explicit location
//$scoreValues[$i] = $val;
于 2012-07-17T08:37:40.147 回答