gpt4 book ai didi

php - 在 foreach 循环中使用 PHP 解析数组值

转载 作者:行者123 更新时间:2023-12-02 21:11:12 27 4
gpt4 key购买 nike

我想用 PHP 的 foreach 循环解析一个数组,以获取“ques”数组内的对象名称和值。我想要 enter image description here

[
{
"ques": [
{
"name": "comment",
"value": "comment me for the reason",
"sur_id": "1",
"user_id": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="18797c757176587f75797174367b7775" rel="noreferrer noopener nofollow">[email protected]</a>",
"pagename": "question_response"
},
{
"name": "check-box[]",
"value": "1"
},
{
"name": "radio",
"value": "radio 2"
},
{
"name": "yes",
"value": "no"
}

]
"ques":[
{
"name": "date",
"value": "2015-10-23"
"user_id": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2b4a4f4642451a6b4c464a424705484446" rel="noreferrer noopener nofollow">[email protected]</a>",
},
{
"name": "select-deopdown",
"value": ""
},
{
"name": "true",
"value": "false"
},
{
"name": "number",
"value": "55"
}
]
}
]

我想将名称、值和 user_id 从“ques”数组中分离出来:

while ($fetch = mysql_fetch_array($query1)) {
$content = $fetch['CONTENT_VALUES'];
// print_r($content);
$content_value= mb_convert_encoding($content ,"UTF-8");
$datas = json_decode($content, true);
foreach($datas->ques as $values)
{
echo $values->value . "\n";
print_r($values);
}
$test[] = array('ques' => $datas ,'answer'=>$values);
}

最佳答案

如果您想为每个值创建三个不同的数组,请创建三个空白数组,然后将相应的值存储到 foreach 中。下面给你一个常见的例子

$name = array(); 
$values= array();
$users = array();
foreach($datas->ques as $values) {
$name[] = $values->name;
$values[] = $values->value;
$users[] = !empty($values->user_id) ? $values->user_id : '';
}

您还可以根据需要进行修改。

谢谢。

关于php - 在 foreach 循环中使用 PHP 解析数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33339316/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com