【问题标题】:Apending session values to existing array structure for API request php将会话值附加到 API 请求 php 的现有数组结构中
【发布时间】:2017-12-15 15:38:52
【问题描述】:

我正在使用一个 API,其中对 API 的每个请求都应该重新发送所有以前的值。我将所有以前的值存储在 php 会话中。生成的数组结构将被 json_encoded 并用于 Curl 请求。除了在现有问题上附加新问题外,一切正常。以下是代码:

$question_counter = ++$_SESSION['question_counter'];
$questionidnew = strip_tags($_POST['questionid1']);
$answernew = strip_tags($_POST['answer1']);
//store new questions as session array
$_SESSION['questions'][$question_counter] = array(
    'questionid' => $questionidnew,
    'questionanswer' => $answernew,
    );
$question_array[] = $_SESSION['questions'];//previous questions

print_r($question_array); 给出以下结果:

Array ( [0] => Array ( 
[1] => Array ( [questionid] => p_48 [questionanswer] => absent ) 
[2] => Array ( [questionid] => p_122 [questionanswer] => absent ) 
) )

如何将其附加到现有数据结构的格式:

$to_json = [
  'sex' => $gender,
  'age' => $age,
  'evidence' => [
    ['id' => $test_id1, 'choice_id' => 'present', 'initial' => true],
    ['id' => $test_id2, 'choice_id' => 'present', 'initial' => true],
    ['id' => $questionidnew, 'choice_id' => $answernew],
    ['id' => 'p_12', 'choice_id' => $choice, 'initial' => true],
    ['id' => $location, 'choice_id' => 'present', 'initial' => true]
  ],
  'extras' => [
    'disable_users' => true
  ],
];

由于我是一名新手程序员,我遇到了一些错误,并且发现为会话中的先前问题添加以下内容有点令人困惑:

['id' => $questionidnew, 'choice_id' => $answernew], 

请求帮助。

【问题讨论】:

  • 我试过 array_push($question_array,$to_json = [第一个问题和答案被覆盖..
  • 也试过:foreach($question_array as $key => $value) { $to_json['evidence'][] = ['id' => $key, 'choice_id' => $value ]; }
  • 没有解决办法??

标签: php arrays session


【解决方案1】:

如果我没看错,您想将所有新答案添加到 $to_json 数组,对吗?我最终做了以下事情来实现这一点。我冒昧,可能有误解,所以如果这没有帮助,请纠正我:

<?php
session_start();
if(!isset($_SESSION['question_counter'])) {
  $_SESSION['question_counter'] = 0;
}

$gender = "male";
$age = 46;
$test_id1 = 12;
$test_id2 = 24;
$questionidnew = 9;
$answernew = "B";
$choice = "A";
$location = "campus";

$to_json = array(
  'sex' => $gender,
  'age' => $age,
  'evidence' => array(
    array('id' => $test_id1, 'choice_id' => 'present', 'initial' => true),
    array('id' => $test_id2, 'choice_id' => 'present', 'initial' => true),
    array('id' => $questionidnew, 'choice_id' => $answernew),
    array('id' => 'p_12', 'choice_id' => $choice, 'initial' => true),
    array('id' => $location, 'choice_id' => 'present', 'initial' => true)
  ),
  'extras' => array(
    'disable_users' => true
  ),
);

$question_counter = ++$_SESSION['question_counter'];
//$questionidnew = strip_tags($_POST['questionid1']);
//$answernew = strip_tags($_POST['answer1']);
//store new questions as session array
$_SESSION['questions'][$question_counter] = array(
    'questionid' => $questionidnew,
    'questionanswer' => $answernew,
    );
$question_array[] = $_SESSION['questions'];//previous questions

echo "<pre>";
print_r($to_json);
echo "</pre>";
//$_SESSION['names'][] = $name;

foreach($question_array as $question) {
  foreach($question as $key => $value) {
    $to_json['evidence'][] = array('id' => $value['questionid'], 'choice_id' => $value['questionanswer']);
  }
}
echo "<pre>";
print_r($to_json);
echo "</pre>";
print_r(json_encode($to_json));
?>

【讨论】:

  • 非常感谢亲爱的。你拯救了我的一天。一切都很完美,只是我在 $to_json = array( .. 之前将新问题存储为会话数组,而不是之后。再次感谢您为帮助我付出了这么多痛苦。
  • 好极了,我很高兴听到你想通了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-22
  • 1970-01-01
  • 1970-01-01
  • 2021-08-03
  • 2022-10-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多