1

I was trying to make a function con convert an array in a string value, the code is:

function makestring($array)
  {
  $outval = '';
  foreach($array as $key=>$value)
    {
    if(is_array($value))
      {
      $outval .= makestring($value);
      }
    else
      {
      $outval .= $value;
      }
    }
  return $outval;
  }

But I get this error: Warning: Invalid argument supplied for foreach(). Can anybody please help me?

2
  • Check your array is not empty Commented Jul 4, 2013 at 8:37
  • 1
    Check the typeof $array is really an Array or not and not an empty Commented Jul 4, 2013 at 8:37

8 Answers 8

1
function makestring($array)
{
    if(is_array( $array )) {
        $outval = '';
        foreach($array as $key=>$value)
        {
            if(is_array($value))
            {
                $outval .= makestring($value);
            }
            else
            {
                $outval .= $value;
            }
        }
        return $outval;
    } else {
        die("Supplied argument is not an array");
    }
}
OR 
function makestring( array $array)
{
    // you code goes here
}

Try this. You need to check passing argument is array or not before you use foreach

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

Comments

0

To prevent an error in foreach, it's better to set type to variable:

foreach((array)$someArray as $data) {

Even if $someArray is not an array, you'll not get any error.

Comments

0

→ Try this:

  function makestring($array)
  {
       $outval = '';
       $keys = array_keys( $array );

       for( $x = 0; $x < count( $array ); $x++ )
       {
            if( is_array( $array[ $keys[ $x ] ] ) )
            {
                 $outval .= makestring( $array[ $keys[ $x ] ] );
            }
            else
            {
                 $outval .= $array[ $keys[ $x ] ];
            }
       }
  return $outval;
  }

Comments

0

Maybe the variables you use $key and $value has old data in them , normally a unset($key,$value) in the end of a foreach will cure that

Comments

0
$array = array(
    '1',
    '2',
    '3',
    array(
        '4',
        '5',
        array(
            '6',
            '7'
        )
    )
);
$output = null;
if(!is_array($array)){
    $array = array($array);
}
// use a reference to the $output in the callback
array_walk_recursive($array, function($array_element) use (&$output){
    if(is_object($array_element) and method_exists($array_element, '__toString')){
        $array_element = strval($array_element);
    }
    if(is_scalar($array_element)){
        $output .= $array_element;
    }else{
        // found a non scalar... handle it! :)
    }
});
echo $output;

Check this out.

Comments

0
array_walk_recursive((array) $array, function($val, $key) {
  global $result;
  $result .= $val;
});

Comments

0

You check via

if(is_array($value))
{
  $outval .= makestring($value);
}

whether to call makestring() recursively. However, in the code that enters makestring() in the first place, you do no such check. That's ok, but then you have to check in makestring(), whether you actually got an array from the caller. That's called defensive programming.

Another option is to do the check on the calling side. That's part of Design By Contract™:

$var = i_usually_give_you_an_array_but_i_might_also_fail();
if (is_array($var)) {
  echo makestring($value);
}

Comments

0

recursive implode() this convert multidimensional array to string

function mYimplode($array, $delimeter) {
 $result='';
 foreach ($array as $key) {
    if (is_array($key)) {
        $result .= mYimplode($key, $delimeter) . $delimeter;
    } else {
        $result .= $key . $delimeter;
    }
 }

 $result = substr($result, 0, 0-strlen($delimeter));

 return $result;
}

Comments

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.