简体   繁体   English

使用字符串作为数组索引PHP

[英]Using strings as an array index PHP

I'm really stumped on this one. 我真的很为难。 I have this array variable $banner_image_description . 我有这个数组变量$banner_image_description When I print it I get Array ( ['title'] => dafha ['price'] => adhfadhf ) . 当我打印它时,我得到Array ( ['title'] => dafha ['price'] => adhfadhf )

Now, what I'm trying to do is check the length of the value at 'title' and make sure it is more than 2. So when I run : 现在,我想做的是检查“ title”值的长度,并确保它大于2。所以当我运行时:

if (isset($this->request->post['banner_image'])) 
    {
        foreach ($this->request->post['banner_image'] as $banner_image_id => $banner_image) 
        {
            foreach ($banner_image['banner_image_description'] as $language_id => $banner_image_description) 
            {   
                print_r($banner_image_description);
                echo('<br/>');
                if ((utf8_strlen($banner_image_description['title']) < 2) || (utf8_strlen($banner_image_description['title']) > 64)) 
                { 

                    $this->error['banner_image'][$banner_image_id][$language_id] = $this->language->get('error_title'); 
                }                   
            }
        }   
    }

I get Notice: Undefined index: title and I have no idea why. 我得到Notice: Undefined index: title ,我不知道为什么。 I thought this was the correct way to go about getting the value at 'title' and I know the index is there and the value is there. 我认为这是获取“ title”值的正确方法,而且我知道索引在那里而且值在那里。

I am pretty sure that the array is being populated by POSTing form values: 我很确定数组是通过POST表单值填充的:

if (isset($this->request->post['banner_image'])) 
{
    $banner_images = $this->request->post['banner_image'];
}

Here is what the form values look like: 表单值如下所示:

<input type="text" 
 name="banner_image[<?php echo $image_row; ?>][banner_image_description][<?php echo $language['language_id']; ?>]['title']" 
 value="<?php echo isset(
 $banner_image['banner_image_description'][$language['language_id']]['title']) ? 
 $banner_image['banner_image_description'][$language['language_id']]['title'] : ''; ?>" />
 <img src="view/image/flags/<?php echo $language['image']; ?>" title="<?php echo $language['name']; ?>" /><br />

That is a warning, it is poor programming but will not break anything yet. 这是一个警告,它的编程很差,但是还不会破坏任何内容。

Do this, it will make sure the key is set and has a value. 这样做,将确保密钥已设置并具有值。 Keep in mind if this is not set so the key has no value, your echo statement will not trigger. 请记住,如果未设置此项,则键没有值,则您的echo语句将不会触发。

if (isset($banner_image_description['title']) && (utf8_strlen($banner_image_description['title']) < 2)) 
{ 
    echo 'too short!';
}

var_dump()显示该键包含',因此if语句应设置为

if ((utf8_strlen($banner_image_description["'title'"]) < 2) || (utf8_strlen($banner_image_description["'title'"]) > 64))

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

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