为什么JSON Encode返回空数组?

时间:2018-10-09 04:43:56

标签: javascript php arrays json

我有一个PHP数组,我想用Javascript访问它。

我的PHP数组:

array(4) { 
[1]=> array(2) { ["le_value"]=> int(3) 
["symptoms"]=> array(6) { [0]=> int(18) [1]=> int(19) 
[2]=> int(20) [3]=> int(21) [4]=> int(22) [5]=> int(23) } } [2]=> array(2) { ["le_value"]=> int(5) 
["symptoms"]=> array(0) { } } 
[3]=> array(2) { ["le_value"]=> int(5) ["symptoms"]=> array(0) { } }   [4]=> array(2) { ["le_value"]=> int(5) ["symptoms"]=> array(4) { [0]=> int(18) [1]=> int(19) [2]=> int(20) [3]=> int(21) } } } 

我用javascript编码了上述数组,但在控制台中返回了空值。

enter image description here

{% block page_js %}
<script type="text/javascript">
    $(document).ready(function() {
        EvaluationQuiz();
    });
    var slideIndex = 1;
    showSlides(slideIndex);

    function plusSlides(n) {
      showSlides(slideIndex += n);
    }

    function currentSlide(n) {
      showSlides(slideIndex = n);
    }

    function showSlides(n) {
      var i;
      var slides = document.getElementsByClassName("mySlides");
      var dots = document.getElementsByClassName("dot");
      if (n > slides.length) {slideIndex = 1}
      if (n < 1) {slideIndex = slides.length}
      for (i = 0; i < slides.length; i++) {
          slides[i].style.display = "none";
      }
      for (i = 0; i < dots.length; i++) {
          dots[i].className = dots[i].className.replace(" active", "");
      }
      slides[slideIndex-1].style.display = "inline";
      dots[slideIndex-1].className += " active";
    }

    var checkBoxPageHeader = "Check off the ones that apply to you";
    var defaultPageHeader = "Body Evaluation";
    var ratingOptions = <?php echo json_encode($ratingJson); ?>;
    console.log(ratingOptions);
    $('.rating-radio a').click(function() {
      $(this).parent().find('a').removeClass('CheckOn');
      $(this).addClass('CheckOn');
      $(this).parent(".rating-radio").find('a span').removeClass('active');
      $(this).find('span').addClass('active');
      $(this).find('input[type="radio"]').attr('checked', 'checked');
        // Data Question ID is id of Ratin Question
        console.log($(this).attr('data-question-id'));

        console.log(ratingOptions[$(this).attr('data-question-id')].le_value);
      if (ratingOptions[$(this).attr('data-question-id')] != undefined) {
        // Radio Value is the clicked radio value
        var radio_value = $(this).find('input[type="radio"]').val();
        var min_value = ratingOptions[$(this).attr('data-question-id')].le_value;
        selSymptoms = ratingOptions[$(this).attr('data-question-id')].symptoms;
        if (selSymptoms.length > 0) {
          $.each(selSymptoms, function(key, symptomId) {
            if (radio_value <= min_value) {
              $('.check-list li label input[id="symptom_' + symptomId + '"]').parent().click();
            } else {
              $('.check-list li label input[id="symptom_' + symptomId + '"]').parent().click();
            }
          });
        }
      }

    });

    $('.nextcat').click(function() {
        saveEvaluationQuizClientDetails();
    });

    // Validate User/Client Data When Next Button is clicked (Up or DOWN)
    $('#client_detail_down, #client_detail_up').click(function(e) {
      saveEvaluationQuizClientDetails();
      e.preventDefault();
      $.ajax({
        url: '<?php echo $this->CxHelper->Route('eb-front-validate-evaluation-quiz-client-details'); ?>',
        type: 'POST',
        data: $('#signup-form').serialize(),
        dataType: 'JSON',
        success: function(data) {
            if (data.status == 'success') {
                console.log(data.status);
            }
        },
        error: function(data){
            $('.error-box').html(data.responseText);
        }
      });
    });


    function saveEvaluationQuizClientDetails() {
      //$("div#eval_ajax_loader").addClass('show');
      $.ajax({
        url: '<?php echo $this->CxHelper->Route('eb-front-save-evaluation-quiz-client-details'); ?>',
        type: 'POST',
        data: $('#signup-form').serialize(),
        dataType: 'JSON',
        success: function(data) {
            console.log('saved');
        },
        error: function(data){
            console.log('Not saved');
        }
      });
    }
</script>

{% endblock %}

PHP代码:

<?php foreach ($ratingQuestions as $rkey => $ratingQuestion): ?>
<?php
    if ($ratingQuestion['apply_symptom_min'] > 0) {
        $ratingJson[$ratingQuestion['id']]['le_value'] = $ratingQuestion['apply_symptom_min'];
        $ratingJson[$ratingQuestion['id']]['symptoms'] = $ratingQuestion['symptoms'];
var_dump($ratingJson);

    }
?>
<? endforeach;?>

任何帮助将不胜感激。非常感谢您的提前帮助。让我知道是否需要更多详细信息。 json_encode函数返回一个空数组。

再次感谢。

1 个答案:

答案 0 :(得分:0)

我看到你在那里做什么:

您期望:

<script type="text/javascript">
   ...
   var ratingOptions = <?php echo json_encode($ratingJson); ?>;
   ...
</script>

在执行JavaScript时变成ratingOptions的有效jsArray?

好吧,尽管这通常应该适用于香草html / php / js-我假设您使用的模板引擎将模板“切碎”成几部分-包括您的javascript代码,其中$ratingJson是不是有效的上下文。