English 中文(简体)
在一个阵列内合并阵列, 保存数字索引
原标题:Merging arrays within an array, preserving numeric index
  • 时间:2012-05-24 17:27:12
  •  标签:
  • php
  • arrays

阵列_ 合并和阵列_ merge_ recursive 没有按要求工作, 创造更多的索引, 而不是将阵列推在一起, 并保留索引编号。 输入/ 需要输出见下文 :

Array (
    [0] => array(
        [0] => "string1",
        [1] => "string2",
        [2] => "string3"
    ),
    [1] => array(
        [0] => "string4",
        [1] => "string5",
        [2] => "string6"
    ),
    [2] => array(
        [0] => "string7",
        [1] => "string8",
        [2] => "string9"
    )
)

希望产出:

Array (
    [0] => array("string1","string4","string7"),
    [1] => array("string2","string5","string8"),
    [2] => array("string3","string6","string9")

)

编辑:

也许不是最好的例子;我想取得同样的结果,但每个嵌套阵列的键数是不平等的。

<?php
$array = Array (
[0] => array(
    [0] => "string1",
    [1] => "string2",
    [2] => "string3"
    ),
[1] => array(
    [0] => "string4",
    [1] => "string5",
    [2] => "string6"
    ),
[2] => array(
    [0] => "string7",
    [1] => "string8",
    [2] => "string9"
    ),
[3] => array(
    [0] => "string10",
    [1] => "string11",
    [2] => "string12"
    )
);

$output=array();
    for($0=0;$j<count($array[0]);$j++){
        $output[$j] = array();
    }

for($i=0;$i<count($array);$i++){
    for($0=0;$j<count($array[0]);$j++){
        $output[$j] = array_push($output[$j],$column_values[$i][$j]);
    }
}


?>

但当我这样做时,我得到我出价数数组中正确的键数, 但它们都包含一个布尔( false) 。 有什么帮助吗?

这是所期望的输出 :

Array (
    [0] => array("string1","string4","string7","string10"),
    [1] => array("string2","string5","string8","string11"),
    [2] => array("string3","string6","string9","string12")
)
最佳回答

这是为这个阵列结构做一个阵列, 这样您就可以根据您的需要修改代码...

<pre>
<?php
    $array = array(array("string1","string2","string3"),array("string4","string5","string6"),array("string7","string8","string9"));
    $output=array();
    for($i=0;$i<count($array[0]);$i++){
        for($j=0;$j<count($array[0]);$j++){
            $output[$i][$j] = $array[$j][$i];
        }
    }

    print_r($output);
?>
</pre>
问题回答

这是针对你的第二个情况:

<?php

    $array = array(array("string1","string2","string3"),array("string4","string5","string6"),array("string7","string8","string9"),array("string10","string11","string12"));
    $output=array();

    for($i=0;$i<count($array[0]);$i++){
        for($j=0;$j<=count($array[0]);$j++){
            $output[$i][$j] = $array[$j][$i];
        }
    }
    print_r($output);
?>

获取它。 上次循环应该简单 < code> array_ push ($output[$j],$clunn_ values[$i][$j]); ,而不是试图设置变量 $output[$j] =trap_push(Yadda,yadda)





相关问题
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 ...

热门标签