如何在php数组中获取php键值

时间:2021-06-29 22:07:49

I am having an array like this..

我有一个这样的数组。

 Array
    (
        [a] => 100%
        [b] => 0%
        [c] => 0%
        [d] => 0%
    )

I want to change this as

我想把这个换成

Array
(
    [0] => a,100%
    [1] => b,0%
    [2] => c,0%
    [3] => d,0%
)

Is it possible in php?

在php中可能吗?

5 个解决方案

#1


2  

A simple foreach will do..

每个人只要用一个简单的方法就可以了。

foreach($arr as $k=>$v)
{
    $new_arr[]=$k.",".$v;
}

Demonstration

示范

#2


3  

Something like this perhaps...

这样也许……

$newArray = array_map(function($k, $v) {
    return sprintf('%s,%s', $k, $v);
}, array_keys($array), $array);

Demo - http://ideone.com/Pc0cdC

演示——http://ideone.com/Pc0cdC

#3


0  

foreach($array as $key=>$value)
{
   $newarr[]=$key.",".$value;
}

#4


0  

If you want to get keys of this array you can use array_keys() function

如果您想获得这个数组的键,可以使用array_keys()函数

#5


-1  

<?php
$array = array(0 => 100, "color" => "red");
print_r(array_keys($array));

?>

? >

Output : 

Array
(
    [0] => 0
    [1] => color
)

May be this ..

可能是这个. .

#1


2  

A simple foreach will do..

每个人只要用一个简单的方法就可以了。

foreach($arr as $k=>$v)
{
    $new_arr[]=$k.",".$v;
}

Demonstration

示范

#2


3  

Something like this perhaps...

这样也许……

$newArray = array_map(function($k, $v) {
    return sprintf('%s,%s', $k, $v);
}, array_keys($array), $array);

Demo - http://ideone.com/Pc0cdC

演示——http://ideone.com/Pc0cdC

#3


0  

foreach($array as $key=>$value)
{
   $newarr[]=$key.",".$value;
}

#4


0  

If you want to get keys of this array you can use array_keys() function

如果您想获得这个数组的键,可以使用array_keys()函数

#5


-1  

<?php
$array = array(0 => 100, "color" => "red");
print_r(array_keys($array));

?>

? >

Output : 

Array
(
    [0] => 0
    [1] => color
)

May be this ..

可能是这个. .