[英]Pushing to multidimensional array php
Can anyone help me how to insert multidimensional array to my array in php? 谁能帮我如何在PHP中将多维数组插入到我的数组中? here is the sample code: 这是示例代码:
$valuesNeeded = array(
array('a', 'a', 'a')
);
$entryOne = 'b';
$entryTwo = 'b';
$entryThree = 'b';
the values should be the same if I do this: 如果执行此操作,则值应相同:
$valuesNeeded = array(
array('a', 'a', 'a'), array('b','b','b')
);
the reason for this is the 2nd array will be given via input so i dont know how to do this. 原因是第二个数组将通过输入给出,所以我不知道该怎么做。 help please. 请帮助。
使用array_push
array_push($valuesNeeded,[$entryOne,$entryTwo,$entryThree]);
Just put your values to new array and assign them as new element of your $valuesNeeded
array: 只需将您的值放入新数组并将它们分配为$valuesNeeded
数组的新元素:
$valuesNeeded[] = array($entryOne, $entryTwo, $entryThree);
OR 要么
$newRow = array();
$newRow[] = $entryOne;
$newRow[] = $entryTwo;
$newRow[] = $entryThree;
$valuesNeeded[] = $newRow;
@Rey Norbert Besmonte simply make an array with these three entry and insert it in $valuesNeeded array like below @Rey Norbert Besmonte只需使用这三个条目创建一个数组,然后将其插入$ valuesNeeded数组,如下所示
<?php
$valuesNeeded = array(
array('a', 'a', 'a')
);
$entryOne = 'b';
$entryTwo = 'b';
$entryThree = 'b';
$valuesNeeded[] = array($entryOne, $entryTwo, $entryThree);
?>
Try this, 尝试这个,
$valuesNeeded = array(array('a', 'a', 'a'));
$entryOne = 'b';
$entryTwo = 'b';
$entryThree = 'b';
$valuesNeeded[] = [$entryOne, $entryTwo, $entryThree];
This should solve your problem. 这应该可以解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.