Home > Enterprise >  how can I remove 0 index key from my result in php?
how can I remove 0 index key from my result in php?

Time:12-21

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..

CodePudding user response:

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.

CodePudding user response:

To remove the first element (i.e., the element with key 0) from an array in PHP, you can use the array_shift function. This function removes the first element of the array and returns the value of the element that was removed. For example:

    $array = [0 => 'a', 1 => 'b', 2 => 'c'];
$first_element = array_shift($array);

print_r($array); // Outputs: [1 => 'b', 2 => 'c']
echo $first_element; // Outputs: 'a'

Alternatively, you can use the unset function to remove an element from an array by its key. For example:

$array = [0 => 'a', 1 => 'b', 2 => 'c'];
unset($array[0]);

print_r($array); // Outputs: [1 => 'b', 2 => 'c']

Note that using unset will leave a gap in the numbering of the keys of the remaining elements. If you want to preserve the numbering of the keys, you can use the array_values function to reindex the array. For example:

$array = [0 => 'a', 1 => 'b', 2 => 'c'];
unset($array[0]);
$array = array_values($array);

print_r($array); // Outputs: [0 => 'b', 1 => 'c']

CodePudding user response:

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);
        }
    }
}

CodePudding user response:

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
        )

)

CodePudding user response:

$result = $old_array[0];

This was the easiest way ...

  • Related