获取数组中键的值

时间:2015-09-04 07:16:45

标签: php arrays

我有一个变量,当我用 private static ConcurrentDictionary<string, Queue<string>> dict; public static void Main() { dict = new ConcurrentDictionary<string, Queue<string>>(); } // If I do this on one thread... private static void Enqueue(string key, string value) { dict.AddOrUpdate( key, k => new Queue<string>(new[] { value }), (k, q) => { q.Enqueue(value); return q; }); } // And I do this on another thread... private static string Dequeue(string key) { string result = null; dict.AddOrUpdate( "key", k => new Queue<string>(), (k, q) => { result = q.Dequeue(); return q; }); return result; } 这样输出它时:

print_r

我明白了:

print_r($sort_order[$field->name]);

但我只需要92的值。我怎么能这样做只输出回声时的输出?例如:

Array ( [0] => Array ( [sort_order] => 92 ) ) 

应该输出简单的

echo $sort_order[$field->name];

3 个答案:

答案 0 :(得分:1)

print_r()函数用于打印有关变量的人类可读信息。

您可以同时执行print和echo以输出所需的值:

echo $sort_order[$field->name];

print $sort_order[$field->name];

希望这有帮助。

答案 1 :(得分:1)

您的$sortOrder数组实际上是一个数组数组,如:

[
    [ 'sort_order' => 92 ]
]

这就是为什么你不能像预期的那样打印它。

尝试:

echo $sort_order[0]['sort_order'];

输出:

92

答案 2 :(得分:0)

命令print_r以人类可读的方式显示变量。因此,如果您需要知道变量中的所有信息(特别是对于大型数组),那么您可以使用它。用于其他用途,例如当你只需要知道内容时(我猜是所有情况的99.999%)你应该使用你已经提到的echoprint(尽管如此,它们或多或少相同) 。

请考虑此链接以获取更多信息

http://php.net/manual/en/function.print-r.php

What's the difference between echo, print, and print_r in PHP?