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
我最近开始使用php在json上工作,现在我有一个json字符串存储在联系表单表的数据库中,从数据库中检索的数据采用以下格式
Array
(
[0] => Array
(
[id] => 18
[data] => {"name":"asdsa","email":"uneebmir321@yahoo.com","phone":"1321","subject":"asdsadsa","message":"ssa"}
)
[1] => Array
(
[id] => 19
[data] => {"name":"uneeb","email":"uneeb@nextcrawl.com","phone":"1232112","subject":"adadas","message":"dasdsa"}
)
[2] => Array
(
[id] => 20
[data] => {"name":"uneeb","email":"uneeb@nextcrawl.com","phone":"1311","subject":"sadasd","message":"sdadas"}
)
[3] => Array
(
[id] => 21
[data] => {"name":"uneeb","email":"uneebmir321@yahoo.com","phone":"13131231","subject":"asda","message":"asdsa"}
)
[4] => Array
(
[id] => 22
[data] => {"name":"asdsad","email":"sads@asda.com","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.
现在您可以看到id和data属性是两个不同的东西,现在将它们传递到视图我所做的是首先解码json字符串,这是我处理上述场景的方法。
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] => uneebmir321@yahoo.com
[phone] => 1321
[subject] => asdsadsa
[message] => ssa
)
[1] => Array
(
[name] => uneeb
[email] => uneeb@nextcrawl.com
[phone] => 1232112
[subject] => adadas
[message] => dasdsa
)
[2] => Array
(
[name] => uneeb
[email] => uneeb@nextcrawl.com
[phone] => 1311
[subject] => sadasd
[message] => sdadas
)
[3] => Array
(
[name] => uneeb
[email] => uneebmir321@yahoo.com
[phone] => 13131231
[subject] => asda
[message] => asdsa
)
[4] => Array
(
[name] => asdsad
[email] => sads@asda.com
[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.
现在这就是我想要的,现在我需要添加前端的删除功能,所以逻辑上我需要数据库中每一行的id,你可以在最顶层的数组中看到,但它们是两个不同的东西,我真正想要的是数组就像这样。
Array
(
[0] => Array
(
[id] => 18
[name] => asdsa
[email] => uneebmir321@yahoo.com
[phone] => 1321
[subject] => asdsadsa
[message] => ssa
)
[1] => Array
(
[id] => 19
[name] => uneeb
[email] => uneeb@nextcrawl.com
[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] => uneebmir321@yahoo.com
[phone] => 1321
[subject] => asdsadsa
[message] => ssa
)
[2] => 19
[3] => Array
(
[name] => uneeb
[email] => uneeb@nextcrawl.com
[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?
正如你可以看到它为id创建一个完全独立的索引,我不希望我希望相应的id在数组的相同索引中,因为它的值是我在上面演示的,任何帮助?
1 个解决方案
#1
4
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']];
#1
4
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']];