skip to Main Content

This is my response:

Array
(
    [0] => Array
        (
            [1] => Array
                (
                    [aid] => 1
                    [address] => Surat
                    [country] => India
                    [state] => Gujarat
                    [city] => Surat
                    [pincode] => 395010
                    [full_name] => Pra test
                    [mobile_no] => 7984509142
                    [email] => 
                    [default] => 0
                )

        )

)

I want response like:


Array
(
     [1] => Array
         (
             [aid] => 1
             [address] => Surat
             [country] => India
             [state] => Gujarat
             [city] => Surat
             [pincode] => 395010
             [full_name] => Pra test
             [mobile_no] => 7984509142
             [email] => 
             [default] => 0
         )
)

I want to remove 0 index from my response. I want my response like what I define below. so how can I do this with functions and etc..

4

Answers


  1. Chosen as BEST ANSWER

    I use array_replace_recursive() function and get response I want.

    now my response is :

    Array
    (
        [1] => Array
            (
                [aid] => 1
                [address] => Surat
                [country] => India
                [state] => Gujarat
                [city] => Surat
                [pincode] => 395010
                [full_name] => Pra test
                [mobile_no] => 7984509142
                [email] => 
                [default] => 0
            )
    
        [2] => Array
            (
                [aid] => 2
                [address] => Surat
                [country] => India
                [state] => Gujarat
                [city] => Surat
                [pincode] => 395010
                [full_name] => Pra test
                [mobile_no] => 7984509142
                [email] => 
                [default] => 0
            )
    
    )
    

  2. It sounds like you are trying to remove an extra layer from a multi-dimentional array.

    An easy way would be something like:

    $new_array = $old_array[0];

    Edit:

    Per @Gert B’s suggestion:
    To return the first element regardless of the key:

    $new_array = reset($old_array);

    The reset function returns the pointer to the beginning of the array, and more importantly for this question, returns the first element of the array.

    Login or Signup to reply.
  3. Try this,

    $data = 'YOUR_DATA_ARRAY';
    $result = [];
    foreach ($data as $category => $attributes) {
        foreach ($attributes as $attribute => $options) {
            foreach ($options as $option) {
                $result[] = array('category' => $category, 'attribute' => $attribute, 'option' => $option);
            }
        }
    }
    
    Login or Signup to reply.
  4. $result = $old_array[0];
    

    This was the easiest way …

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search