Add to array from string

I have an array called $ data that needs to be updated with data from an ajax call.

There are two variables sent via an ajax call (with examples of inputs):

sectionDetails :

[111][0][2][0][service_providers][]

serviceProvider :

Google

ServiceProvider is the data, and sectionDetails is the array the service provider should be in, in the $ data array.

I need the $ data array to end as:

$data  =   Array
(
[111] => Array
    (
        [0] => Array
            (
                [2] => Array
                    (
                        [0] => Array
                            (
                                [service_providers] => Array 
                                                    (
                                                        [0] = Google
                                                    )
                            )

                    )

            )

    )

)

This way, I can dynamically enter data into any cell, and then I can update certain arrays (for example, $data[111][0][2][0][service_providers][0]= "Yahoo";

$_POST['sectionDetails'] is, however, the line in which the problem is.

, $data ( )?

, .

+4
2

​​, :

function setToPath(&$data, $path, $value){
    //$temp will take us deeper into the nested path
    $temp = &$data;

    //notice this preg_split logic is specific to your path syntax
    $exploded = preg_split("/\]\[/", rtrim(ltrim($path,"["),"]")); 

    // Where $path = '[111][0][2][0][service_providers][]';
    // $exploded = 
    //    Array
    //    (
    //        [0] => 111
    //        [1] => 0
    //        [2] => 2
    //        [3] => 0
    //        [4] => service_providers
    //        [5] => 
    //    )
    foreach($exploded as $key) {
        if ($key != null) {
            $temp = &$temp[$key];
        } else if(!is_array($temp)) {
            //if there no key, i.e. '[]' create a new array
            $temp = array();
        }
    }
    //if the last index was '[]', this means push into the array
    if($key == null) {
        array_push($temp,$value);
    } else {
        $temp = $value;
    }
    unset($temp);
}

:

setToPath($data, $_POST['sectionDetails'], $_POST['serviceProvider']);

print_r($data) :

Array
(
    [111] => Array
        (
            [0] => Array
                (
                    [2] => Array
                        (
                            [0] => Array
                                (
                                    [service_providers] => Array
                                        (
                                            [0] => Google
                                        )

                                )

                        )

                )

        )

)
+3

, eval:

eval("\$data$sectionDetails = '$serviceProvider';");

print_r($data) :

Array
(
    [111] => Array
        (
            [0] => Array
                (
                    [2] => Array
                        (
                            [0] => Array
                                (
                                    [service_providers] => Array
                                        (
                                            [0] => Google
                                        )

                                )

                        )

                )

        )

)

eval() , PHP-. . , , , , - , , .

+1

Source: https://habr.com/ru/post/1609826/


All Articles