dpt1712
dpt1712
2010-09-29 06:57

将此关联数组转换为字符串或单个索引数组

已采纳

I need to convert this array into a single dimensional indexed array or a string. Happy to discard the first key (0, 1) and just keep the values.

$security_check_whitelist = array
  0 => 
    array
      'whitelisted_words' => string 'Happy' (length=8)
  1 => 
    array
      'whitelisted_words' => string 'Sad' (length=5)

I tried array_values(), but it returned the exact same array structure.

This works:

$array_walker = 0;
$array_size = count($security_check_whitelist);

while($array_walker <= $array_size)
{
   foreach($security_check_whitelist[$array_walker] as $security_check_whitelist_value)
    {
        $security_check[] = $security_check_whitelist_value;
    }
    $array_walker++;
}

But it returns:

Warning: Invalid argument supplied for foreach()

How can I convert the associative array without receiving the warning message? Is there a better way?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • duanjiao8871 duanjiao8871 11年前
    foreach ($security_check_whitelist as &$item) {
        $item = $item['whitelisted_words'];
    }
    

    Or simply:

    $security_check_whitelist = array_map('current', $security_check_whitelist);
    
    点赞 评论 复制链接分享
  • doudun1029 doudun1029 11年前

    So is "whitelisted_words" an array as well? If so, I think the following would work:

    $single_dim_array = array();
    foreach(array_values($security_check_whitelist) as $item) {
        foreach($item['whitelisted_words'] as $word) {
            $single_dim_array[] = $word;
        }   
    }
    

    Then the variable $single_dim_array contains all your whitelisted words.

    点赞 评论 复制链接分享
  • dongxiejie9387 dongxiejie9387 11年前

    The problem here could be that you should only walk up to N-1, so $array_walker < $array_size.

    点赞 评论 复制链接分享

相关推荐