如何在PHP中基于值创建数组?

时间:2015-10-27 07:36:14

标签: php arrays

我有一个像这样的数组

  Array
(
    [0] => 123_dr_for_ma_on_2352015_2nd Shift_(08-29-2015_11-31).pdf
    [1] => 123_dr_for_ma_on_2352015_2nd Shift_(08-29-2015_11-30).pdf
    [2] => 123_bms_for__on__(10-06-2015_18-36).pdf
)

我想根据它的值将其转换为多维数组 如

  Array
(
    [dr] => Array
        (
            [0] => 123_dr_for_ma_on_2352015_2nd Shift_(08-29-2015_11-31).pdf
            [1] => 123_dr_for_ma_on_2352015_2nd Shift_(08-29-2015_11-30).pdf
        )
    [bms] => Array
        (
            [0] => 123_bms_for__on__(10-06-2015_18-36).pdf
        )
)  

基于名字后的第一个下划线(bms,dr)就像.......请帮我实现这个

5 个答案:

答案 0 :(得分:18)

我会做以下事情:

$newArray = array();
foreach ($array as $key => $value) {
    $parts = explode('_', $value);
    $newArray[$parts[1]][] = $value;
}

print_r($newArray); 

答案 1 :(得分:2)

要在没有循环的情况下解决这个问题,使用函数和内置的Array Filter函数会很聪明。

这是另一种可能的解决方案 - 我认为这似乎有点凉爽;

<?php

// ####################
// GLOBAL VARIABLES DEFINITION    
// ####################
// Store Pattern Here = Used to Define Array at the end
global $Array_IDs; 
$Array_IDs = array();
// Store Variables With Header Pattern (ID) in this area
global $Array_Values; 
$Array_Values = array();

// ####################
// FUNCTION DEFINITION
// ####################
// FUNCTION TO SPLIT ARRAY
function Get_ID($variable){
    // ACCESS GLOBALS
    global $Array_IDs; 
    global $Array_Values; 

    // GET CURRENT VARIBALE - EXPLODE TO EXTRACT PATTERN AS ID
    $Temp_Parts = explode("_", $variable);

    // CHECK IF IDENTIFIER IS ALREADY FOUND (STORED IN ARRAY_ID)
    if (in_array($Temp_Parts[1], $Array_IDs)){
        // ID ALREADY HAS SUB-ARRAY CREATED
        // JUST APPEND VARIABLE
        $Array_Values[$Temp_Parts[1]][] = $variable;
    }else{
        // ADD ID TO Array_IDs
        $Array_IDs[] = $Temp_Parts[1];
        // Create New ARRAY with HEADER AS PATTERN - ID
        $Array_Values[$Temp_Parts[1]][] = $variable;
    }
}

// ####################
// CODE STARTS HERE == ONLY THREE LINES ;)
// ####################
$Start_Array = array('123_dr_for_ma_on_2352015_2nd Shift_(08-29-2015_11-31).pdf','123_dr_for_ma_on_2352015_2nd Shift_(08-29-2015_11-30).pdf','123_bms_for__on__(10-06-2015_18-36).pdf');
array_filter($Start_Array,"Get_ID");
print_r($Array_Values);

?>

尝试一下,让我知道它是怎么回事,输出是按照要求的;)

答案 2 :(得分:2)

您可以使用array_reduce功能和正则表达式匹配优雅&amp;较小的代码。

$array = array(
    '0'=>'123_dr_for_ma_on_2352015_2nd Shift_(08-29-2015_11-31).pdf',
    '1'=>'123_dr_for_ma_on_2352015_2nd Shift_(08-29-2015_11-30).pdf',
    '2'=>'123_bms_for__on__(10-06-2015_18-36).pdf'
);

$result = array_reduce($array, function($prod, $current){
    preg_match('/(?<=^123_)\w+(?=_for\w+)/',$current,$match);
    $prod[$match[0]][] = $current;
    return $prod;
}, []);

将产生以下结果。

array(2) {
  [0]=>
  string(57) "123_dr_for_ma_on_2352015_2nd Shift_(08-29-2015_11-31).pdf"
  [1]=>
  string(57) "123_dr_for_ma_on_2352015_2nd Shift_(08-29-2015_11-30).pdf"
}
array(1) {
  [0]=>
  string(39) "123_bms_for__on__(10-06-2015_18-36).pdf"
}

使用方括号需要上面的PHP 5.4,你可以用array()代替低版本的PHP。

答案 3 :(得分:1)

请根据您的要求查找以下代码。

<?php
$array = array(
    '0'=>'123_dr_for_ma_on_2352015_2nd Shift_(08-29-2015_11-31).pdf',
    '1'=>'123_dr_for_ma_on_2352015_2nd Shift_(08-29-2015_11-30).pdf',
    '2'=>'123_bms_for__on__(10-06-2015_18-36).pdf'
);

$dr_array = array();
$bms_array = array();
foreach($array as $val){
    $str_explode = explode("_",$val);

    if($str_explode[1]=="dr"){
        $dr_array[] = $val;
    }
    else if($str_explode[1]=="bms"){
        $bms_array[] = $val;
    }
}
var_dump($dr_array);
var_dump($bms_array);
?>

输出:

array(2) {
  [0]=>
  string(57) "123_dr_for_ma_on_2352015_2nd Shift_(08-29-2015_11-31).pdf"
  [1]=>
  string(57) "123_dr_for_ma_on_2352015_2nd Shift_(08-29-2015_11-30).pdf"
}
array(1) {
  [0]=>
  string(39) "123_bms_for__on__(10-06-2015_18-36).pdf"
}

感谢。

答案 4 :(得分:-2)

$basicArray = array(); //It Stores All The Elements
$firstArray = array(); //It will store array elements of first type
$secondArray = array(); //It will store array elements of second type
for($i = 0; $i < count($basicArray); $i++)
{
    $elementArray = explode("_", $basicArray[i]);
    if($elementArray[1] == "dr")
        array_push($firstArray, $basicArray[i]);
    else if($elementArray[1] == "bms")
        array_push($secondArray, $basicArray[i]);
}
$finalArray = array(); 
array_push($finalArray, $firstArray, $secondArray);