使用PHP使用动态线解码json?

时间:2015-10-26 06:46:24

标签: php json object wikipedia wikipedia-api

这是我在json_decode之后的代码:

static func makeGridBackground(dirtyRect: NSRect, view: NSView){
    //view.print("WMEditorUtils: initiated drawing")

    //Fill background with white color
    if let context = NSGraphicsContext.currentContext()?.CGContext {
        NSColor.whiteColor().setFill()
        CGContextFillRect(context, dirtyRect)
        CGContextFlush(context)
    }

    //Draw Lines: Horizontal
    for var i:Int = 1; i < (Int)(view.bounds.size.height / 10); i++ {
        if (i % 10 == 0) {
            NSColor(deviceRed: 100.0/255.0, green: 149.0/255.0, blue: 237.0/255.0, alpha: 0.3).set()
        }
        else if (i % 5 == 0) {
            NSColor(deviceRed: 100.0/255.0, green: 149.0/255.0, blue: 237.0/255.0, alpha: 0.2).set()
        }
        else{
            NSColor(deviceRed: 100.0/255.0, green: 149.0/255.0, blue: 237.0/255.0, alpha: 0.1).set()
        }
        NSBezierPath.strokeLineFromPoint(NSMakePoint(0, (CGFloat)(i * 10) - 0.5), toPoint: NSMakePoint(view.bounds.size.width, (CGFloat)(i * 10) - 0.5))
    }

    //Draw Lines: Vertical
    for var i:Int = 1; i < (Int)(view.bounds.size.width / 10); i++ {
        if (i % 10 == 0) {
            NSColor(deviceRed: 100.0/255.0, green: 149.0/255.0, blue: 237.0/255.0, alpha: 0.3).set()
        }
        else if (i % 5 == 0) {
            NSColor(deviceRed: 100.0/255.0, green: 149.0/255.0, blue: 237.0/255.0, alpha: 0.2).set()
        }
        else{
            NSColor(deviceRed: 100.0/255.0, green: 149.0/255.0, blue: 237.0/255.0, alpha: 0.1).set()
        }
        NSBezierPath.strokeLineFromPoint(NSMakePoint((CGFloat)(i * 10) - 0.5, 0), toPoint: NSMakePoint((CGFloat)(i * 10) - 0.5, view.bounds.size.width))
    }

}

如何使用PHP获取值stdClass Object ( [batchcomplete] => [query] => stdClass Object ( [pages] => stdClass Object ( [56667] => stdClass Object ( [pageid] => 56667 [ns] => 0 [title] => Hanoi [contentmodel] => wikitext [pagelanguage] => en [touched] => 2015-10-25T20:13:21Z [lastrevid] => 687471695 [length] => 53648 [fullurl] => https://en.wikipedia.org/wiki/Hanoi [editurl] => https://en.wikipedia.org/w/index.php?title=Hanoi&action=edit [canonicalurl] => https://en.wikipedia.org/wiki/Hanoi ) ) ) ) [title][fullurl]?我现在不知道如何通过第[pageid]行,因为[56667] => stdClass Object (是动态的(取决于请求)。

1 个答案:

答案 0 :(得分:2)

您可以使用reset()获取第一个数组值。这不需要您知道密钥。

试试这个:

$output = json_decode($output, true); // convert to array so we can use reset.

$output_details = reset($output['query']['pages']);

$output_details['title']; // title
$output_details['fullurl']; // fullurl
$output_details['pageid']; // pageid