I've got an multi-dimensional array at the moment and want to remove the second-level of arrays and have the value of that second level as the new index value on the parent array. My current array is:

Array ( [0] => Array ( [connectee] => 1 ) [1] => Array ( [connectee] => 6 ) )

And want from that:

Array ( [0] => 1,  [1] =>  6 )

I was poking around the usort function but couldn't get it to work (where $current_connections is my array as above:

function cmp($a, $b) {
    return strcmp($a["connectee"], $b["connectee"]);
} 

$current_connections = usort($current_connections, "cmp");

The key doesn't need to be maintained (should be destroyed in the process).

有帮助吗?

解决方案

foreach ($array as &$value) {
    $value = $value['connectee'];
}

其他提示

Note: Please note that the question statement is very confusing and contradicting, but this answer is based upon your statement for expected output

Array ( [0] => 1, [1] => 6 )

You could do

<?php
$values=array();
$values[0]=array("connectee"=>1);
$values[1]=array("connectee"=>6);
foreach($values as $index=>$value)
{
$values[$index]=$value["connectee"];
}
print_r($values);
?>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top