我如何获得指定的数组值

时间:2017-07-05 04:03:39

标签: php arrays codeigniter

美好的一天,我的脚本中有这个数组。这是我的数组

Array
(
    [0] => Array
        (
            [0] => BACK
            [1] => PACK
            [2] => BBP160800103
            [3] => 
            [4] => G086-1
            [5] => 8#.JPG
        )
     [2] => Array
        (
            [0] => BACKPACK
            [1] => BBP160500010
            [2] => G114-3#1.JPG
        )
 [3] => Array
        (
            [0] => WSL160800024-WSL160800025
            [1] => L83-5.JPG
        )
 [4] => Array
        (
            [0] => IA041017
            [1] => L83-5.JPG
        )
)

我从这个脚本中获取数组。我使用codeigniter 3

public function generatenewname(){
    $this->load->helper('directory');
    $map = directory_map( './assets/file_upload/md/temp/',FALSE, TRUE);
    for($x=0;$x<count($map);$x++){
        $newest[$x] = explode(" ",$map[$x]);
    }
    echo "<pre>";print_r($newest);
}

所以从上面的数组中我想得到这个值BBP160800103,BBP160500010,WSL160800024,WSL160800025IA041017我该如何实现呢?

- UPDATE -

我从我的脚本做了一些更新(我正在尝试)

public function generatenewname(){
        $this->load->helper('directory');
        $map = directory_map( './assets/file_upload/md/temp/',FALSE, TRUE);
        for($x=0;$x<count($map);$x++){
            $newest[$x] = explode(" ",$map[$x]);
                for($y=0;$y<count($newest[$x]);$y++){
                    if(strlen($newest[$x][$y]) >= 7){
                        $file[] =$newest[$x][$y];

                    }
                }
        }
            for($x=0;$x<count($file);$x++){
                echo $x.' '. $file[$x].'<br>';
            }
    }

对于我上面的脚本,我在列表中得到了这个

[n] BBP160800103
[n] BBP160500010
[n] WSL160800024-WSL160800025

我想实现它

    [n] BBP160800103
    [n] BBP160500010
    [n] WSL160800024
    [n] WSL160800025

3 个答案:

答案 0 :(得分:2)

您可以根据启动键匹配模式。

$array = [
    ['BACK', 'PACK', 'BBP160800103', '', 'G086-1', '8#.JPG'],
    ['BACKPACK', 'BBP160500010', 'G114-3#1.JPG'],
    ['WSL160800024-WSL160800025', 'L83-5.JPG'],
    ['IA041017', 'L83-5.JPG']
];

$patterns = ['(BBP\w+)', '(WSL\w+)', '(IA\w+)'];

$codes = [];
$matcher = '/' . implode($patterns, '|') . '/';

foreach ($array as $arr) {
    array_map(function ($value) use ($matcher, &$codes) {
        preg_match_all($matcher, $value, $matches);

        foreach($matches as $match) {
            $codes = array_merge(array_filter($match), $codes);
        }
    }, $arr);
}

print_r(array_unique($codes));

// output
Array
(
    [0] => IA041017
    [2] => WSL160800024
    [3] => WSL160800025
    [6] => BBP160500010
    [8] => BBP160800103
)

答案 1 :(得分:1)

将最终数组传递给此函数以获得结果。

arraymakking($file);//your array

function arraymakking($arr){
    foreach($arr as $key=>$val){
        if(strpos($val,'-') !== false){
            $res=explode('-',$val);
            unset($arr[$key]);
        }
    }
    $result=array_merge($arr,$res);
    return $result;
}

答案 2 :(得分:-1)

试试这个:

echo $array[0][3]; //BBP160800103
echo $array[2][1]; //BBP160500010

等。请参阅编号索引。使用命名索引创建数组会更好。像这样:

$array = array("Array1"=>array("key1"=>"val1","key2"=>"val2"),"Array2"=>(array("2ndKey1"=>"val1", "2ndKey2"=>"val2));