dongxiz5342 2016-03-10 18:39 采纳率: 100%
浏览 49

PHP数组未定义的偏移量,即使代码执行

I'm trying to extract some variables from my REQUEST_URI:

liquidfinger.com/key1-value1/key2-value2/key3-value3

I want to transform these values into a php associative array. My code is as follows:

$pState = [];
$stateString = substr($_SERVER['REQUEST_URI'],1);//remove leading slash
if($stateString){
    $statePairs = explode("/",$stateString);
    foreach($statePairs as $statePair){
        $statePairArray = explode("-",$statePair);
        $pState[$statePairArray[0]] = $statePairArray[1];
    }
}

The $pState array is being created correctly and I can echo all the keys and values. However, I am getting an error_log:

Undefined offset: 1

I am even getting an error_log when there are no key-value pairs, so the IF statement shouldn't be executed, but possibly that is a characteristic of the error_log?

Okay, just to recap, the code was working but I was getting error messages. Further tests yielded the following:

url: www.liquidfinger.com
print_r($pState): Array ( )
[11-Mar-2016 10:01:02 UTC] PHP Notice:  Undefined offset: 1 in /home/adamglynsmith/public_html/index.php on line 20


url: http://www.liquidfinger.com/user-2/tab-browseAll/marker-101
print_r($pState): Array ( [user] => 2 [tab] => browseAll [marker] => 101 )
print_r($statePairArray): Array ( [0] => user [1] => 2 ) Array ( [0] => tab     [1] => browseAll ) Array ( [0] => marker [1] => 101 )
[11-Mar-2016 10:14:41 UTC] PHP Notice:  Undefined offset: 1 in /home/adamglynsmith/public_html/index.php on line 20
[11-Mar-2016 10:14:43 UTC] PHP Notice:  Undefined offset: 1 in /home/adamglynsmith/public_html/index.php on line 20

Since I have spent quite enough time on it and since I ultimately wanted to end up with a JavaScript array, I solved - or avoided - the problem by using the php to construct a string for the JavaScript like so:

    $stateString = str_replace("/","', ",$stateString);//get rid of slashes
    $stateString = str_replace("-",":'",$stateString);//get rid of dashes
    $stateString .= "'";//add final single quote
    <script>
        jState = {<?php echo $stateString; ?>};
    </script>

Thanks.

  • 写回答

1条回答 默认 最新

  • doukan1258 2016-03-10 18:51
    关注

    PHP does not guard your code implicitly, so you have to do some extra work to make sure you're actually able to do the correct thing:

    $pState = [];
    $stateString = substr($_SERVER['REQUEST_URI'],1);//remove leading slash
    if($stateString){
        $statePairs = explode("/",$stateString);
        if (count($statePairs)) >= 1 {  // Technically unnecessary unless you want to log the fact you didn't find anything.
            foreach($statePairs as $statePair){
                $statePairArray = explode("-",$statePair);
                if (count($statePairArray) == 2) {  // Found two and only two elements
                    $pState[$statePairArray[0]] = $statePairArray[1];
                } else {
                    //It is very helpful in log statements to include the values of things that produced unexpected results
                    Log.warn("Invalid key-pair found for statePairArray=" + $statePairArray);
                }
            }
        } else {
            LOG.warn("No state pairs found for stateString=" + $stateString);//Or some other appropriate log or exception
        }
    }
    

    explode may return an empty array if there is nothing to explode on. Therefore you need to check and take an appropriate action if it isn't there.

    评论

报告相同问题?

悬赏问题

  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP