PHP,在循环中,将某些数组值设置为单个数组

时间:2017-01-12 15:22:31

标签: php arrays foreach

我有水果信息的数组。一个例子:

Array
("
    [0] => Array
        ("
            [Id] => 58759d25bb3aa9.09061697
            [Name] => Gala
            [Description] => ''
            [Note] => ''

        ) "

       [1] => Array
        ("
            [Id] => 587589476029e8.93893728
            [Name] => Admiral
            [Description] => ''
            [Note] => ''

        ) "
   [2] => Array
        ("
            [Id] => 5875b2a1865f50.73423631
            [Name] => Elstar
            [Description] => ''
            [Note] => ''

        ) "
) "
Array
("
    [0] => Array
        ("
            [Id] => 5875b2a1865f50.73423631
            [Name] => Bing-Cherries
            [Description] => ''
            [Note] => ''

        ) "

       [1] => Array
        ("
            [Id] => 5875b2c6210de4.91470020
            [Name] => Van-Cherries
            [Description] => ''
            [Note] => ''

        ) "
   [2] => Array
        ("
            [Id] => 5875b911eeddb1.20644994
            [Name] => Reiner-Cherries
            [Description] => ''
            [Note] => ''

        ) "
) "

在循环中,我可以访问像apple,cherries等数组类型。每个数组都有一个唯一的数字作为类型。示例樱桃是($sConfigType == 1)

现在我想,如果水果"樱桃"来将数组值设为单个数组。

例如:

Array
(
    [0] => Array
        (
            [id] => 58759d25bb3aa9.09061697
            [name] => Gala
            [description] => ''
            [note] => ''

        )

       [1] => Array
        (
            [id] => 587589476029e8.93893728
            [name] => Admiral
            [description] => ''
            [note] => ''

        )
   [2] => Array
        (
            [id] => 5875b2a1865f50.73423631
            [name] => Elstar
            [description] => ''
            [note] => ''

        )
)
Array
(
    [0] => Array
        (
            [id] => 5875b2a1865f50.73423631
            [name] => Bing-Cherries
            [description] => ''
            [note] => ''

        )
)
Array (Cherries)
(
       [0] => Array
        (
            [id] => 5875b2c6210de4.91470020
            [name] => Van-Cherries
            [description] => ''
            [note] => ''

        )
)
Array (Cherries)
(
   [0] => Array
        (
            [id] => 5875b911eeddb1.20644994
            [name] => Reiner-Cherries
            [description] => ''
            [note] => ''

        )
)

所以我试图实现它,遗憾的是不起作用:

protected function _splitFruitArray($sConfig)
{
    if (is_array($sConfig) || is_object($sConfig)) {

        $sConfigType = $sConfig->type;

        $sOther = array();

        foreach ($sConfig as $sKey => $sVal) {
            if ($sConfigType == 1) { // if fruit is cherries split values to single array
                foreach ($sVal as $iKey => $iVal) {
                    $arr[$iKey] = $iVal;
                }
            } else {
                $sOther[$sKey] = $sVal;
            }
            array_push($sOther, $arr);
        }
        $sConfig = $sOther;
    }
    print_r($sConfig);

    return $sConfig;
}

1 个答案:

答案 0 :(得分:0)

试试这个..

if (is_array($sConfig) || is_object($sConfig)) {
    $sOther = array();
    foreach ($sConfig as $sKey => $sVal) {
        if ($sConfigType == 1) { // if fruit is cherries split values to single array
            is_array($sVal){
            foreach ($sVal as $iKey => $iVal) {
                $arr[$iKey] = $iVal;
            }
             }

        } else {
            $sOther[$sKey] = $sVal;
        }
        array_push($sOther, $arr);
    }
    print_r($sOther);
     //print_r($arr);
 for(i=0;i<count($arr);i++){
     ${"newArray".$i} = $arr[$i];
        }
     //print_r($newArray0);
     //print_r($newArray1);
     //print_r($newArray2);
    return $sOther;
}