I have array variable which is unpredicted format and length. For example:
$arrayData = array(
0 => (int) 100,
1 => (date) 2012-12-01,
2 => array(
0 => (string) 'some string',
1 => (int) 200,
),
3 => (string) 'another string'
);
I need to remove array key and it’s value and keep just string data type.
Maybe some one can suggest me the right way to resolve that problem instead of use loop and gettype.
Result will be:
$arrayData = array(
2 => array(
0 => (string) 'some string',
),
3 => (string) 'another string'
);
or:
$arrayData = array(
0 => (string) 'some string',
1 => (string) 'another string'
);
or others suggestion result are welcome.
You could iterate over the array and use the
gettype()function to keep/remove array items. However, you need a recursive function to handle arrays within array. Notice the careful use of two&in the following code example:Original answer – using
array_walk_recursivewith global variable