用json_decode解析多维数组

时间:2016-01-25 19:00:04

标签: php json

我试图在我的网站上显示本月的最高选民,但我遇到了这个阵列的问题:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    { 
     [application registerUserNotificationSettings:
       [UIUserNotificationSettings settingsForTypes:
        (UIUserNotificationTypeSound |
         UIUserNotificationTypeAlert |
         UIUserNotificationTypeBadge) categories:nil]];
       [application registerForRemoteNotifications];
    }
    else
    {
        [application registerForRemoteNotificationTypes:
         (UIUserNotificationTypeBadge |
          UIUserNotificationTypeSound |
          UIUserNotificationTypeAlert)];
    }
    NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
    NSString *messagePre = nil;
    NSString *message = nil;
    id alert = [userInfo objectForKey:@"aps"];
    if ([alert isKindOfClass:[NSString class]]) {
        messagePre = alert;
    } else if ([alert isKindOfClass:[NSDictionary class]]) {
        messagePre = [alert objectForKey:@"alert"];
    }
    NSArray *messageSplit = [messagePre componentsSeparatedByString:@"|"];
    serviceID = [messageSplit objectAtIndex:0];
    message =[messageSplit objectAtIndex:1];

    UIAlertView *alertRC = [[UIAlertView alloc]initWithTitle: @"New Road Call"
                                                     message: message
                                                    delegate: self
                                           cancelButtonTitle:nil
                                           otherButtonTitles:@"View",nil];

     [alertRC show];

    //Accept push notification when app is not open
    if (userInfo) {
        [self iGotIt];
    }

    return YES;
}

这就是我所拥有的,但它不起作用:

{
   "name":"Server name",
   "address":"***********",
   "port":"****",
   "month":"201601",
   "voters":[
      {
         "nickname":"John",
         "votes":"6"
      },
      {
         "nickname":"Beth",
         "votes":"4"
      },
      {
         "nickname":"Jimmy",
         "votes":"4"
      }
   ]
}

我怎么能这样做才能显示出来?

  1. 约翰(6)
  2. Beth(4)
  3. 吉米(4)
  4. 谢谢。

    更新:哇,谢谢你们!你们都非常有帮助。很难在他们之间选择答案。你太棒了!

4 个答案:

答案 0 :(得分:1)

你能做的是:

destPpl = destPpl.Offset(0, 1) 

答案 1 :(得分:1)

你可以用这个循环来做到这一点:

foreach ($results->voters as $i => $el) {
    echo ($i+1) . ". {$el->nickname} ({$el->votes})<br/>";
}

请注意,首先需要选择选民键,因为您对顶级其他键不感兴趣。

其次,您输入的是一些对象(不是数组),因此您需要使用->运算符来访问对象属性。

答案 2 :(得分:1)

您需要解码数据结构,然后像处理任何其他PHP变量/对象/数组一样对其进行操作。

<?php
// Raw JSON input
$input = '{
   "name":"Server name",
   "address":"***********",
   "port":"****",
   "month":"201601",
   "voters":[
      {
         "nickname":"John",
         "votes":"6"
      },
      {
         "nickname":"Beth",
         "votes":"4"
      },
      {
         "nickname":"Jimmy",
         "votes":"4"
      }
   ]
}';

//Unserialize the JSON into PHP structures
$results = json_decode($input);

//$results is now an object with properties `name`, `address`, ..., `voters`, etc

// `$results->voters` is an indexed array of objects
// each of these objects have a `nickname` and `votes` property
// no need for a counter, use the index (gleaned from @trincot's answer)
foreach ($results->voters as $i => $voter) {
    // I like `printf` here bcause it keeps my template cleaner to understand
    printf(
        '%s. %s (%s)<br />',
        ($i + 1),
        $voter->nickname,
        $voter->votes
    );
}

答案 3 :(得分:1)

您可以使用此代码。

$string = '{"name":"Server         name","address":"***********","port":"****","month":"201601","voters":[{"nickname":"John","votes":"6"},{"nickname":"Beth","votes":"4"},{"nickname":"Jimmy","votes":"4"}]}';

$result = json_decode($string);

// To print HTML using DOM
echo "<ol>";
foreach($result->voters as $voter){
    echo "<li>$voter->nickname ($voter->votes)</li>";
}
echo "</ol>";

在这里工作 - &gt; https://eval.in/507711

此代码打印此HTML

<ol><li>John (6)</li><li>Beth (4)</li><li>Jimmy (4)</li></ol>

结果在这里 - &gt; https://jsfiddle.net/h6q8zy86/