array_push为现有键添加值

时间:2011-10-12 21:05:33

标签: php array-push

我有以下数组叫$ zoo

Array
(
[0] => &dog&
[1] => *1*
[2] => one
[3] => &cat&
[4] => *2*
[5] => two
[6] => &mouse&
[7] => *3*
[8] => three
[9] => &dog&
[10] => *4*
[11] => four
)

我需要以下结果:

Array
(
[&dog&] => Array
    (
        [0] => Array
            (
                [0] => *1*
                [1] => one
            )
        [1] => Array
            (
                [0] => *4*
                [1] => four
            )
    )
[&cat&] => Array
    (
        [0] => Array
            (
                [0] => *2*
                [1] => two
            )
    )
[&mouse&] => Array
    (
        [0] => Array
            (
                [0] => *3*
                [1] => three
            )
    )
)

这就是我提出的问题,但问题是对于[& dog&]他只给我最后一个值(即4,4)而不是第一个值(1,一个)

$animals=array();
for ($i = 0; $i < count($zoo); $i++)
{
if($zoo[$i][0] === "&")
    {
    $name=$zoo[$i];
    if (isset($name))
        $animals[$name] = array($liste);
    else
        $animals[$name] = array($liste);
    $liste="";
    }
if($zoo[$i][0] !== "&")
    {        
    $number = $zoo[$i];
    $liste[] = $number;
    $animals[$name] = array($liste);
    }
}
print_r($animals);

这导致

Array
(
[&dog&] => Array
    (
        [0] => Array
            (
                [0] => *4*
                [1] => four
            )
    )
[&cat&] => Array
    (
        [0] => Array
            (
                [0] => *2*
                [1] => two
            )
    )
[&mouse&] => Array
    (
        [0] => Array
            (
                [0] => *3*
                [1] => three
            )
    )
)

有人能指出我的方向吗?

2 个答案:

答案 0 :(得分:2)

$animals = array();

$c = count($zoo)/3;
for ($i = 0 ; $i < $c ; $i++)
{
    $species = array_shift($zoo);
    $number = array_shift($zoo);
    $number_text = array_shift($zoo);

    $animals[$species] []= array($number, $number_text);
}

如果你想通过“&amp;”触发新动物的开始在文中,有几种解决方案。这是我的:

$animals = array();
unset($current_animal);
foreach ($zoo as $text)
{
    if ($text{0} == '&')
    {
        // Insert an element for the new animal and get a reference to it
        $animals[$text] []= array();
        end($animals[$text]);
        $current_animal =& $animals[$text][key($animals[$text])];
    }
    else
        $current_animal []= $text;
}
unset($current_animal);

答案 1 :(得分:1)

适用于具有任意数量应该分组的索引的数组的解决方案。它只搜索&前缀并将新值添加到animals条目。

    $zooAmp = array(
            '&dog&',
            '*1*',
            'one',
            '&cat&',
            '*2*',
            'two',
            '&mouse&',
            '*3*',
            'three',
            '&dog&',
            '*4*',
            'four'
    );

    $zooStar = array(
            '*1*',
            '&dog&',
            'one',
            '*2*',
            '&cat&',
            'two',
            '*3*',
            '&mouse&',
            'three',
            '*4*',
            '&dog&',
            'four'
    );

    function & refactor(array $unfactored) {
            $len = count($unfactored);
            $data = array();

            if ($len<3) {
                    return $data;
            }

            if ($unfactored[0][0]=='&') {
                    //this algorithm isn't too bad, just loop through and add the ones starting with
                    //'&' to the data, and write everything from that index down to the next '&'
                    //into the created index in data.

                    $i=0;
                    while ($i<$len) {
                            if (!array_key_exists($unfactored[$i], $data))
                                    $data[$unfactored[$i]] = array();

                            //save to $arr for easier reading and writing
                            $arr = &$data[$unfactored[$i]];

                            $index = count($arr);
                            $arr[$index] = array();

                            for ($c=$i+1; $c<$len && $unfactored[$c][0]!='&'; $c++) {
                                    $arr[$index][] = $unfactored[$c];
                            }


                            $i = $c;
                    }

            } elseif ($unfactored[0][0]=='*') {
                    //this algorithm is a bit harder, but not so bad since we've already done the
                    //basic algorithm above.  We just need to store the ones with a '*' and then
                    //add them back into the array after it's been created.

                    $i=0;
                    $unorganizedItem = NULL;

                    while ($i<$len) {
                            $key = $unfactored[$i];

                            if ($key[0]=='*') {
                                    $unorganizedItem = $key;
                                    $i++;
                            } elseif ($key[0]=='&') {
                                    if(!array_key_exists($key, $data))
                                            $data[$key] = array();

                                    //save to $arr for easier reading and writing
                                    $arr = &$data[$key];
                                    $index = count($arr);

                                    $arr[$index][] = $unorganizedItem;
                                    $unorganizedItem = null;

                                    for ($c=$i+1; $c<$len && $unfactored[$c][0]!='&'; $c++) {
                                            if ($unfactored[$c][0]=='*') {
                                                    $unorganizedItem = $unfactored[$c];
                                            } else {
                                                    $arr[$index][] = $unfactored[$c];
                                            }
                                    }

                                    $i = $c;

                            }
                    }

            }

            return $data;

    }

    print_r(refactor($zooAmp));
    print_r(refactor($zooStar));

打印:

Array
(
    [&dog&] => Array
        (
            [0] => Array
                (
                    [0] => *1*
                    [1] => one
                )

            [1] => Array
                (
                    [0] => *4*
                    [1] => four
                )

        )

    [&cat&] => Array
        (
            [0] => Array
                (
                    [0] => *2*
                    [1] => two
                )

        )

    [&mouse&] => Array
        (
            [0] => Array
                (
                    [0] => *3*
                    [1] => three
                )

        )

)
Array
(
    [&dog&] => Array
        (
            [0] => Array
                (
                    [0] => *1*
                    [1] => one
                )

            [1] => Array
                (
                    [0] => *4*
                    [1] => four
                )

        )

    [&cat&] => Array
        (
            [0] => Array
                (
                    [0] => *2*
                    [1] => two
                )

        )

    [&mouse&] => Array
        (
            [0] => Array
                (
                    [0] => *3*
                    [1] => three
                )

        )

)