phparraysimplode

Join array element value with its second neighboring element value (if it exists)


Is it possible to implode multiple array values?

From what I have researched so far, all the examples include imploding multi dimensional arrays (using array_map) or imploding all values from an array using implode(',', $array);

However, I would like to know if there is a way to implode array values from an array:

array([0] (
   array [0] ([0] A, [1] B, [2] C, [3] H)
   array [1] ([0] A, [1] D, [2] G, [3] H, [4] L)
   array [2] ([0] D, [1] Z, [2] J, [3] K, [4] O, [5] X)
)
array([1] (
   array [2] ([0] F, [1] Y, [2] W, [3] H, [4] L)
)
array([2] (
   array [0] ([0] O, [1] T, [2] C, [4] O, [5] X)
   array [1] ([0] U, [1] E, [2] E, [3] D)
))

Note: the strings in each array can be repeated several times and it has no bearing on the outcome.

Desired Outcome

to arrive at a result that looks like this:

$result = array(
          array [0] (A_C, B_H, C)
          array [1] (A_G, D_H, G_L)
          etc...

The expected results should allow me to test, IF the value cannot be combined (because it is at the end of the array), then display the single value

Being a beginner, my first resort was to try out implode implode($array[0], '_', $array[2]);
but I found out it does not work as 2 parameters are allowed


Solution

  • Some examples to use implode in multidimensional array

    $sampleArray = array(array('abc', 'def', 'hij'), array('mno', 'xxy', 'kkl'));
    foreach($sampleArray as $key => $val) {
        $impArray = array($val[0], $val[2]);
        echo implode('_', $impArray);
    }
    
    
    -- -- -- -- -- -Dynamic implode with array first and last value-- -- -- -- -
    
    $sampleArray = array(array('abc', 'def', 'hij'), array('mno', 'xxy', 'kkl'));
    foreach($sampleArray as $key => $val) {
        $count = count($val);
        if ($count > 1) {
            $first = reset($val);
            $last = end($val);
            $impArray = array($first, $last);
        } else {
            $first = reset($val);
            $impArray = array($first);
        }
    
        echo implode('_', $impArray);
    }
    
    -- -- -- -- -- -- -- -- -- -- -- -Fully Dynamic with odd, even rules-- -- -- -- --
    
    $finalArray = array();
    $sampleArray = array(array('abc', 'def', 'hij', 'dsfd', 'fff'), array('mno', 'xxy', 'kkl'));
    foreach($sampleArray as $key => $val) {
        $oddEvenArr = make_array_odd_even($val);
        if (!empty($oddEvenArr['odd']) || !empty($oddEvenArr['even'])) {
            $findMaxSize = 0;
            $oddSize = count($oddEvenArr['odd']);
            $evenSize = count($oddEvenArr['even']);
            if ($oddSize > $evenSize) {
                $findMaxSize = $oddSize;
            } else {
                $findMaxSize = $evenSize;
            }
            for ($i = 0; $i < $findMaxSize; $i++) {
                if (array_key_exists($i, $oddEvenArr['even'])) {
                    $finalArray[$key][] = implode('_', $oddEvenArr['even'][$i]);
                }
                if (array_key_exists($i, $oddEvenArr['odd'])) {
                    $finalArray[$key][] = implode('_', $oddEvenArr['odd'][$i]);
                }
            }
        }
    }
    
    function make_array_odd_even($temp) {
        $odd = array();
        $even = array();
        foreach($temp as $k => $v) {
            if ($k % 2 == 0) {
                $even[] = $v;
            } else {
                $odd[] = $v;
            }
        }
        $oddChunk = array_chunk($odd, 2);
        $evenChunk = array_chunk($even, 2);
        return array('odd' => $oddChunk, 'even' => $evenChunk);
    }
    echo "<pre>";
    print_r($finalArray);
    die;