3

I have a collection of array it contains both numeric index as well as non numeric. I want to unset all the numeric indexes.

My array is something like this .

Array
(

    [554] => Array
        (
            [0] => 554
            [1] => Jiaqi Zheng
            [2] => Female
            [3] => 28
            [4] => Table Tennis
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [rank] => 554
            [athlet_name] => Jiaqi Zheng
            [gender] => Female
            [sport] => Table Tennis
        )

    [555] => Array
        (
            [0] => 555
            [1] => Zach Ziemek
            [2] => Male
            [3] => 23
            [4] => Athletics
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [rank] => 555
            [athlet_name] => Zach Ziemek
            [gender] => Male
            [sport] => Athletics
        )
)

Here i have to unset all the numeric index .

I used unset like this and its working fine for me .

unset(
                     $history_years_wise_country_wise_details_arr[     $history_years_wise_country_wise_details_info[0]][0],
                      $history_years_wise_country_wise_details_arr[$history_years_wise_country_wise_details_info[0]][1],
                      $history_years_wise_country_wise_details_arr[$history_years_wise_country_wise_details_info[0]][2],
                      $history_years_wise_country_wise_details_arr[$history_years_wise_country_wise_details_info[0]][3],
                      $history_years_wise_country_wise_details_arr[$history_years_wise_country_wise_details_info[0]][4],
                      $history_years_wise_country_wise_details_arr[$history_years_wise_country_wise_details_info[0]][5],
                      $history_years_wise_country_wise_details_arr[$history_years_wise_country_wise_details_info[0]][6],
                      $history_years_wise_country_wise_details_arr[$history_years_wise_country_wise_details_info[0]][7],
                      $history_years_wise_country_wise_details_arr[$history_years_wise_country_wise_details_info[0]][8]
                      );

Is there any way I will reduce the lines of codes? here 0 to 8 are in one series.

Can I unset all index in one line of code , as all are numeric?

Is it possible to use regular expression instead?

I want something like

unset(
                     $history_years_wise_country_wise_details_arr[     $history_years_wise_country_wise_details_info[0]][anything_which_will_take_index_from_0_to_8]);

Any suggestions?

Thank you

4 Answers 4

3

You could use array_filter() with is_string() function as its callback function:

$array = array_filter($array, 'is_string', ARRAY_FILTER_USE_KEY);
Sign up to request clarification or add additional context in comments.

2 Comments

I am getting result like this . Array ( [] => Array ( [rank] => [athlet_name] => [gender] => [sport] => ) )
I tried your code . $array = array(); foreach($history_years_wise_country_wise_details_arr as $key2=> $hywcwda) { $array[] = array_filter($hywcwda, 'is_string', ARRAY_FILTER_USE_KEY); } echo '<pre>';print_r($array);exit;
2

Use looping.

foreach ($array as $key => $value) {
    if (is_numeric ($key)) {
        unset($array [$key]);
    }
}

Or use array_filter

$filtered = array_filter(
    $array,
    function ($key) {
        return !is_numeric($key);
    },
    ARRAY_FILTER_USE_KEY
);

2 Comments

Array ( [] => Array ( [rank] => [athlet_name] => [gender] => [sport] => ) ) I think it removed all the main keys .
Loop through main array and filter child elements. revo's answer is better one.
1

You shoud used foreach loop with is_numeric function like

foreach ($your_array as $key => $value) {
    if (!is_numeric($key)) {
        unset($arr[$key]);
    }
}

i think there is no need of any regular expression

1 Comment

again i have to use looping , is there any other way ? ,in same code i can not do it i guess
1

Since you have array inside array first you need to use array_map() and then traverse through array using array_filter(),

considering $array as your array:

$resultData = array_map([$this, 'allData'], $array);

 public function allData($data)
 {
    $numericKeys = array_filter(array_keys($data), function ($k) {
        return is_int($k);
    });
    // Updated Code
    $arrayKeys = array_diff(array_keys($data),$numericKeys);
    return array_intersect_key($data,array_flip($arrayKeys));
 }

3 Comments

[554] => Array ( [9] => rank [10] => athlet_name [11] => gender [12] => sport ) [555] => Array ( [9] => rank [10] => athlet_name [11] => gender [12] => sport ) removed numeric index but data is coming wrong.
I got the values . [554] => Array ( [0] => 554 [1] => Jiaqi Zheng [2] => Female [3] => Table Tennis ) [555] => Array ( [0] => 555 [1] => Zach Ziemek [2] => Male [3] => Athletics ) I changed return array_diff(array_keys($data),$numericKeys); to return array_diff(array_values($data),$numericKeys);
I want associative array only . here i am getting numeric index

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.