我想使用php将数组拆分成多个数组

时间:2015-08-22 08:47:45

标签: php arrays

这是我的o / p,我想得到多个数组,如果数组中包含一个带空格的字符串。我该怎么做呢

Array ( [0] => KERALA-682030 );
Array ( [0] => INDIA );
Array ( [0] => Date : 19 Aug 2015 ) etc.........18 arrays are there

我想:

$arr[1]=KERALA;
$arr[2]=682030
$arr[3]=INDIA
$arr[4]=Date
$arr[5]=19 Aug 2015;

剩余阵列:

Array ( [0] => Address :    IX/355 G2, 2ND FLR, MLMLALL MANSION IJKIKK ROAD, KUXHSIDFSFF  KUGGGGGG PO, KJJSJFSJF ErJSDFJSF ) Array ( [0] => KOCHI ) Array ( [0] => KERALA-6812555 ) Array ( [0] => INDIA ) Array ( [0] => Date : 19 Aug 2015 ) Array ( [0] => Account Number :  _000000555444545889 ) Array ( [0] => Account Description:   CURRENT A/C - COLLECTION A/C ) Array ( [0] => Branch : Skkgdgdg ) Array ( [0] => Drawing Power :    0.00 ) Array ( [0] => Interest Rate(% p.a.):    0.0 ) Array ( [0] => MOD Balance :  0.00 ) Array ( [0] => CIF No. : _44454444545 ) Array ( [0] => IFS Code :    HY155555555 ) Array ( [0] => MICR Code :    _XXXXXXX) Array ( [0] => Balance on 1 Aug 2010 : 1,12,494.98 ) Array ( [0] => Start Date :  1 Aug 2015 ) Array ( [0] => End Date :  19 Aug 2015 ) Array ( [0] => Txn Date   Value Date  Description Ref No./Cheque No.  Branch Code Debit   Credit  Balance ) Array ( [0] => 12 Aug 2015    12 Aug 2015 TO TRANSFER-INB TAX RETERN JULY--   CT1254555 TRANSFER TO 12212121211 Mr.XXXXXT G / 99922   12,300.00   1,00,194.98 ) Array ( [0] => 17 Aug 2015    17 Aug 2015 BY TRANSFER-NEFT*HDS5008888885*KEhhhh CARS PVT--    TRANSFER FROM 319255555554 / 4430   13,000.00   1,13,194.98 ) Array ( [0] => 18 Aug 2015    18 Aug 2015 TO TRANSFER-INB BSNL_RECHARGE Payments--    dfsff122121212 TRANSFER TO 3199597162098 / 99922    135.00  1,13,059.98 ) Array ( [0] => ) 

3 个答案:

答案 0 :(得分:0)

您可以使用explode函数来爆炸或分离数组中的各个元素。如果您想要获取数组并将其分解为多个部分,这将非常有用。我所包含的这段代码可能无法按照您的意愿执行,但我认为您正在寻找的是爆炸()。

$addressArray[1] = '1234 Wilker Way';
$addressArray[2] = '5678 Jonas Town';
function breakArrayUp($passedArray1,$passedArray2)
{
global $addressArray;


foreeach($break as $passedArray)
{
$x++;
$final[] = explode($passwedArray[$x]));
}
}

答案 1 :(得分:0)

您需要使用array_chunk函数http://php.net/array_chunk

或者这里是一个非常好的函数:(但这比上面的PHP函数慢)

/**
 * Split a array into semi-equal sized chunks.
 * A so-called 'columnizer'
 *
 * @param array $array          The array to split
 * @param array $numberOfChunks The number of chunks
 * @param bool  $vertical       Whether to order vertically or horizonally
 *
 * @return array Array with $numberOfColumns nodes with items of $array
 */
function arraySplit($array, $numberOfChunks = 2, $vertical = true)
{
    if ($vertical) {
        $perColumn = floor(count($array) / $numberOfChunks);
        $rest = count($array) % $numberOfChunks;
        $perColumns = array();
        for ($i = 0; $i < $numberOfChunks; $i++) {
            $perColumns[$i] = $perColumn + ($i < $rest ? 1 : 0);
        }
        foreach ($perColumns as $rows) {
            for ($i = 0; $i < $rows; $i++) {
                $data[$i][] = array_shift($array);
            }
        }
    } else {
        $i = 0;
        foreach ($array as $node) {
            $data[$i][] = $node;
            $i = ($i >= ($numberOfChunks - 1) ? 0 : $i + 1);
        }
    }
    return $data;
}

答案 2 :(得分:0)

我想要通过这三个分隔符来分割字符串:'-'' : '' ' 在那种情况下:

//array with all the split elements from all arrays 
$total_array = array();

//do the following foreach of your arrays

$array_to_split = array('KERALA-682030');

//replace the '-' and ' : ' by a space ' '
$string_to_split = $array_to_split[0];
$string_to_split = str_replace('-',' ',$string_to_split);
$string_to_split = str_replace(' : ',' ',$string_to_split);
$splited_array = explode(' ',$string_to_split);

$total_array = array_merge($total_array, $splited_array);