我是一名新手php程序员,并且在编写此代码时遇到问题。 我正在努力的是这个,
From this:
----------------------------
Array
(
[0] => Array
(
[0] => apples,
[1] => oranges,
[2] => lettuce,
)
[1] => Array
(
[0] => bananas,
[1] => grapes,
[2] => cabbage,
)
[2] => Array
(
[0] => pears,
[1] => mangoes,
[2] => celery,
)
)
----------------------------
To this:
Array
(
[0] => Array
(
[0] => apples,
[1] => oranges,
[2] => lettuce?
)
[1] => Array
(
[0] => bananas,
[1] => grapes,
[2] => cabbage?
)
[2] => Array
(
[0] => pears,
[1] => mangoes,
[2] => celery?
)
)
---------------------------
正如你所看到的,我很厌倦用“?”代替“,”每个子数组中每个最后一个元素的每个最后一个字符上的问号。
代码方式这就是我现在所处的位置:
$array = array("apples,", "oranges,", "lettuce,", "bananas,", "grapes,", "cabbage,", "pears,", "mangoes,", "celery,");
$subarrays = array_chunk($array, 3);
print_r($miniarrays);
foreach ($subarrays as $value){
//$output1 = array_slice($value, 2, 1, true); #<== required maybe?
$output1 = array_slice($value, 2, 1);
print_r($output1); #<== debug purposes only
foreach ($output1 as $val){
$locate = $val[strlen($val)-1];
print_r($result); #<== debug purposes only
//foreach ($result as $val2) #<== required maybe?
$output3 = strtr($locate, ",", "?");
//echo $output2;
print_r($output3); #<== debug purposes only
}
}
-
麻烦的是我被指针锁定了,我的字符串函数和数组函数相互交互,同时又厌倦遍历每个子数组。我尝试了功能化代码,但无法弄清楚如何让php显示原始数组结构,并在每个数组中最后一个元素的每个最后一个字符上编辑问号,Yaakes。
如上所述,我是一个新手,这是我参与过的最难的功能之一。我也很累,想要找到一个更好的方法,不使用php字符串函数,只考虑我认为的数组函数我的做法是错的。任何建议,伪代码或知识和方向都会非常感激,以提高我对这个主题的理解,谢谢〜
答案 0 :(得分:0)
我认为你的解决方案过于复杂,只需遍历每三个元素并将它们结合起来:
$arr = array("apples,", "oranges,", "lettuce,", "bananas,", "grapes,", "cabbage,", "pears,", "mangoes,", "celery,");
$result = array();
for($i = 0; $i < count($arr); $i+=3){
$s = $arr[$i + 2];
$s[strlen($s) - 1] = '?';
$result[] = array($arr[$i], $arr[$i + 1], $s);
}
如果数组的大小不是3的倍数,那么最后一个元素会出现问题。
答案 1 :(得分:0)
作为多维数组的函数:
<?php
// Start array
$array = array(array("apples", "oranges", "lettuce"),array("bananas", "grapes", "cabbage"),array("pears", "mangoes", "celery"));
$newArray = addtoarray($array, "?");
// Echo out the new array
foreach ($newArray as $aKey => $aValue) {
echo "Array: {$aKey}<br />";
foreach ($aValue as $key => $value) {
echo "Key: ". $key . " - Value: " . $value . "<br />";
}
echo "<br />";
}
function addtoarray($array, $stringtoadd) {
$newArray = array();
foreach ($array as $aValue) {
$count = count($aValue);
$tArray = array();
foreach ($aValue as $key => $value) {
if ($key == $count - 1) {
array_push($tArray, $value . $stringtoadd);
}
else {
array_push($tArray, $value);
}
}
array_push($newArray, $tArray);
}
return $newArray;
}
?>
答案 2 :(得分:0)
尝试:
foreach ($subarrays as $value){
//replaces all occurences of ',' with '?' within the last element of the subarray
$value[sizeof($value)-1] = str_replace(',', '?', $value[sizeof($value)-1]);
}
OR
foreach ($subarrays as $value){
//replaces last character with '?' within the last element of the subarray
$value[sizeof($value)-1] = substr($value[sizeof($value)-1], 1, strlen($value[sizeof($value)-1])-1) . '?';
}
或更易读的版本:
foreach ($subarrays as $value){
//replaces last character with '?' within the last element of the subarray
$str = $value[sizeof($value)-1];
$value[sizeof($value)-1] = substr($str, 1, strlen($str)-1) . '?';
}
答案 3 :(得分:0)
您可以将preg_replace函数用作 -
$array = array("apples,", "oranges,", "lettuce,", "bananas,", "grapes,", "cabbage,", "pears,", "mangoes,", "celery,");
for($i=2;$i<count($array);$i=$i+3)
$array[$i]= preg_replace('/^([a-z]+),$/','${1}?',$array[$i]);
print_r($array);