Foreach循环使用php获取记录中数组的最后一项?

时间:2015-02-04 11:42:08

标签: php mysql json ip record

我想从单一记录中获取ip,该记录以这种格式保存:

a:10:{i:0;a:6:  {s:2:"ip";s:12:"92.74.183.58";s:7:"country";N;s:7:"browser";s:0:"";s:2:"os";s:0:  "";s:8:"referrer";s:16:"www.mysite.com";s:4:"time";i:1384112675;}i:1;a:6:  {s:2:"ip";s:14:"94.218.210.161";s:7:"country";N;s:7:"browser";s:0:"";s:2:"os";s:  0:"";s:8:"referrer";s:12:"mysite.com";s:4:"time";i:1381001719;}i:2;a:6:  {s:2:"ip";s:14:"109.193.141.97";s:7:"country";N;s:7:"browser";s:0:"";s:2:"os";s:  0:"";s:8:"referrer";s:16:"www.mysite.com";s:4:"time";i:1373617006;}i:3;a:6:{s:2:"ip";s:13:"77.29.163.221";s:7:"country";N;s:7:"browser";s:0:"";s:2:"os";s:0:"";s:8:"referrer";s:16:"www.mysite.com";s:4:"time";i:1372942359;}i:4;a:6:{s:2:"ip";s:13:"77.29.163.221";s:7:"country";N;s:7:"browser";s:0:"";s:2:"os";s:0:"";s:8:"referrer";s:16:"www.mysite.com";s:4:"time";i:1372942159;}i:5;a:6:{s:2:"ip";s:12:"77.29.163.83";s:7:"country";N;s:7:"browser";s:0:"";s:2:"os";s:0:"";s:8:"referrer";s:16:"www.gigaodds.com";s:4:"time";i:1372889274;}i:6;a:6:{s:2:"ip";s:14:"46.217.104.220";s:7:"country";N;s:7:"browser";s:0:"";s:2:"os";s:0:"";s:8:"referrer";s:12:"mysite.com";s:4:"time";i:1372438492;}i:7;a:6:{s:2:"ip";s:14:"46.217.104.220";s:7:"country";N;s:7:"browser";s:0:"";s:2:"os";s:0:"";s:8:"referrer";s:16:"www.mysite.com";s:4:"time";i:1372438135;}i:8;a:6:{s:2:"ip";s:14:"46.217.207.213";s:7:"country";N;s:7:"browser";s:0:"";s:2:"os";s:0:"";s:8:"referrer";s:16:"www.mysite.com";s:4:"time";i:1372368077;}i:9;a:6:{s:2:"ip";s:14:"109.192.181.68";s:7:"country";N;s:7:"browser";s:0:"";s:2:"os";s:0:"";s:8:"referrer";s:16:"www.mysite.com";s:4:"time";i:1370461381;}}

我试图只获取最后一个IP,而不是保存在此记录中的所有IP。

$user_login_info = unserialize($user_info['u_login_info']);
  foreach ($user_login_info as $info) {
    $ip = $info['ip'];
    echo $ip;
    echo "<br />";
 }

2 个答案:

答案 0 :(得分:1)

$user_login_info = unserialize($user_info['u_login_info']);

$lastIp = end($user_login_info);

答案 1 :(得分:0)

我找到了解决方案。 这是方法:

  $user_login_info = unserialize($user_info['u_login_info']);
  $lastIp = current($user_login_info);
  echo $lastIp['ip'];