[英]creating a multidimensional array from arrays in php
I'm trying to create an array of bar objects in php which consist of seven different attributes. 我正在尝试在php中创建包含七个不同属性的bar对象数组。 The code I am using for this array is as follows:
我为此数组使用的代码如下:
$barsArray = array();
for($i = 0; $i < count($barNameArray); $i++)
{
$barsArray[] = array('BarName' => $barNameArray[$i], 'first' => $firstCover[$i], 'timeFirst' => $firstTimes[$i],
'second' => $secondCover[$i], 'timeSecond' => $secondTimes[$i],
'third' => $thirdCover[$i], 'timeThird' => $thirdTimes[$i]);
}
I have checked to make sure that all the other arrays are as I intend them. 我已经检查以确保所有其他阵列都符合我的预期。 I just need to get this into one array of objects.
我只需要将其放入一个对象数组即可。 Is this method completely off?
这种方法完全关闭了吗? Also, If I wanted to test to make sure that the correct objects are in the correct locations in a multidimensional array, how would I go about do that?
另外,如果我想进行测试以确保正确的对象在多维数组中的正确位置,我该怎么做?
Thanks! 谢谢!
That code looks fine (although you may want to cache the count
instead of performing it repeatedly). 该代码看起来不错(尽管您可能希望缓存
count
而不是重复执行)。
I can't say for sure, not knowing your greater purpose, but you may want to make $barsArray
an associative array by the bar name. 我不能肯定地说,不知道您有更大的用途,但是您可能希望将
$barsArray
作为条形名称的关联数组。 To do that, use $barsArray[$barNameArray[$i]] =
, instead of $barsArray[] =
(and of course remove the BarName
property). 为此,请使用
$barsArray[$barNameArray[$i]] =
,而不是$barsArray[] =
(当然,请删除BarName
属性)。 This would not keep it in the original ordering, but would make getting a particular bar easier. 这不会将其保留在原始顺序中,但是会更容易获得特定的小节。
You can get an element from $barsArray
like so: $barsArray[3]['first']
(or $barsArray['some_bar_name']['first']
if you change it to an associative array). 您可以像这样从
$barsArray
获取元素: $barsArray[3]['first']
(如果将其更改为关联数组,则可以是$barsArray['some_bar_name']['first']
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.