我如何获得指定的数组值

时间:2021-12-06 12:16:59

Good day, I have this array from my Script. Here is my array

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

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
        )
)

I get the array from this script. I'm using codeigniter 3

我从这个脚本中获取数组。我正在使用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);
}

so from the array above i want to get this value BBP160800103,BBP160500010,WSL160800024,WSL160800025 and IA041017 how can i achieve it?

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

--UPDATE--

--UPDATE--

I do some update from my script (I'm trying)

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

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>';
            }
    }

for my script above i get this in my list

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

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

i want to achieve it

我想实现它

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

3 个解决方案

#1


1  

Pass your final array to this function to achieve your result.

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

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


2  

You can match against the patterns based on starting keys.

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

$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
)

#3


-1  

Try this:

尝试这个:

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

Etc. Refer to the indexing by number. You'd be better off creating your array with named indexes. Like this:

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

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

#1


1  

Pass your final array to this function to achieve your result.

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

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


2  

You can match against the patterns based on starting keys.

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

$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
)

#3


-1  

Try this:

尝试这个:

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

Etc. Refer to the indexing by number. You'd be better off creating your array with named indexes. Like this:

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

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