获取最后一个项目URL

时间:2018-03-15 13:29:27

标签: php json

我有一个遵循JSON格式的数据,我试图只获取最后一个URL,在下面的例子中是https://lastexample.com/images/I/4162nAIaRCL.jpg。有没有一种简单的方法可以通过PHP检索这个?

{"https://example.com/images/I/4162nAIaRCL._SX425_.jpg":[425,425],
"https://example.com/images/I/4162nAIaRCL._SY355_.jpg":[355,355],
"https://example.com/images/I/4162nAIaRCL._SX466_.jpg":[466,466],
"https://example.com/images/I/4162nAIaRCL._SY450_.jpg":[450,450],
"https://lastexample.com/images/I/4162nAIaRCL.jpg":[500,500]}

2 个答案:

答案 0 :(得分:1)

试试这个:

        public async Task<IActionResult> Edit(int? id)
    {
        if (id == null)
        {
            return NotFound();
        }
        var student = await _context.Student.SingleOrDefaultAsync(m => m.ID == id);
        if (student == null)
        {
            return NotFound();
        }
        return View(student);
    }
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Edit(int id, [Bind("ID,Name,LastName,BI,SubjectName,Grade")] ViewModel student)
    {
        if (id != student.Students.ID)
        {
            return NotFound();
        }
            if (ModelState.IsValid)
        {
            try
            {
                _context.Update(student);
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!StudentExists(student.Students.ID))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }
            return RedirectToAction(nameof(Index));
        }
        return View(student);
    }

答案 1 :(得分:1)

尝试将其解码为数组(json_decode)然后获取密钥(array_keys),然后输入最后一个条目(end / max)。

<?php 
$json = '{
    "https://example.com/images/I/4162nAIaRCL._SX425_.jpg":[425,425],
    "https://example.com/images/I/4162nAIaRCL._SY355_.jpg":[355,355],
    "https://example.com/images/I/4162nAIaRCL._SX466_.jpg":[466,466],
    "https://example.com/images/I/4162nAIaRCL._SY450_.jpg":[450,450],
    "https://lastexample.com/images/I/4162nAIaRCL.jpg":[500,500]
}';


$arr = json_decode($json,true);
$arrkeys = array_keys($arr);
$lastkey = end($arrkeys);
echo $lastkey; 
?>

https://3v4l.org/MLbdt