Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9027161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:41:30+00:00 2026-06-16T06:41:30+00:00

I have array variable which is unpredicted format and length. For example: $arrayData =

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T06:41:31+00:00Added an answer on June 16, 2026 at 6:41 am

    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:

    $arrayData = array(
        0 => (int) 100,
        1 => '2012-12-01',
        2 => array(
            0 => (string) 'some string',
            1 => (int) 200
        ),
        3 => (string) 'another string'
    );
    function remove_nonstring_recursive(&$array) {
        foreach ($array as $key => &$value) {
            $type = gettype($value);
            if ($type == "array") {
                remove_nonstring_recursive($value);
            } elseif ($type != "string") {
                unset($array[$key]);
            }
        }
    }
    remove_nonstring_recursive($arrayData);
    var_dump($arrayData);
    // array(3) {
    //   [1]=>
    //   string(10) "2012-12-01"
    //   [2]=>
    //   array(1) {
    //     [0]=>
    //     string(11) "some string"
    //   }
    //   [3]=>
    //   string(14) "another string"
    // }
    

    Original answer – using array_walk_recursive with global variable

    $arrayData = array(
        0 => (int) 100,
        1 => '2012-12-01',
        2 => array(
            0 => (string) 'some string',
            1 => (int) 200
        ),
        3 => (string) 'another string'
    );
    $arrayDataFiltered = array();
    function remove_non_string($value, $key) {
        global $arrayDataFiltered;
        if (gettype($value) == "string") {
            $arrayDataFiltered[] = $value;
        }
    }
    array_walk_recursive($arrayData, "remove_non_string");
    var_dump($arrayDataFiltered);
    // array(3) {
    //   [0]=>
    //   string(10) "2012-12-01"
    //   [1]=>
    //   string(11) "some string"
    //   [2]=>
    //   string(14) "another string"
    // }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string array variable called 'myAttachmentArray[]' which holds different figures like this:
I have a string variable which contains the name of an array. What I'd
I have a Session variable which is an array of integers, for example: myArray{1,4,3,5,6,7,9,2,...n}
I am having some trouble I have $currentPages variable which store below data Array
I have an array stored in a variable temp which looks like this: temp.inspect
I have this array, for example (the size is variable): x = [1.111, 1.122,
Let's say I have a C# variable and array: int variable_1 = 1; int[3]
I have a variable length array of strings declared in javascript that contains Dungeons
i have a variable $visitRecord->getReferallist() which get the list of doctors in an array
I have a problem in copying the data in array from object variable which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.