0

I want to seperate the values of array by string and integer to make it more readable.

Example:

$array = [1,2,3,"string"];

Return would be like this:

'int' => [
    1,
    2,
    3
]
'string' => [
    "string"
]
2
  • What do you mean by to make it more readable? Commented Jul 20, 2019 at 14:58
  • I want to get the value of array if it is int or string Commented Jul 20, 2019 at 15:01

1 Answer 1

4

You could do this using is_int and is_string:

$result = [];
foreach ($array as $value) {
    if(is_int($value)) {
        $result['int'][] = $value;  
    }
    else if(is_string($value)) {
        $result['string'][] = $value;
    }
}

Demo

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

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.