0

I'am executing code :

<?php
$input="ABC123";
$splits = chunk_split($input,2,"");
foreach($splits as $split)
{
$split = strrev($split);
$input = $input . $split;
}
?>

And output that i want is :

BA1C32

But it gaves me Warning.

Warning: Invalid argument supplied for foreach() in /home/WOOOOOOOOHOOOOOOOO/domains/badhamburgers.com/public_html/index.php on line 4
2
  • 1
    You can start by doing print_r($splits) Commented Nov 13, 2011 at 18:35
  • 1
    chuck_split return a string, you can't do a foreach on a string Commented Nov 13, 2011 at 18:37

3 Answers 3

5

chunk_split doesn't return an array but rather a part of the string.

You should use str_split instead:

$input="ABC123";
$splits = str_split($input, 2);

And don't forget to reset your $input before the loop, else it will contain the old data aswell.

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

1 Comment

don't forget to reset your input or change your variable name.
1

It looks like http://php.net/manual/en/function.chunk-split.php returns a string, not an array. You could use str_split instead:

$input = "ABC123";
$splits = str_split( $input, 2 );
$output = "";
foreach( $splits as $split ){
   $split = strrev($split);
   $output .= $split;
}

Comments

0

As the documentation for chunk_split mentions clearly, http://php.net/manual/en/function.chunk-split.php chunk split returns a string.

foreach expects an array as first argument.

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.