0

I would like to ask for your help with the following.

I am getting similar nested json as mentioned below in PHP. I would like to store the data in MySQL db, but I am facing problem with the variable information.

My problem is that the data are not consistent. As you can see in json below:

   [6] => Array
        (
            [pflds] => Array
                (
                    [1] => Array
                        (
                            [id] => 1
                            [n] => registration_plate
                            [v] => xxxxxx
                        )

                    [2] => Array
                        (
                            [id] => 2
                            [n] => vehicle_type
                            [v] => xxxxx
                        )

                    [3] => Array
                        (
                            [id] => 3
                            [n] => brand
                            [v] => xxxx
                        )

                )

            [pfldsmax] => 0
        )

    [7] => Array
        (
            [pflds] => Array
                (
                    [1] => Array
                        (
                            [id] => 1
                            [n] => vehicle_type
                            [v] => Трактор
                        )

                    [2] => Array
                        (
                            [id] => 2
                            [n] => registration_plate
                            [v] => xxxxx
                        )

                    [3] => Array
                        (
                            [id] => 3
                            [n] => brand
                            [v] => John Deere
                        )

                )

            [pfldsmax] => 0
        )

Once the registration plate is

Array -> [6] -> [1] -> [n] registration plate

and then

Array -> [7] -> [2] -> [n] registration plate

So I cannot easily say "insert into" because the column's position varies. The field [n] has the same name at any time, the [id] can vary though.

I am not able to think of any conditions to use. But I would like to focus on the field [n] which will say in which column the value will be stored.

Any suggestions or any other approach to go?

I'm new to this field. Thx for the help.

1
  • you can use foreach loop multiple times, avoid using one inside another just increment the index part. You can also sort the data, it will be better if you can post the exact o/p that you want and the code you've tried. Commented Jan 24, 2020 at 12:59

1 Answer 1

1

You can use foreach loop a several times like:

foreach($ar as $ind1=>$set) {       // ind1 = 6 or 7
    foreach($set as $key=>$subar){  // key = 'pflds' or 'pfldsmax'
        if (is_array($subar)){      // if subar is an array
            foreach($subar as $item){
                if ($item['n'] === 'registration_plate'){
                    // code for insert into DB
                   echo $item['id'].PHP_EOL;
                }
            }
        }
    }
}

Demo

Sign up to request clarification or add additional context in comments.

Comments

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.