Php - 如何在数组中存储3个元素的元组?

时间:2015-03-05 08:56:28

标签: php arrays

我需要在循环的每次迭代中存储三个字符串值,例如

$myArray = array();
foreach (..) {
  $myArray[] = // I need to store as a single row or tuple three strings
}
foreach ($myArray as $arrayTuple)
  // Do something with $arrayTuple.firstString, $arrayTuple.secondString and $arrayTuple.thirdString

我似乎无法理解如何使用关联数组执行此操作。

3 个答案:

答案 0 :(得分:2)

如果我理解正确,那么你想要这样

$myArray = array();
foreach (..) {
  $myArray[] = array("one","two","three");
}

答案 1 :(得分:0)

如果我理解正确的话,这对你有帮助:

foreach ($array as $key => $val) {
    $myArray[$key] = $val;
}

答案 2 :(得分:0)

您可以使用array_push()

例如:

$myArray = array();
array_push($myArray, "one","two","three");

或者

$myArray = array();
array_push($myArray, "one:two:three");