0

Lets say I have an array like this, it could be multi-dimensional so I do need to make this loop recursive.

I think I'm close but can't quite see where I'm wrong.

[ 
    { "value": "rigging" }, 
    { "value": "animation" }, 
    { "value": "modeling" }
]


function _replace_amp($post = array()) {
    foreach($post as $key => $value)
    {
        if (is_array($value)) {
           $b = $this->_replace_amp($value);
        }  else  {
            $b .= $value . ', ';
        }
    }

    return $b;
}

The intended result should be:

"rigging, animation, modeling"

I'm getting just "modeling,"

3
  • used implode(',',$array); Commented Oct 6, 2014 at 12:19
  • do you mean you have an array of objects? (like in your code example)? Commented Oct 6, 2014 at 12:21
  • If you’ll look closely at your array, you’ll note it contains one element, overwritten twice. Keys are the same. Commented Oct 6, 2014 at 12:21

4 Answers 4

1

In your code, you need to write

$b .= $this->_replace_amp($value); // note the period

Without the period, you are initiating $b every time your script finds a new array, but you want to append the results to $b.

Other than that, there is a nice implode function for multidimensional arrays available:

/**
 * Recursively implodes an array with optional key inclusion
 * 
 * Example of $include_keys output: key, value, key, value, key, value
 * 
 * @access  public
 * @param   array   $array         multi-dimensional array to recursively implode
 * @param   string  $glue          value that glues elements together   
 * @param   bool    $include_keys  include keys before their values
 * @param   bool    $trim_all      trim ALL whitespace from string
 * @return  string  imploded array
 */ 
function recursive_implode(array $array, $glue = ',', $include_keys = false, $trim_all = true)
{
    $glued_string = '';

    // Recursively iterates array and adds key/value to glued string
    array_walk_recursive($array, function($value, $key) use ($glue, $include_keys, &$glued_string)
    {
        $include_keys and $glued_string .= $key.$glue;
        $glued_string .= $value.$glue;
    });

    // Removes last $glue from string
    strlen($glue) > 0 and $glued_string = substr($glued_string, 0, -strlen($glue));

    // Trim ALL whitespace
    $trim_all and $glued_string = preg_replace("/(\s)/ixsm", '', $glued_string);

    return (string) $glued_string;
}

Source: https://gist.github.com/jimmygle/2564610

Sign up to request clarification or add additional context in comments.

Comments

0

I think either json_encode( your_php_array ) or serialize() function is help you.

Comments

0

You want to use function implode(). No need to re-invent the wheel.

<?php

$arr = ['one', 'two', 'three'];

echo implode(',', $arr); // one, two, three

1 Comment

Look at the OP’s array closely. It has the only element, despite the syntax is not php-conformed.
0

$b = $this->_replace_amp($value); change in this line to $b .= $this->_replace_amp($value); this answer as per your coding

[ 
 { "value": "rigging" }, 
 { "value": "animation" }, 
 { "value": "modeling" }
]


function _replace_amp($post = array()) {
    foreach($post as $key => $value)
    {
        if (is_array($value)) {
           $b .= $this->_replace_amp($value);
        }  else  {
            $b .= $value . ', ';
        }
    }

    return $b;
}

batter way to do this using used implode(',',$array);

1 Comment

Implode didn't work, it just kept spitting out 'Array'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.