I have an Array containing arrays with 2 values, the first one is the Number of the Author the second is his Affiliation.
我有一个包含两个值的数组,第一个是作者的数量第二个是他的从属关系。
Array (
[0] => Array (
[0] => 2
[1] => Department of General Chemistry
)
[1] => Array (
[0] => 3
[1] => Institute of Silicate Materials
)
[2] => Array (
[0] => 4
[1] => Department of General Chemistry
)
[3] => Array (
[0] => 5
[1] => Department of General Chemistry
)
[4] => Array (
[0] => 6
[1] => Institute of Silicate Materials
)
)
How can I group the Authors if the Affiliation is the same? I need the output to be something like:
如果隶属关系相同,我如何将作者分组?我需要输出如下:
3,6 Institute of Silicate Materials
2,4,5 Department of General Chemistry
4 个解决方案
#1
24
foreach ($array as $key => $value) {
$return[$value[1]][] = $value[0];
}
foreach ($return as $key => $value) {
echo implode(',', $value)." ".$key;
}
#2
3
<?php
//the array
$data = array(array(2,'Department of General Chemistry'),array(3,'Institute of Silicate Materials'),array(4,'Department of General Chemistry'),array(5,'Department of General Chemistry'),array(6,'Institute of Silicate Materials'));
//a new array to store the data
$newData = array();
//loop over each value in the data
foreach($data as $d){
//check if a key exists under the new data for the common value (affiliation)
if(!isset($newData[$d[1]])){
//doesn't exist, group under the common value (affiliation)
$newData[$d[1]] = array(array(),$d[1]);
}
//add the author under it's affiliation
$newData[$d[1]][0][] = $d[0];
}
//get the values from the new data, this resets the keys
$newData = array_values($newData);
//display the data
echo '<pre>'.print_r($newData,1).'</pre>';
例子
results in:
结果:
Array
(
[0] => Array
(
[0] => Array
(
[0] => 2
[1] => 4
[2] => 5
)
[1] => Department of General Chemistry
)
[1] => Array
(
[0] => Array
(
[0] => 3
[1] => 6
)
[1] => Institute of Silicate Materials
)
)
#3
2
Try this code , it should solve your problem:
试试这个代码,它应该能解决你的问题:
<?php
$element = array (
array (
2
,'Department of General Chemistry'
) ,
array (
3,
'Institute of Silicate Materials'
),
array (
4
, 'Department of General Chemistry'
) ,
array (
5
, 'Department of General Chemistry'
) ,
array (
6
, 'Institute of Silicate Materials'
)
);
$res = array();
for($i=0; $i< count($element); $i++){
$res[$element[$i][1]][] = $element[$i][0];
}
echo '<pre>';
var_dump($res);
echo '</pre>';
foreach ($res as $key => $value){
echo $key .' : '. implode(',',$value). '<br>';
}
?>
#4
0
You can do it like this:
你可以这样做:
$ar = array (
array(
2,
'Department of General Chemistry'
),
array(
3,
'Institute of Silicate Materials'
),
array(
4,
'Department of General Chemistry'
),
array(
5,
'Department of General Chemistry'
),
array(
6,
'Institute of Silicate Materials'
)
);
$result = array();
foreach ($ar as $key => $value) {
if (array_key_exists($value[1], $result)) {
$result[$value[1]] .= ',' . $value[0];
} else {
$result[$value[1]] = $value[0];
}
}
foreach ($result as $key => $value) {
echo $value . ' ' . $key . '<br>';
}
#1
24
foreach ($array as $key => $value) {
$return[$value[1]][] = $value[0];
}
foreach ($return as $key => $value) {
echo implode(',', $value)." ".$key;
}
#2
3
<?php
//the array
$data = array(array(2,'Department of General Chemistry'),array(3,'Institute of Silicate Materials'),array(4,'Department of General Chemistry'),array(5,'Department of General Chemistry'),array(6,'Institute of Silicate Materials'));
//a new array to store the data
$newData = array();
//loop over each value in the data
foreach($data as $d){
//check if a key exists under the new data for the common value (affiliation)
if(!isset($newData[$d[1]])){
//doesn't exist, group under the common value (affiliation)
$newData[$d[1]] = array(array(),$d[1]);
}
//add the author under it's affiliation
$newData[$d[1]][0][] = $d[0];
}
//get the values from the new data, this resets the keys
$newData = array_values($newData);
//display the data
echo '<pre>'.print_r($newData,1).'</pre>';
例子
results in:
结果:
Array
(
[0] => Array
(
[0] => Array
(
[0] => 2
[1] => 4
[2] => 5
)
[1] => Department of General Chemistry
)
[1] => Array
(
[0] => Array
(
[0] => 3
[1] => 6
)
[1] => Institute of Silicate Materials
)
)
#3
2
Try this code , it should solve your problem:
试试这个代码,它应该能解决你的问题:
<?php
$element = array (
array (
2
,'Department of General Chemistry'
) ,
array (
3,
'Institute of Silicate Materials'
),
array (
4
, 'Department of General Chemistry'
) ,
array (
5
, 'Department of General Chemistry'
) ,
array (
6
, 'Institute of Silicate Materials'
)
);
$res = array();
for($i=0; $i< count($element); $i++){
$res[$element[$i][1]][] = $element[$i][0];
}
echo '<pre>';
var_dump($res);
echo '</pre>';
foreach ($res as $key => $value){
echo $key .' : '. implode(',',$value). '<br>';
}
?>
#4
0
You can do it like this:
你可以这样做:
$ar = array (
array(
2,
'Department of General Chemistry'
),
array(
3,
'Institute of Silicate Materials'
),
array(
4,
'Department of General Chemistry'
),
array(
5,
'Department of General Chemistry'
),
array(
6,
'Institute of Silicate Materials'
)
);
$result = array();
foreach ($ar as $key => $value) {
if (array_key_exists($value[1], $result)) {
$result[$value[1]] .= ',' . $value[0];
} else {
$result[$value[1]] = $value[0];
}
}
foreach ($result as $key => $value) {
echo $value . ' ' . $key . '<br>';
}