How do you convert an array in PHP that looks like this:
如何在PHP中转换如下所示的数组:
Array (
[2] => B.eot
[3] => B.ttf
[4] => CarnevaleeFreakshow.ttf
[5] => CarnevaleeFreakshow.eot
[6] => TRASHED.ttf
[7] => sub.ttf
)
To look like this:
看起来像这样:
Array(
[B]=>array(
[0] => B.eot
[1] => B.ttf
)
[CarnevaleeFreakshow]=>array(
[0] => CarnevaleeFreakshow.ttf
[1] => CarnevaleeFreakshow.eot
)
[TRASHED]=>array(
[0] => TRASHED.ttf
)
[sub]=>array(
[0] => sub.ttf
)
)
Is there a name for doing something like this? the data is being retrieved from a
做这样的事情会有名字吗?正在从中检索数据
scandir
array.
5 个解决方案
#1
1
<?php
$data = array (
2 => 'B.eot',
3 => 'B.ttf',
4 => 'CarnevaleeFreakshow.ttf',
5 => 'CarnevaleeFreakshow.eot',
6 => 'TRASHED.ttf',
7 => 'sub.ttf'
);
$new_data = array();
foreach ( $data as $value ) {
$tmp = explode( '.', $value );
$ext = '';
if ( $tmp[1] ) $ext = '.' . $tmp[1];
$new_data[ $tmp[0] ][] = $tmp[0] . $ext;
}
print_r( $new_data );
?>
#2
1
Here is an example.
这是一个例子。
It can be written shorter, but I think this is the most instructive.
它可以写得更短,但我认为这是最有启发性的。
$ARRraw = array (
"B.eot",
"B.ttf",
"CarnevaleeFreakshow.ttf",
"CarnevaleeFreakshow.eot",
"TRASHED.ttf",
"sub.ttf"
) ;
$sorted = array();
foreach($ARRraw as $one){
$firstPoint = strpos($one,".");
// No point? then skip.
if (!($firstPoint === false)){
// Get the part before the point.
$myKey = substr($one,0,$firstPoint);
$sorted[$myKey][] = $one;
}
}
#3
0
Is there a name for doing something like this?
做这样的事情会有名字吗?
Nope. Anyway, it should be rather simple using a loop:
不。无论如何,使用循环应该相当简单:
<?php
$newArray = array( );
foreach( $originalArray as $fontfile ) {
$newArray[basename( $font )][] = $fontfile;
}
echo '<pre>' . print_r( $newArray, true );
#4
0
To what i know there is no 'simple' method of doing this. you could build a function to handle it though.
据我所知,没有“简单”的方法来做到这一点。你可以构建一个函数来处理它。
function convertArray($array) {
$newArray = array();
foreach( $array as $item ) {
$newArray[basename($item)] = $item;
}
return $newArray;
}
That should do what your looking for.
那应该是你想要的。
#5
0
Try it:
function subdiv(array $arr) {
$res = array();
foreach($arr as $val) {
$tmp = explode('.', $val);
if(!isset($res[$tmp[0]]))
$res[$tmp[0]] = array();
$res[$tmp[0]][] = $val;
} return $res;
}
use with:
$res = subdiv($array);
var_dump($res);
#1
1
<?php
$data = array (
2 => 'B.eot',
3 => 'B.ttf',
4 => 'CarnevaleeFreakshow.ttf',
5 => 'CarnevaleeFreakshow.eot',
6 => 'TRASHED.ttf',
7 => 'sub.ttf'
);
$new_data = array();
foreach ( $data as $value ) {
$tmp = explode( '.', $value );
$ext = '';
if ( $tmp[1] ) $ext = '.' . $tmp[1];
$new_data[ $tmp[0] ][] = $tmp[0] . $ext;
}
print_r( $new_data );
?>
#2
1
Here is an example.
这是一个例子。
It can be written shorter, but I think this is the most instructive.
它可以写得更短,但我认为这是最有启发性的。
$ARRraw = array (
"B.eot",
"B.ttf",
"CarnevaleeFreakshow.ttf",
"CarnevaleeFreakshow.eot",
"TRASHED.ttf",
"sub.ttf"
) ;
$sorted = array();
foreach($ARRraw as $one){
$firstPoint = strpos($one,".");
// No point? then skip.
if (!($firstPoint === false)){
// Get the part before the point.
$myKey = substr($one,0,$firstPoint);
$sorted[$myKey][] = $one;
}
}
#3
0
Is there a name for doing something like this?
做这样的事情会有名字吗?
Nope. Anyway, it should be rather simple using a loop:
不。无论如何,使用循环应该相当简单:
<?php
$newArray = array( );
foreach( $originalArray as $fontfile ) {
$newArray[basename( $font )][] = $fontfile;
}
echo '<pre>' . print_r( $newArray, true );
#4
0
To what i know there is no 'simple' method of doing this. you could build a function to handle it though.
据我所知,没有“简单”的方法来做到这一点。你可以构建一个函数来处理它。
function convertArray($array) {
$newArray = array();
foreach( $array as $item ) {
$newArray[basename($item)] = $item;
}
return $newArray;
}
That should do what your looking for.
那应该是你想要的。
#5
0
Try it:
function subdiv(array $arr) {
$res = array();
foreach($arr as $val) {
$tmp = explode('.', $val);
if(!isset($res[$tmp[0]]))
$res[$tmp[0]] = array();
$res[$tmp[0]][] = $val;
} return $res;
}
use with:
$res = subdiv($array);
var_dump($res);