English 中文(简体)
将联系阵列变成正常阵列
原标题:Converting associative arrays into normal ones

我有这样的联系阵列:

array
(
    device_1  => array( a , b , c , d ),
    device_2  => array( x , y , z )
)

我怎么能够把阵列变成这样的标准阵列:

array(0 =>  a , 1 =>  b , 2 =>  c , 3 =>  d , 4 =>  x , 5 =>  y , 6 =>  z )

更简单地说:

array( a , b , c , d , e , x , y , z )

是否有任何人知道我应该做些什么?

最佳回答

你可以这样做:

$result = call_user_func_array( array_merge , $array);

您:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => x
    [5] => y
    [6] => z
)

Demo

问题回答

有了功能阵列——你可以合并阵列。

Example from: http://www.php.net/manual/en/function.array-merge.php

<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>

产出:

Array
(
    [color] => green
    [0] => 2
    [1] => 4
    [2] => a
    [3] => b
    [shape] => trapezoid
    [4] => 4
)

利用联系阵列:

$devices = array
(
    device_1  => array( a , b , c , d ),
    device_2  => array( x , y , z )
);

$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($devices));
foreach( $iterator as $value ) {
    $output[] = $value;
}

print_r($output); 

for more information you can read the RecursiveIteratorIterator class





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签