php如果数组键存在于multidimentional数组中

时间:2022-11-28 21:36:40

how can i check if logo exists in this array called $attachements print_r is below:

我如何检查这个名为$ attachements print_r的数组中是否存在徽标如下:

Array ( [logo] => /home/richar2/public_html/ioagh/images/stories/jreviews/20100510115659_1_img.gif )

数组([logo] => /home/richar2/public_html/ioagh/images/stories/jreviews/20100510115659_1_img.gif)

when theres no logo, the array print_r's

当没有标识时,数组print_r's

Array ( )

数组()

i tried: if (isset($attachments['logo']) ) {..} but the conditional code runs when there is no logo

我试过:if(isset($ attachments ['logo'])){..}但条件代码在没有徽标时运行

6 个解决方案

#1


2  

Use the function array_key_exists.

使用函数array_key_exists。

http://php.net/manual/en/function.array-key-exists.php

#2


1  

It's very stange that isset() is not working, I am pretty sure it should. Maybe you have a problem elsewhere in your code.

isset()无法正常工作,我很确定应该这样做。也许你的代码中有其他问题。

Anyway, if you want to try something else, there is a specific function: array_key_exists()

无论如何,如果你想尝试别的东西,有一个特定的功能:array_key_exists()

#3


1  

This works for me as expected:

这符合我的预期:

$arr['logo'] = '/home/richar2/public_html/ioagh/images/stories/jreviews/20100510115659_1_img.gif';
print_r($arr);

if (isset($arr['logo'])){
    echo $arr['logo'];
}else{
    echo 'Key doesn\'t exist!';
}

Are you sure you set $arr['logo'] = null, not $arr['logo'] = ''? For the latter you can also check

你确定你设置$ arr ['logo'] = null,而不是$ arr ['logo'] =''?对于后者你也可以检查

if (isset($arr['logo'] && !empty($arr['logo'])){
...
}

#4


0  

but the conditional code runs when there is no logo

但条件代码在没有徽标时运行

You could construct an else clause to take appropriate action:

您可以构造一个else子句来采取适当的操作:

if (isset($attachments['logo']))
{
  // logo is set
}
else
{
  // loto is not set
}

Or simply try this:

或者只是尝试这个:

if (array_key_exists('logo', $attachments))
{
    // logo is set
}

More info on array_key_exists

有关array_key_exists的更多信息

#5


0  

You can use array_key_exists.

您可以使用array_key_exists。

#6


0  

you could write it like:

你可以这样写:

function md_array_key_exists ($key, $array)
{
    foreach ($array as $item => $val)
    {
        if ($item === $key)
        {
            return true;
        }

        if (is_array ($val))
        {
            if (true == marray_key_exists ($key, $val))
                return true;
        }
    }

    return false;
}

#1


2  

Use the function array_key_exists.

使用函数array_key_exists。

http://php.net/manual/en/function.array-key-exists.php

#2


1  

It's very stange that isset() is not working, I am pretty sure it should. Maybe you have a problem elsewhere in your code.

isset()无法正常工作,我很确定应该这样做。也许你的代码中有其他问题。

Anyway, if you want to try something else, there is a specific function: array_key_exists()

无论如何,如果你想尝试别的东西,有一个特定的功能:array_key_exists()

#3


1  

This works for me as expected:

这符合我的预期:

$arr['logo'] = '/home/richar2/public_html/ioagh/images/stories/jreviews/20100510115659_1_img.gif';
print_r($arr);

if (isset($arr['logo'])){
    echo $arr['logo'];
}else{
    echo 'Key doesn\'t exist!';
}

Are you sure you set $arr['logo'] = null, not $arr['logo'] = ''? For the latter you can also check

你确定你设置$ arr ['logo'] = null,而不是$ arr ['logo'] =''?对于后者你也可以检查

if (isset($arr['logo'] && !empty($arr['logo'])){
...
}

#4


0  

but the conditional code runs when there is no logo

但条件代码在没有徽标时运行

You could construct an else clause to take appropriate action:

您可以构造一个else子句来采取适当的操作:

if (isset($attachments['logo']))
{
  // logo is set
}
else
{
  // loto is not set
}

Or simply try this:

或者只是尝试这个:

if (array_key_exists('logo', $attachments))
{
    // logo is set
}

More info on array_key_exists

有关array_key_exists的更多信息

#5


0  

You can use array_key_exists.

您可以使用array_key_exists。

#6


0  

you could write it like:

你可以这样写:

function md_array_key_exists ($key, $array)
{
    foreach ($array as $item => $val)
    {
        if ($item === $key)
        {
            return true;
        }

        if (is_array ($val))
        {
            if (true == marray_key_exists ($key, $val))
                return true;
        }
    }

    return false;
}