查找数组1中的数组2中是否存在键

时间:2012-08-21 09:56:11

标签: php arrays

$array_1 = array(
    'name' => 'Example Name',
    'company' => 'Intel ',
    'website' => 'WWW.intel.COM',
    'contact' => 'CONTACT@intel.COM',
    'twitter' => 'HTTP://TWITTER.COM/intel',
);

$array_2 = array(
    0 => array(
        'id' => '1',
        'field' => 'name',
        'default_field_text' => 'Name Here'
    ),
    1 => array(
        'id' => '3',
        'field' => 'company',
        'default_field_text' => 'COMPENY NAME HERE'
    ),
    2 => array(
        'id' => '4',
        'field' => 'website',
        'default_field_text' => 'WWW.WEBSITECOMPENY.COM'
    ),
    3 => array(
        'id' => '5',
        'field' => 'contact',
        'default_field_text' => 'CONTACT@COMPENY.MAIL.COM'
    ),
    4 => array(
        'id' => '6',
        'field' => 'twitter',
        'default_field_text' => 'HTTP://TWITTER.COM/NAME HERE'
    ),
    5 => array(
        'id' => '8',
        'field' => 'facebook',
        'default_field_text' => 'HTTP://FACEBOOK.COM/YOURNAME'
    )
);

嗨..首先对不起我的英语...这就是我想弄清楚的, array_1 键被视为字段&表单帖子值, array_2 有字段+默认值..

所以我想尝试这样输出

Example Name
Intel
WWW.intel.COM
CONTACT@intel.COM
HTTP://TWITTER.COM/intel
HTTP://FACEBOOK.COM/YOURNAME //from array_2 value

我的代码在这里,但它不起作用..我知道我做错了

foreach($array_2 as $item){
    $text = $item['default_field_text'];
    if($array_1[$item['field']] == $item['field']) $text = $array_1[$item['field']];
    echo $text;
}

1 个答案:

答案 0 :(得分:0)

尝试使用:

if( x === y)

而不是:

if(x == y)