本文介绍了PHP:展平多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用,"爆炸,然后是:".很简单...

I need to explode by "," then ":". Easy enough...

$alttitle = "35:title1,36:title2, 59:title5"
$replacetitles = explode(",",$alttitle);
for ($i = 0 ; $i < count($replacetitles) ; $i++) {
   $replacetitle[] = explode(":", $replacetitles[$i]);
}

产生...

数组 ( [0] => 数组 ( [0] => 35 [1] => title1 ) [1] => 数组 ( [0] => 36 [1] => title2 ) [2] =>数组 ( [0] => 59 [1] => title5 ) )

Array ( [0] => Array ( [0] => 35 [1] => title1 ) [1] => Array ( [0] => 36 [1] => title2 ) [2] => Array ( [0] => 59 [1] => title5 ) )

...但是数字 35,36,59 是唯一的,所以我希望它成为数组的键?

...but the number 35,36,59 are unique so I want this to become the key for the array?

数组 ( [35] => title1 [36] => title2 [59] => title5)

Array ( [35] => title1 [36] => title2 [59] => title5 )

推荐答案

循环时简单设置:

$alttitle = "35:title1,36:title2, 59:title5"
$tmptitles = explode(",",$alttitle);

$replacetitle = array();
foreach($tmptitles as $tmptitle) {
   $tmparr = explode(":", trim($tmptitle));
   $replacetitle[intval($tmparr[0])] = trim($tmparr[1]);
}

根据上述内容,您将创建数组的迭代次数最少.

With the above, you will create your array a minimum number of iterations.

这篇关于PHP:展平多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:32