我有一个数组,看起来像这样,
[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把它变成这样呢?
[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
)可以爆炸它,或者通过循环或其他什么方式运行它?
发布于 2011-11-30 00:40:22
假设原始数据保存在一个名为$input的变量中
// 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);但是,您的输入中有相同的键多次出现,后面的值将覆盖第一个值。因此,您可能想要这样做:
foreach ($input as $index => $item) { // Loop outer array
foreach ($item as $key => $val) { // Loop inner items
$result[$key.$index] = $val; // Create entry in $result
}
}发布于 2011-11-30 00:39:11
<?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
发布于 2011-11-30 00:41:48
数组键必须有唯一的名称,这意味着您不能有多个youtube_showreel_description键,否则您只需覆盖该值。您可以将键重命名为类似于youtube_showreel_description_NN的名称,其中NN是描述的编号,类似于您拥有url的方式。
https://stackoverflow.com/questions/8314301
复制相似问题