如何在PHP中循环遍历JSON字符串

时间:2019-05-07 08:54:52

标签: php json

此json来自ajax,同时调用php文件

 "{"4":{"sp_id":"4","sp_name":"RENU RAWAT"},"7":{"sp_id":"7","sp_name":"RENU RAWAT"},"10":{"sp_id":"10","sp_name":"RENU RAWAT"}}"

如何在php中循环通过,这里的键4,7,10是动态的

我保存了这是$ login并尝试了此代码

if (!empty($_POST)) {
    $jsona = $_POST;
} else if (!empty($_REQUEST)) {
    $jsona = $_REQUEST;
} else {
    $string = file_get_contents("php://input");
    $jsona = json_decode($string, true);
}
foreach ($jsona as $key => $value) {
    if (!is_array($value)) {
        $value = trim($value);
        $value = stripslashes($value);
        $jsona[$key] = $value;
//$_POST[$key]=mysql_real_escape_string($value);
    }
}
$login = $jsona['login'];

<?php foreach($login as $sp_id=>$subject_teacher){ ?>
                    <a href="message.html?sp_id=<?php echo $subject_teacher['sp_id']; ?>" class="item-link">
                        <li>
                            <div class="item-content">
                                <div class="item-media">
                                    <img src="<?php echo (!empty($subject_teacher['sp_photo']))?'data:image/jpeg;base64,' . base64_encode($subject_teacher['sp_photo']):"images/psad/blank_photo.jpg";?>" width="40" alt="Photo">
                                </div>
                                <div class="item-inner">
                                    <div class="item-title"><?php echo $subject_teacher['sp_name'];?></div>
                                </div>
                            </div>
                        </li>
                    </a>
                <?php } ?>

任何答案,如果没有重复,请删除该标签

0 个答案:

没有答案