我正在运行一个函数并查询如下内容:
{
"Mar 22, 2015": [ ],
"Mar 23, 2015": [,
{
"id": 5,
"company_id": 1,
"task": "Do stuff right now",
"description": "Come on man! serious "
}
],
"Mar 24, 2015": [
{
"id": 8,
"company_id": 1,
"task": "Do all your taxes",
"description": "Or else you'll owe a bunch of money."
}
],
"Mar 25, 2015": [
{
"id": 2,
"company_id": 1,
"task": "Hey you",
"description": "Blah blah"
}
],
"Mar 26, 2015": [
{
"id": 1,
"company_id": 1,
"task": "Do something",
"description": "Oh ya!"
}
],
"Mar 27, 2015": [
{
"id": 4,
"company_id": 1,
"task": "Remember to do the laundry",
"description": "Oh la la!"
},
{
"id": 11,
"company_id": 1,
"task": "Buy a new snowboard",
"description": "blah blah"
},
{
"id": 12,
"company_id": 1,
"task": "Create your business plan",
"description": "blah blah"
}
],
"Mar 28, 2015": [ ]
}
我的数组总是包含7天。我的问题是,我想在我的视图中获取要输出的第一个和最后一个日期(数组键)(无需指定键值)。
当然,我想{{ $myweek[0] }}
让Mar 22, 2015
和{{ $myweek[6] }}
获得Mar 28, 2015
。但我知道我不能用多维数组做到这一点。
如何在不必在日期中进行硬编码的情况下将这些开始日期和结束日期记录到我的星期数组中?
注意, 我已经为此变量运行FOREACH
循环以显示每个日期。但是我想在foreach之外获取值...来显示日期范围。即2015年3月22日至2015年3月28日等任务
答案 0 :(得分:1)
使用array_keys:
<?php
$json = json_decode('{
"Mar 22, 2015": [ ],
"Mar 23, 2015": [
{
"id": 5,
"company_id": 1,
"task": "Do stuff right now",
"description": "Come on man! serious "
}
],
"Mar 24, 2015": [
{
"id": 8,
"company_id": 1,
"task": "Do all your taxes",
"description": "Or else you\'ll owe a bunch of money."
}
],
"Mar 25, 2015": [
{
"id": 2,
"company_id": 1,
"task": "Hey you",
"description": "Blah blah"
}
],
"Mar 26, 2015": [
{
"id": 1,
"company_id": 1,
"task": "Do something",
"description": "Oh ya!"
}
],
"Mar 27, 2015": [
{
"id": 4,
"company_id": 1,
"task": "Remember to do the laundry",
"description": "Oh la la!"
},
{
"id": 11,
"company_id": 1,
"task": "Buy a new snowboard",
"description": "blah blah"
},
{
"id": 12,
"company_id": 1,
"task": "Create your business plan",
"description": "blah blah"
}
],
"Mar 28, 2015": [ ]
}', true);
$keys = array_keys($json);
echo "From ".$keys[0]." to ".$keys[count($keys) - 1];
?>
答案 1 :(得分:0)
希望您明白以下代码:
<?php
$arr = array(
'key1' => array(1,2,3),
'key2' => array(4,5,6),
'key3' => array(7,8,9),
'key4' => array(10,1,12),
'key5' => array(13,14,15),
);
$keys = array_keys($arr);
echo $keys[0] . ' AND ' . $keys[count($keys)-1];
<强>更新强> 如果您想在视图中执行此操作,那么您有两种方法:
1)您需要将$keys
传递给您的视图并从那里获取这些密钥。
2)没有传递密钥:( $arr
是您传递给视图的变量)
{{ array_keys($arr)[0] }}
{{ array_keys($arr)[count($arr)-1] }}