我有一个数组
$result = array();
foreach ($object as $val) {
if(!isset($result[$val['id']]))
$result[$val['id']] = $val;
else
$result[$val['id']]['month'] = array($result[$val['id']]['month'], $val['month']);
}
$result = array_values($result); // reindex array
我希望当数组中的键重复时,它将被添加到数组中 这是我的代码:
Array (
[0] => Array ( [id] => 5353 [month] => 05)
[1] => Array ( [id] => 5542 [month] => Array ( [0] => Array ( [0] => 06 [1] => 07 ) [1] => 09 ) )
[2] => Array ( [id] => 4676 [month] => 06 )
)
结果:
Array (
[0] => Array ( [id] => 5353 [month] => 05)
[1] => Array ( [id] => 5542 [month] => Array ( [0] => 06 [1] => 07 [2] => 09 ) )
[2] => Array ( [id] => 4676 [month] => 06 )
)
如何将其修复为:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.JOptionPane;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Scanner;
import java.util.Formatter;
import java.io.IOException;
import java.io.File;
import java.io.FileOutputStream;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class Ch5PA1 extends JFrame {
private String[] courseDescription = new String[5];
private JLabel courseLabel;
private JButton closeProgram;
private JTextArea displayArea;
private Font largeFont;
private ImageIcon computer;
private JComboBox<String> coursesCombo;
private JPanel p1, p2;
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
Ch5PA1 frame = new Ch5PA1();
frame.setSize(700, 400);
frame.setTitle("CIT Courses and Descriptions");
frame.setVisible(true);
}
public Ch5PA1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createIcon();
createLabel();
createTextArea();
createCombo();
createButton();
createPanels();
JComponent cp = (JComponent) getContentPane();
cp.setLayout(new BorderLayout(0, 40));
cp.setBorder(new EmptyBorder(20, 20, 20, 20));
cp.add(p1, BorderLayout.WEST);
cp.add(new JScrollPane(displayArea), BorderLayout.CENTER);
cp.add(p2, BorderLayout.CENTER);
}
public void createIcon() {
computer = new ImageIcon("computer.jpg");
}
public void createLabel() {
courseLabel = new JLabel("Select a Course From the List", SwingConstants.CENTER);
courseLabel.setIcon(new ImageIcon("computer.jpg"));
}
public void createTextArea() {
displayArea = new JTextArea(10, 20);
displayArea.setLineWrap(true);
displayArea.setEditable(false);
displayArea.setBorder(new EmptyBorder(20, 20, 20, 20));
}
public void createCombo() {
coursesCombo = new JComboBox<String>();
coursesCombo.addItem("CIT 110");
coursesCombo.addItem("CIT 111");
coursesCombo.addItem("CIT 149");
coursesCombo.addItem("CIT 170");
coursesCombo.addItem("CIT 249");
coursesCombo.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
setDisplay(coursesCombo.getSelectedIndex());
}
});
setDisplay(0);
}
public void createButton() {
closeProgram = new JButton("Close Program");
closeProgram.setFont(largeFont);
closeProgram.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
public void createPanels() {
p1 = new JPanel();
p1.setLayout(new BorderLayout());
p1.add(coursesCombo, BorderLayout.NORTH);
p1.add(closeProgram, BorderLayout.SOUTH);
p1.setBorder(new EmptyBorder(20, 40, 20, 40));
p2 = new JPanel();
p2.setLayout(new GridLayout(2, 1, 20, 40));
p2.add(courseLabel);
p2.add(displayArea);
add(p2, BorderLayout.CENTER);
p2.setBorder(new EmptyBorder(20, 40, 20, 40));
}
public void createDescriptions() {
for (int i = 0; i < courseDescription.length; i++) {
courseDescription[i] = getDescription(i);
}
}
public String getDescription(int i) {
StringBuilder result = new StringBuilder();
try {
Scanner input = new Scanner(new File("text/description" + i + ".txt"));
while (input.hasNext()) {
result.append(input.nextLine() + '\n');
}
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "Error loading file! Check name and location of file.");
}
return result.toString();
}
public void setDisplay(int index) {
displayArea.setText(getDescription(index));
}
}
答案 0 :(得分:1)
$result = array();
foreach ($object as $val) {
if( ! isset($result[$val['id']])) {
$result[$val['id']] = $val;
} else if ( ! is_array($result[$val['id']]['month'])){
$result[$val['id']]['month'] = array($result[$val['id']]['month'], $val['month']);
} else {
$result[$val['id']]['month'][] = $val['month'];
}
}
$result = array_values($result);
但是,如何获得如下所示的stdClass对象
stdClass Object (
[0] => Array ( [id] => 5353 [month] => 05 )
[1] => Array ( [id] => 5542 [month] => 06 )
[2] => Array ( [id] => 4676 [month] => 06 )
[3] => Array ( [id] => 5542 [month] => 07 )
[4] => Array ( [id] => 5542 [month] => 09 )
)