i have wrote a script to produce an array of data but now want to display in order of score. The array outputs as follows;
我已经编写了一个脚本来生成数据数组,但是现在想按分数的顺序显示。阵列输出如下所示;
[display_name] => Array
(
[0] => ACT_Web_Designs
[1] => user1_design
[2] => user2_design
)
[proffesion] => Array
(
[0] => Web Developer
[1] => web developer
[2] => Web Developer
)
[score] => Array
(
[0] => 15
[1] => 6
[2] => 15
)
[img] => Array
(
[0] => ./?0000=gif&0001=3fadb8c362ff39f3322909899ff14760&0002=prof_pic
[1] =>
[2] =>
)
so in a nutshell I am wanting it to be converted as follows;
简而言之,我希望它能被转换成如下形式;
[display_name] => Array
(
[0] => ACT_Web_Designs
[1] => user2_design
[2] => user1_design
)
[proffesion] => Array
(
[0] => Web Developer
[1] => web developer
[2] => Web Developer
)
[score] => Array
(
[0] => 15
[1] => 15
[2] => 6
)
[img] => Array
(
[0] => ./?0000=gif&0001=3fadb8c362ff39f3322909899ff14760&0002=prof_pic
[1] =>
[2] =>
)
I have been looking at asort() but cant get anything to work. any help would be much appreciated.
我一直在找工作,但找不到工作。如有任何帮助,我们将不胜感激。
7 个解决方案
#1
6
This is exactly where the PHP
's array_multisort comes to use. It is a case where you want to sort many arrays based on the comparison happening in just one of them.
这正是PHP的array_multisort使用的地方。在这种情况下,您希望基于在其中一个数组中发生的比较对多个数组进行排序。
I've modified the array score
to have distinct values.
我修改了数组的分数,使其具有不同的值。
<?php
$arr = array(
'display_name' => array('ACT_Web_Designs','user1_design','user2_design'),
'proffesion' => array('Web Developer','web developer','web developer'),
'score' => array(12,6,15),
'img' => array('./?0000=gif&0001=3fadb8c362ff39f3322909899ff14760&0002=prof_pic','','')
);
var_dump($arr);
array_multisort($arr['score'], SORT_ASC, SORT_NUMERIC,
$arr['display_name'],
$arr['proffesion'],
$arr['img']
);
var_dump($arr);
?>
这是一个工作演示。
#2
1
How about this simpler one
这个简单点的怎么样
$arr = array("k"=>array("A","B","C"),"l"=>array(15,6,15),"n"=>array("k","l","n"));
array_multisort($arr["k"],SORT_NUMERIC,SORT_DESC,$arr["l"],$arr["n"]);
var_dump($arr);
#3
0
Doesn't it work to just rsort
the score array?
对分数数组进行排序不是很有用吗?
rsort($data['score'], SORT_NUMERIC);
#4
0
I have managed to do this, i was just after a more efficient way;
我成功地做到了这一点,我只是想找到一种更有效的方法;
$array = array(
'display_name' => array(0 => 'ACT_Web_Designs', 1 => 'user1_design', 2 => 'user2_design' ),
'proffesion' => array( 0 => 'Web Developer', 1 => 'web developer', 2 => 'Web Developer' ),
'score' => array( 0 => 15, 1 => 6, 2 => 15 ),
'img' => array( 0 => './?0000=gif&0001=3fadb8c362ff39f3322909899ff14760&0002=prof_pic', 1 => '', 2 => '' )
);
arsort($array['score'], SORT_NUMERIC );
foreach($array['score'] as $key => $val ) {
$newarray['display_name'][] = $array['display_name'][$key];
$newarray['proffesion'][] = $array['proffesion'][$key];
$newarray['score'][] = $array['score'][$key];
$newarray['img'][] = $array['img'][$key];
}
print_r($newarray);
returns
返回
Array
(
[display_name] => Array
(
[0] => ACT_Web_Designs
[1] => user2_design
[2] => user1_design
)
[proffesion] => Array
(
[0] => Web Developer
[1] => Web Developer
[2] => web developer
)
[score] => Array
(
[0] => 15
[1] => 15
[2] => 6
)
[img] => Array
(
[0] => ./?0000=gif&0001=3fadb8c362ff39f3322909899ff14760&0002=prof_pic
[1] =>
[2] =>
)
)
#5
0
Use rsort()
使用函数()
<?php
$fruits = array("lemon", "orange", "banana", "apple");
rsort($fruits);
reset($fruits);
while (list($key, $val) = each($fruits)) {
echo "$key = $val\n";
}
?>
This example would display:
这个例子将显示:
0 = orange
1 = lemon
2 = banana
3 = apple
#6
0
The most elegant solution that I could find would not reorder the data structure but merely access it in a different fashion.
我所能找到的最优雅的解决方案不会重新排序数据结构,而是以不同的方式访问它。
$scores = $data['score'];
arsort($scores);
$keys_ordered_by_score = array_keys($scores);
Now you can, say, grab the display_name and "proffesion" that has the highest score by the following:
现在,您可以获取display_name和“proffesion”,它们的得分最高,如下所示:
$first_place = $keys_ordered_by_score[0];
echo $data['display_name'][$first_place],
' is a ', $data['proffesion'][$first_place];
Of course, if you really need to reorder the data structure, this idea is useless to you. Any of the other answers using array_multisort() will likely suit that need.
当然,如果您确实需要重新排序数据结构,这个想法对您来说是无用的。使用array_multisort()的任何其他答案都可能适合这种需要。
#7
0
This will not re-sort them for you, but it will let you go through them in the order you want. You can reassign them if you want, but I'd just use this for the output order.
这不会为你重新排序,但它会让你按照你想要的顺序进行。如果你想,你可以重新分配它们,但是我只是用这个来做输出顺序。
Edit: This will not work, due to the possibility of non-unique key values. See Comments below, and learn from my mistake
编辑:这将不起作用,因为可能有非唯一键值。请看下面的评论,从我的错误中吸取教训
$sort_order = $array['score'];
arsort($sort_order);
$sort_order = array_flip($sort_order);
foreach($sort_order as $key){
echo $array['display_name'][$key].' - '.$array['score'][$key];
}
#1
6
This is exactly where the PHP
's array_multisort comes to use. It is a case where you want to sort many arrays based on the comparison happening in just one of them.
这正是PHP的array_multisort使用的地方。在这种情况下,您希望基于在其中一个数组中发生的比较对多个数组进行排序。
I've modified the array score
to have distinct values.
我修改了数组的分数,使其具有不同的值。
<?php
$arr = array(
'display_name' => array('ACT_Web_Designs','user1_design','user2_design'),
'proffesion' => array('Web Developer','web developer','web developer'),
'score' => array(12,6,15),
'img' => array('./?0000=gif&0001=3fadb8c362ff39f3322909899ff14760&0002=prof_pic','','')
);
var_dump($arr);
array_multisort($arr['score'], SORT_ASC, SORT_NUMERIC,
$arr['display_name'],
$arr['proffesion'],
$arr['img']
);
var_dump($arr);
?>
这是一个工作演示。
#2
1
How about this simpler one
这个简单点的怎么样
$arr = array("k"=>array("A","B","C"),"l"=>array(15,6,15),"n"=>array("k","l","n"));
array_multisort($arr["k"],SORT_NUMERIC,SORT_DESC,$arr["l"],$arr["n"]);
var_dump($arr);
#3
0
Doesn't it work to just rsort
the score array?
对分数数组进行排序不是很有用吗?
rsort($data['score'], SORT_NUMERIC);
#4
0
I have managed to do this, i was just after a more efficient way;
我成功地做到了这一点,我只是想找到一种更有效的方法;
$array = array(
'display_name' => array(0 => 'ACT_Web_Designs', 1 => 'user1_design', 2 => 'user2_design' ),
'proffesion' => array( 0 => 'Web Developer', 1 => 'web developer', 2 => 'Web Developer' ),
'score' => array( 0 => 15, 1 => 6, 2 => 15 ),
'img' => array( 0 => './?0000=gif&0001=3fadb8c362ff39f3322909899ff14760&0002=prof_pic', 1 => '', 2 => '' )
);
arsort($array['score'], SORT_NUMERIC );
foreach($array['score'] as $key => $val ) {
$newarray['display_name'][] = $array['display_name'][$key];
$newarray['proffesion'][] = $array['proffesion'][$key];
$newarray['score'][] = $array['score'][$key];
$newarray['img'][] = $array['img'][$key];
}
print_r($newarray);
returns
返回
Array
(
[display_name] => Array
(
[0] => ACT_Web_Designs
[1] => user2_design
[2] => user1_design
)
[proffesion] => Array
(
[0] => Web Developer
[1] => Web Developer
[2] => web developer
)
[score] => Array
(
[0] => 15
[1] => 15
[2] => 6
)
[img] => Array
(
[0] => ./?0000=gif&0001=3fadb8c362ff39f3322909899ff14760&0002=prof_pic
[1] =>
[2] =>
)
)
#5
0
Use rsort()
使用函数()
<?php
$fruits = array("lemon", "orange", "banana", "apple");
rsort($fruits);
reset($fruits);
while (list($key, $val) = each($fruits)) {
echo "$key = $val\n";
}
?>
This example would display:
这个例子将显示:
0 = orange
1 = lemon
2 = banana
3 = apple
#6
0
The most elegant solution that I could find would not reorder the data structure but merely access it in a different fashion.
我所能找到的最优雅的解决方案不会重新排序数据结构,而是以不同的方式访问它。
$scores = $data['score'];
arsort($scores);
$keys_ordered_by_score = array_keys($scores);
Now you can, say, grab the display_name and "proffesion" that has the highest score by the following:
现在,您可以获取display_name和“proffesion”,它们的得分最高,如下所示:
$first_place = $keys_ordered_by_score[0];
echo $data['display_name'][$first_place],
' is a ', $data['proffesion'][$first_place];
Of course, if you really need to reorder the data structure, this idea is useless to you. Any of the other answers using array_multisort() will likely suit that need.
当然,如果您确实需要重新排序数据结构,这个想法对您来说是无用的。使用array_multisort()的任何其他答案都可能适合这种需要。
#7
0
This will not re-sort them for you, but it will let you go through them in the order you want. You can reassign them if you want, but I'd just use this for the output order.
这不会为你重新排序,但它会让你按照你想要的顺序进行。如果你想,你可以重新分配它们,但是我只是用这个来做输出顺序。
Edit: This will not work, due to the possibility of non-unique key values. See Comments below, and learn from my mistake
编辑:这将不起作用,因为可能有非唯一键值。请看下面的评论,从我的错误中吸取教训
$sort_order = $array['score'];
arsort($sort_order);
$sort_order = array_flip($sort_order);
foreach($sort_order as $key){
echo $array['display_name'][$key].' - '.$array['score'][$key];
}