2

I built a form where i have to create array of objects from kind of inputs related to each other like below:

{
 "name_node":{"type":"input","name":"name","validation":"required, alpha numeric"},
 "content":{"type":"textarea","name":"contentco","validation":"required, alpha numeric"}
}

I used Jquery to built this array then save it to an hidden input field named "inputs" in the form. so when i submit the form i could retrieve the object above like:

$this->input->post('inputs');

What i need to do is to save the previous object into the below format instead

[inputs] => Array(
                  [name_node] => Array(
                        [type] => input
                        [slug] => name_node
                        [name] => name
                        [validation] => Array
                            (
                                [0] => required
                                [1] => alpha numeric
                            )

                    )

                [content] => Array
                    (
                        [type] => textarea
                        [slug] => content
                        [name] => content
                        [validation] => Array
                            (
                                [0] => required
                                [1] => alpha numeric
                            )

                    )

            )
3
  • 1
    I guess "unserialize()" is what you're looking for. php.net/manual/de/function.unserialize.php Commented Dec 23, 2013 at 9:30
  • do you understand mechanic of models and all that? I can slightly feel that you are asking about either parsing (unserialize) data or actual "saving to database". Commented Dec 23, 2013 at 9:34
  • actually i am asking about parsing the object, I know how to save it to database Commented Dec 23, 2013 at 9:38

2 Answers 2

2

You can achieve in PHP by using json_decode() and format your array using foreach loop like below.

$str = '{
"name_node":{"type":"input","name":"name","validation":"required, alpha numeric"},
"content":{"type":"textarea","name":"contentco","validation":"required, alpha numeric"}
}';

$arr = json_decode(($str), true);

foreach ($arr as $k => $v) {
   $newArray[$k] = $v;
   $newArray[$k]['slug'] = $k;
   if(isset($v['validation'])) {
     $newArray[$k]['validation'] = explode(",",$v['validation']);
   }

}
print_r($newArray);

Working Demo

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

Comments

0
$inputs = json_decode($this->input->post('inputs'));

Now if you print_r($inputs); you will get bellow formation

 stdClass Object
    (
        [name_node] => stdClass Object
            (
                [type] => input
                [name] => name
                [validation] => required, alpha numeric
            )

        [content] => stdClass Object
            (
                [type] => textarea
                [name] => contentco
                [validation] => required, alpha numeric
            )

    )

If you want exactly structure then...

$inputs_object = json_decode($this->input->post('inputs'));

$data['inputs'] = array(
                        'name_node'=>(array)$inputs_object ->name_node,
                        'content'=>(array)$inputs_object ->content,
                    )

$data['inputs']['name_node']= explode(',',$data['inputs']['name_node']['validation']);
$data['inputs']['content']= explode(',',$data['inputs']['content']['validation'])

3 Comments

What if I have 100 key like name_code content then I need to write it for 100 key manually. :)
Hi @Roopendra I just write this code only for 1 key.. not for dynamic value.. if key is more than one then obviously use loop through the values and prepare it.. Your code is good.. When I post the answer I did'nt saw your answer otherwise I was not posting it.. :)
@SKG Thanks a lot for you cooperation

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.