我有一个数组格式的数据,如下所示:
Array
(
[0] => Array
(
[@attributes] => Array
(
[Domain] => domain1.com
[Available] => true
[ErrorNo] => 0
[Description] =>
[IsPremiumName] => false
[PremiumRegistrationPrice] => 0
[PremiumRenewalPrice] => 0
[PremiumRestorePrice] => 0
[PremiumTransferPrice] => 0
[IcannFee] => 0
[EapFee] => 0
)
)
[1] => Array
(
[@attributes] => Array
(
[Domain] => domain2.com
[Available] => true
[ErrorNo] => 0
[Description] =>
[IsPremiumName] => false
[PremiumRegistrationPrice] => 0
[PremiumRenewalPrice] => 0
[PremiumRestorePrice] => 0
[PremiumTransferPrice] => 0
[IcannFee] => 0
[EapFee] => 0
)
)
[2] => Array
(
[@attributes] => Array
(
[Domain] => domain3.com
[Available] => true
[ErrorNo] => 0
[Description] =>
[IsPremiumName] => false
[PremiumRegistrationPrice] => 0
[PremiumRenewalPrice] => 0
[PremiumRestorePrice] => 0
[PremiumTransferPrice] => 0
[IcannFee] => 0
[EapFee] => 0
)
)
)
我想在Laravel的刀片视图中使用这些数据 所以我这样做了:
@foreach($results as $result => $datas)
@foreach($datas as $data => $attributes )
@foreach($attributes as $attribute => $value)
<tr>
@foreach($attribute["Domain"] as $domain)
<td>{{ $domain }}</td>
@endforeach
<td>.COM</td>
<td>.NET</td>
<td>.ORG</td>
</tr>
@endforeach
@endforeach
@endforeach
但当我使用它时:{{ $attribute['Domain'] }}
它说:
非法字符串偏移......
当我使用它时:{{ $attribute->Domain }}
它说
尝试获取非对象的属性......
我不知道我还应该尝试什么!请帮我。谢谢。
P.S:我知道,这里没有必要使用四个foreach。应该有更好的方法来做到这一点。
答案 0 :(得分:1)
如果public partial class MainWindow : Window
{
public MainWindow
{
InitializeComponent();
this.PreviewKeyDown += (s, e) =>
{
if(e.Key == Key.F4)
SelectRowByIndex(dataGridProducts, dataGridProducts.Items.Count - 1);
};
//populate DataGrid etc...
}
private static void SelectRowByIndex(DataGrid dataGrid, int rowIndex)
{
if (!dataGrid.SelectionUnit.Equals(DataGridSelectionUnit.FullRow))
throw new ArgumentException("The SelectionUnit of the DataGrid must be set to FullRow.");
if (rowIndex < 0 || rowIndex > (dataGrid.Items.Count - 1))
throw new ArgumentException(string.Format("{0} is an invalid row index.", rowIndex));
dataGrid.SelectedItems.Clear();
object item = dataGrid.Items[rowIndex];
dataGrid.SelectedItem = item;
DataGridRow row = dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow;
if (row == null)
{
/* bring the data item (Product object) into view
* in case it has been virtualized away */
dataGrid.ScrollIntoView(item);
row = dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow;
}
if (row != null)
{
DataGridCell cell = GetCell(dataGrid, row, 0);
if (cell != null)
cell.Focus();
}
}
private static DataGridCell GetCell(DataGrid dataGrid, DataGridRow rowContainer, int column)
{
if (rowContainer != null)
{
System.Windows.Controls.Primitives.DataGridCellsPresenter presenter
= FindVisualChild<System.Windows.Controls.Primitives.DataGridCellsPresenter>(rowContainer);
if (presenter == null)
{
/* if the row has been virtualized away, call its ApplyTemplate() method
* to build its visual tree in order for the DataGridCellsPresenter
* and the DataGridCells to be created */
rowContainer.ApplyTemplate();
presenter = FindVisualChild<System.Windows.Controls.Primitives.DataGridCellsPresenter>(rowContainer);
}
if (presenter != null)
{
DataGridCell cell = presenter.ItemContainerGenerator.ContainerFromIndex(column) as DataGridCell;
if (cell == null)
{
/* bring the column into view
* in case it has been virtualized away */
dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[column]);
cell = presenter.ItemContainerGenerator.ContainerFromIndex(column) as DataGridCell;
}
return cell;
}
}
return null;
}
private static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is T)
return (T)child;
else
{
T childOfChild = FindVisualChild<T>(child);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}
}
是提供的数组,那么您可以将代码简化为:
$results