简体   繁体   English

PHP从数组键中删除方括号

[英]PHP Remove Square Brackets from Array Key

Answer: 回答:

So my issue turned out to be with the $eventValue and not the key. 因此,我的问题竟然是$ eventValue而不是键。 I was assuming the key was wrapped in brackets when it was added to the $days array but that was not the case. 我假设将密钥添加到$ days数组中时将其括在方括号中,但事实并非如此。 var_export showed me what really gets added to the array. var_export向我展示了真正添加到数组的内容。

To solve the problem, I removed the enclosing quotes from the $eventValue as well as the trailing , comma. 为了解决该问题,我从$ eventValue以及结尾的逗号中删除了引号。

Below is the original question: 以下是原始问题:

Apologies as I do not know a whole lot of PHP, but I am trying to achieve something that I hope someone here can help with. 道歉,因为我不了解很多PHP,但是我正在努力实现一些希望我能在这里为您提供帮助的东西。

I have a loop that is finding specific days of a month to highlight on a calendar. 我有一个循环,该循环查找一个月中的特定日期以在日历上突出显示。 In order for the calendar to highlight a specific day and wrap it in a class as well as a link tag, I need the array to be in this format: 为了使日历突出显示特定的一天并将其包装在一个类以及一个链接标记中,我需要使用以下格式的数组:

$days = array(

    2 => array('/weblog/archive/2004/Jan/02', 'linked-day'),
    3 => array('/weblog/archive/2004/Jan/03', 'linked-day'),
    8 => array('/weblog/archive/2004/Jan/08', 'linked-day'),
    22 => array('/weblog/archive/2004/Jan/22', 'linked-day'),

);

Within my loop, I have these two variables: 在我的循环中,我有以下两个变量:

$eventDay = substr("$str", -2, 2);
$eventValue = "array('http://example.com', 'linked-day'),";

and at the end of my loop I have this: 在循环的结尾我有这个:

$days[$eventDay] = $eventValue;

The problem is, when I print_r ($days), my $eventDay key is wrapped in [] square brackets and so the day cannot be found by the calendar. 问题是,当我print_r($ days)时,我的$ eventDay密钥被包装在[]方括号中,因此日历无法找到日期。 I need to find a way to prevent it from being wrapped in brackets. 我需要找到一种方法来防止将其包裹在括号中。

Perhaps I am approaching this all wrong. 也许我正在解决所有这些错误。 If someone has some suggestions I would really appreciate it. 如果有人提出建议,我将非常感激。

I am using Keith Devens' PHP Calendar script to do this: 我正在使用Keith Devens的PHP日历脚本来执行此操作:

http://keithdevens.com/software/php_calendar http://keithdevens.com/software/php_calendar

Thanks! 谢谢!

Full code: 完整代码:

http://pastie.org/5503664 http://pastie.org/5503664

Replace 更换

$eventValue = "array('http://example.com', 'linked-day'),";

with

$eventValue = array('http://example.com', 'linked-day');

in line 44 of the pastie 在馅饼的第44行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM