1

Hi guys I have to port a few functions from AS to PHP, but I ran into a small problem.

There's an array, lets called it arr1. There is a switch, that pushes argument to this array, but after the switch ends, I'm stuck at join("").split("");

var arr1 = [];
    switch (myString)
    {
        case "apple":
        {
            arr1.push("apple");
            break;
        } 
    }
}
arr1 = arr1.join("").split("");

I know is simple, but i never had experience with arrays in flash before, I just realized join("") was used to convert the Array to string, but isn't split reversing this?

2 Answers 2

2

join(delimiter:*) will create a string from an array where each of the items from the array are separated by whatever the delimiter is (defaults to NaN, if you're interested).

split(delimiter:*, limit:Number = 0x7fffffff) beaks a string into an array of limit length based on whatever is the delimiter.

while this may look like it should output the same array consistently:

var delim:String = "someString";
arr.join(delim).split(delim)

In this case, it will actually result in a different array:

var arr:Array = ['asomeStringb','c','d']
var delim:String = "someString";
trace(arr.join(delim).split(delim))//[a,b,c,d]

If delimiter is an empty string for split, it will break the string up character by character:

var arr:Array = ['ab','c','d']
var delim:String = "";
trace(arr.join(delim).split(delim))//[a,b,c,d]

Hope that helps.

PHP equivalent might be:

$input = array( /*stuff...*/ );
// join is an alias of implode. I used it here because the AS method is join.
$input = /* explode( <-- won't work */ str_split( "", join($input)); 

Side note: in AS3 always type your variables as strictly as possible -- it really helps in the end. var arr1 should be var arr1:Array.

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

2 Comments

Im sorry, but what should be the PHP equivalent to this?
See my comment to my answer, you can't use explode with an empty delimiter.
0

What this code is doing is first joining all your array values together into a string, and then splitting them into an array of the separate characters. The PHP equivalent would look something this (tested):

$arr1 = array("apple");  //arr1 after the switch
$arr1 = str_split(implode('',$arr1));

2 Comments

For some reason its not correct implementation, my function errors out.
Yeah, sorry. explode() doesn't allow an empty delimiter, you have to use str_split() instead.

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.