在数组内更改值

时间:2018-09-01 22:04:24

标签: php arrays explode

我想更改数组的值。

当前我的数组如下:

Array
(
    [0] => Array
        (
            [0] => 12-Multi_select-customfield-retina-ready+Yes
            [1] => 12-Multi_select-customfield-retina-ready+N/A
            [2] => 12-Multi_select-customfield-retina-ready+No
        )

)

我想删除+号之前的所有内容,因此最终新数组看起来像这样

Array
(
    [0] => Array
        (
            [0] => Yes
            [1] => N/A
            [2] => No
        )

)

这是我的代码:

        $new_array = array();

        foreach( $array as $key => $value ) {

            $split = explode("+", $value[0]);               
            $new_array[] = $split[1];   

        }

希望它能起作用,但是当我检查新数组时,它仅显示一个值。

Array
(
    [0] => Yes
)   

任何帮助我朝正确方向发展的帮助,我们深表感谢。

2 个答案:

答案 0 :(得分:1)

请检查:

<?php
$array[0][0] = '12-Multi_select-customfield-retina-ready+Yes';
$array[0][1] = '12-Multi_select-customfield-retina-ready+N/A';
$array[0][2] = '112-Multi_select-customfield-retina-ready+No';

echo '<pre>';
print_r($array);

$new_array = array();

foreach( $array[0] as $key => $value ) {

    $split = explode("+", $value);
    $new_array[] = $split[1];

}
print_r($new_array);

echo '</pre>';

答案 1 :(得分:0)

尝试即使您在原始数组$original_array[0]$original_array[1] ...中有多个键,也可以进行此操作:

$original_array[0] = [
    0 => '12-Multi_select-customfield-retina-ready+Yes',
    1 => '12-Multi_select-customfield-retina-ready+N/A',
    2 => '12-Multi_select-customfield-retina-ready+No'
];
print_r($original_array); 

$new_array = [];
foreach ($original_array as $key => $value) {
    foreach ($value as $index => $val) {
        $split = explode("+", $val);
        $new_array[$key][] = $split[1];
    }
}

print_r($new_array);

示例:
Original array

Array
(
    [0] => Array
        (
            [0] => 12-Multi_select-customfield-retina-ready+Yes
            [1] => 12-Multi_select-customfield-retina-ready+N/A
            [2] => 12-Multi_select-customfield-retina-ready+No
        ),
   [1] => Array
        (
            [0] => 12-Multi_select-customfield-retina-ready+Yes
            [1] => 12-Multi_select-customfield-retina-ready+N/A
            [2] => 12-Multi_select-customfield-retina-ready+No
        )
)  

New Array

Array
(
    [0] => Array
        (
            [0] => Yes
            [1] => N/A
            [2] => No
        ),
    [1] => Array
        (
            [0] => Yes
            [1] => N/A
            [2] => No
        )
)