PHP将多个对象添加到数组中

时间:2016-06-08 19:49:51

标签: php

我有以下php:

 $refined_results = array();

    $refined_results['First']    = $results[0];         
    $refined_results['Second']   = $results[1]; 
    $refined_results['Third']    = $results[2]; 
    $refined_results['Fourth']   = $results[3]; 
    $refined_results['Fifth']    = $results[4]; 
    $refined_results['Sixth']    = $results[5]; 

有没有办法简化这个?

例如(当然完全错误):

$refined_results['First','Second','Third']    = $results[0,1,2]; //Yup...

2 个答案:

答案 0 :(得分:3)

您可以使用array_combine功能:

$keys = ['first', 'second', 'third', 'fourth', 'fifth'];
$refined_results = array_combine($keys, $results);

答案 1 :(得分:1)

package javaapplication63;

import java.awt.*;
import java.util.*;

import javax.swing.*;
import javax.swing.table.*;
import javax.swing.Timer;

public class NewClass extends JFrame
{
ColorRenderer colorRenderer;

public NewClass()
{
String[] columnNames = {"Date", "String", "Integer", "Boolean"};
Object[][] data =
{
{new Date(), "A", new Integer(1), new Boolean(true)},
{new Date(), "B", new Integer(2), new Boolean(false)},
{new Date(), "C", new Integer(3), new Boolean(true)},
{new Date(), "D", new Integer(4), new Boolean(false)}
};

DefaultTableModel model = new DefaultTableModel(data, columnNames);
JTable table = new JTable( model )
{
public Class getColumnClass(int column)
{
return getValueAt(0, column).getClass();
}

// By overriding this method we can use a single renderer for every column

public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
{
Component c = super.prepareRenderer(renderer, row, column);
colorRenderer.setBackground(c, row, column);
return c;
}


};

table.setPreferredScrollableViewportSize(new Dimension(300,400));
JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );

// Create blinking color renderer

colorRenderer = new ColorRenderer( table );

colorRenderer.setRowColor(1, Color.GREEN);

colorRenderer.startBlinking(1000);
}

public static void main(String[] args)
{
NewClass frame = new NewClass();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible(true);
}

/*
** Color cell background
*/
class ColorRenderer 
{
private JTable table;
private AbstractTableModel model;
private Map colors;
private boolean isBlinking = true;
private Timer timer;
private Point location;

public ColorRenderer(JTable table)
{
this.table = table;
model = (AbstractTableModel)table.getModel();
colors = new HashMap();
location = new Point();
}

public void setBackground(Component c, int row, int column)
{
// Don't override the background color of a selected cell

//if ( table.isCellSelected(row, column) ) return;

// The default render does not reset the background color
// that was set for the previous cell, so reset it here

if (c instanceof DefaultTableCellRenderer)
{
c.setBackground( table.getBackground() );
}

// Don't highlight this time

if ( !isBlinking ) return;

// In case columns have been reordered, convert the column number

column = table.convertColumnIndexToModel(column);

// Get cell color

Object key = getKey(row, column);
Object o = colors.get( key );

if (o != null)
{
c.setBackground( (Color)o );
return;
}

// Get row color

key = getKey(row, -1);
o = colors.get( key );

if (o != null)
{
c.setBackground( (Color)o );
return;
}

// Get column color

key = getKey(-1, column);
o = colors.get( key );

if (o != null)
{
c.setBackground( (Color)o );
return;
}

}

public void setCellColor(int row, int column, Color color)
{
Point key = new Point(row, column);
colors.put(key, color);
}



public void setRowColor(int row, Color color)
{
setCellColor(row, -1, color);
}

private Object getKey(int row, int column)
{
location.x = row;
location.y = column;
return location;
}

public void startBlinking(int interval)
{

if (model.getValueAt(0,1).equals("A")){//row 0 = first row/ column 2 = column 1
System.out.println("hello");
timer = new Timer(); //here is where i cant get the timer to start based on the interval
TimerTask tasknew = new TimerTask(interval) {
    @Override
    public void run() {

    }
};
timer.schedule(tasknew, 2000, 2000);
}
//timer = new Timer(interval,this);
//timer.start();
}

public void stopBlinking()
{
timer.stop();
}


}
}