首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多维数组,移动到简单数组中

多维数组,移动到简单数组中
EN

Stack Overflow用户
提问于 2011-11-30 00:35:59
回答 6查看 150关注 0票数 0

我有一个数组,看起来像这样,

代码语言:javascript
复制
[0] => Array
    (
        [youtube_showreel_url_1] => youtube1.com
        [youtube_showreel_description] => youtube1.com - desc
    )

[1] => Array
    (
        [youtube_showreel_url_2] => youtube2.com
        [youtube_showreel_description] => youtub2.com - desc
    )

[2] => Array
    (
        [youtube_showreel_url_3] => youtube3.com
        [youtube_showreel_description] => youtube3.com - desc
    )

[3] => Array
    (
        [youtube_showreel_url_4] => youtube4.com
        [youtube_showreel_description] => youtube4.com - desc
    )

[4] => Array
    (
        [youtube_showreel_url_5] => youtube5.com
        [youtube_showreel_description] => youtube5.com - desc
    )

有没有可能用PHP把它变成这样呢?

代码语言:javascript
复制
[0] => Array ( 
        [youtube_showreel_url_1] => youtube1.com  
        [youtube_showreel_description] => youtube1.com - desc 
        [youtube_showreel_url_2] => youtube2.com
        [youtube_showreel_description] => youtub2.com - desc
        [youtube_showreel_url_3] => youtube3.com
        [youtube_showreel_description] => youtube3.com - desc
        [youtube_showreel_url_4] => youtube4.com
        [youtube_showreel_description] => youtube4.com - desc
        [youtube_showreel_url_5] => youtube5.com
        [youtube_showreel_description] => youtube5.com - desc
    )

可以爆炸它,或者通过循环或其他什么方式运行它?

EN

回答 6

Stack Overflow用户

发布于 2011-11-30 00:40:22

假设原始数据保存在一个名为$input的变量中

代码语言:javascript
复制
// This will hold the result
$result = array();

foreach ($input as $index => $item) { // Loop outer array
  foreach ($item as $key => $val) { // Loop inner items
    $result[$key] = $val; // Create entry in $result
  }
}

// Show the result
print_r($result);

但是,您的输入中有相同的键多次出现,后面的值将覆盖第一个值。因此,您可能想要这样做:

代码语言:javascript
复制
foreach ($input as $index => $item) { // Loop outer array
  foreach ($item as $key => $val) { // Loop inner items
    $result[$key.$index] = $val; // Create entry in $result
  }
}
票数 1
EN

Stack Overflow用户

发布于 2011-11-30 00:39:11

代码语言:javascript
复制
<?php

$aNonFlat = array(
    1,
    2,
    array(
        3,
        4,
        5,
        array(
            6,
            7
        ),
        8,
        9,
    ),
    10,
    11
);

$objTmp = (object) array('aFlat' => array());

array_walk_recursive($aNonFlat, create_function('&$v, $k, &$t', '$t->aFlat[] = $v;'), $objTmp);

var_dump($objTmp->aFlat);

/*
array(11) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(4)
  [4]=>
  int(5)
  [5]=>
  int(6)
  [6]=>
  int(7)
  [7]=>
  int(8)
  [8]=>
  int(9)
  [9]=>
  int(10)
  [10]=>
  int(11)
}
*/

?>

来源:http://ca.php.net/manual/fr/function.array-values.php#86784

票数 0
EN

Stack Overflow用户

发布于 2011-11-30 00:41:48

数组键必须有唯一的名称,这意味着您不能有多个youtube_showreel_description键,否则您只需覆盖该值。您可以将键重命名为类似于youtube_showreel_description_NN的名称,其中NN是描述的编号,类似于您拥有url的方式。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8314301

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档