开发者

php edit the last char in the last occurrence of a element within an array of arrays

开发者 https://www.devze.com 2023-03-14 18:25 出处:网络
I\'m a novice php programmer and having a problem assembling this code to work. what I\'m tiring to to is this,

I'm a novice php programmer and having a problem assembling this code to work. what I'm tiring to to is this,

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?
        )

)
---------------------------

As you can see I'm tiring to replace the "," with a "?" question mark on each last char of each last element in each sub-array.

code-wise this is where I'm up to now:

    $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
    }
   }

-

Tr开发者_开发技巧ouble is I'm pointer locked and I've got string functions and array functions interacting with each other and at the same time tiring to traverse each sub-array. I tried functionalizing the code but can't figure out how to get php to display the original array structure with the question mark edits on each of the last chars on the last elements in each array, Yaakes.

As stated I am a novice and this is one of the hardest functions I've ever engaged in. I'm also tiring to find a better way of doing this without using the php string functions, only array functions as I think my approach is wrong. Any suggestions, pseudo code or knowledge and direction would be most grateful to improve my understanding on this subject, thanks~


I think you are over complicating your solution, just loop through every three elements and combine them:

$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);
}

This will have issues with the last elements if the size of the array is not a multiple of 3.


As a function for multidimensional arrays:

<?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;
    }

?>


Try:

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) . '?';
}

OR a more legible version:

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) . '?';
}


You could use the preg_replace function as -

$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);
0

精彩评论

暂无评论...
验证码 换一张
取 消