将此关联数组转换为字符串或单索引数组 [英] Convert this associative array to a string or single indexed array

查看:111
本文介绍了将此关联数组转换为字符串或单索引数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将此数组转换为一维索引数组或字符串.很高兴舍弃第一个键(0、1)并只保留值.

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)

我尝试使用array_values(),但返回的数组结构完全相同.

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

这有效:

$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++;
}

但是它返回:

警告:为提供了无效的参数 foreach()

Warning: Invalid argument supplied for foreach()

如何在不收到警告消息的情况下转换关联数组?有更好的方法吗?

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

推荐答案

foreach ($security_check_whitelist as &$item) {
    $item = $item['whitelisted_words'];
}

或者简单地:

$security_check_whitelist = array_map('current', $security_check_whitelist);

这篇关于将此关联数组转换为字符串或单索引数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆