uneeb meer
uneeb meer

Reputation: 692

pushing id with json data on same index of array

i recently started working on json with php, now i have a json string stored in database in a contact form table the data being retrieved from database is in the following format

Array
(
    [0] => Array
        (
            [id] => 18
            [data] => {"name":"asdsa","email":"[email protected]","phone":"1321","subject":"asdsadsa","message":"ssa"}
        )

    [1] => Array
        (
            [id] => 19
            [data] => {"name":"uneeb","email":"[email protected]","phone":"1232112","subject":"adadas","message":"dasdsa"}
        )

    [2] => Array
        (
            [id] => 20
            [data] => {"name":"uneeb","email":"[email protected]","phone":"1311","subject":"sadasd","message":"sdadas"}
        )

    [3] => Array
        (
            [id] => 21
            [data] => {"name":"uneeb","email":"[email protected]","phone":"13131231","subject":"asda","message":"asdsa"}
        )

    [4] => Array
        (
            [id] => 22
            [data] => {"name":"asdsad","email":"[email protected]","phone":"1231","subject":"asd","message":"saadsa"}
        )

)

now as you can see the id and data attributes are two different things for now to pass them into the view what i did is decoded the json string first, this is my method for handling the above scenario.

public function retrieve_contact_us(){
        $data=$this->Menu->retrieve_contact_us();
        $newarray['results']=array();

        foreach($data as $d):
            array_push($newarray['results'],json_decode($d['data'], true));
        endforeach;

        echo "<pre>";
        print_r($newarray['results']);

        //$this->load->view("admin/contactus",$newarray);
    }

the array that is returned from above is

Array
(
    [0] => Array
        (
            [name] => asdsa
            [email] => [email protected]
            [phone] => 1321
            [subject] => asdsadsa
            [message] => ssa
        )

    [1] => Array
        (
            [name] => uneeb
            [email] => [email protected]
            [phone] => 1232112
            [subject] => adadas
            [message] => dasdsa
        )

    [2] => Array
        (
            [name] => uneeb
            [email] => [email protected]
            [phone] => 1311
            [subject] => sadasd
            [message] => sdadas
        )

    [3] => Array
        (
            [name] => uneeb
            [email] => [email protected]
            [phone] => 13131231
            [subject] => asda
            [message] => asdsa
        )

    [4] => Array
        (
            [name] => asdsad
            [email] => [email protected]
            [phone] => 1231
            [subject] => asd
            [message] => saadsa
        )

)

now this is what i wanted, now i need to add a delete feature of front end, so logically i need the id of every row in my database as you can see in the topmost array but they are two different things what i actually want is the array to be like this.

 Array
        (
            [0] => Array
                (
                    [id] => 18
                    [name] => asdsa
                    [email] => [email protected]
                    [phone] => 1321
                    [subject] => asdsadsa
                    [message] => ssa
                )


    [1] => Array
        (
              [id] => 19
            [name] => uneeb
            [email] => [email protected]
            [phone] => 1232112
            [subject] => adadas
            [message] => dasdsa
        )


        )

this is waht i've tried so far!

foreach($data as $d):
            array_push($newarray['results'],$d['id']);
            array_push($newarray['results'],json_decode($d['data'], true));
        endforeach;

now what this returns is,

Array
(
    [0] => 18
    [1] => Array
        (
            [name] => asdsa
            [email] => [email protected]
            [phone] => 1321
            [subject] => asdsadsa
            [message] => ssa
        )

    [2] => 19
    [3] => Array
        (
            [name] => uneeb
            [email] => [email protected]
            [phone] => 1232112
            [subject] => adadas
            [message] => dasdsa
        )
)

as you can see it creates a totally separate index for id which i don't want ii want the corresponding id to be in the same index of array as their value is which i demonstrated above, any help?

Upvotes: 3

Views: 304

Answers (1)

jeroen
jeroen

Reputation: 91734

Personally, I would use it as the key for the contents.

In your loop you could do:

$newarray['results'][$d['id']] = json_decode($d['data'], true);

If you need it as a value, you could add it instead:

$newarray['results'][] = json_decode($d['data'], true) + ['id' => $d['id']];

Upvotes: 4

Related Questions