php 在php中将关联数组转换为其值的简单数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15191903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:45:00  来源:igfitidea点击:

Convert an associative array to a simple array of its values in php

phparraysassociative-array

提问by ItsGeorge

I would like to convert the array:

我想转换数组:

Array ( 
[category] => category 
[post_tag] => post_tag 
[nav_menu] => nav_menu 
[link_category] => link_category 
[post_format] => post_format 
)

to

array(category, post_tag, nav_menu, link_category, post_format)

I tried

我试过

$myarray = 'array('. implode(', ',get_taxonomies('','names')) .')';

which echos out:

呼应:

array(category, post_tag, nav_menu, link_category, post_format)

So I can do

所以我可以做

echo $myarray;
echo 'array(category, post_tag, nav_menu, link_category, post_format)';

and it prints the exact same thing.

它打印出完全相同的东西。

...but I can't use $myarrayin a function in place of the manually entered array because the function doesn't see it as array or something.

...但我不能$myarray在函数中使用来代替手动输入的数组,因为该函数不会将其视为数组或其他东西。

What am I missing here?

我在这里缺少什么?

回答by bitWorking

simply use array_valuesfunction:

只需使用array_values功能:

$array = array_values($array);

回答by Mario Naether

You should use the array_values()function.

您应该使用该array_values()功能

回答by code_10

create a new array, use a foreach loop in PHP to copy all the values from associative array into a simple array

创建一个新数组,使用 PHP 中的 foreach 循环将关联数组中的所有值复制到一个简单数组中

      $data=Array(); //associative array

      $simple_array = array(); //simple array

      foreach($data as $d)
      {
            $simple_array[]=$d['value_name'];   
      }