Foreach循环仅返回多维数组的第一个索引

时间:2017-11-09 22:07:30

标签: php arrays foreach

我在数组中的数组中有一个如下所示的数组:

$flattened_pics = array (
    [0] = array (
        _A = array (a bunch of data),
        _B = array (a bunch of data)
    ),
    [1] = array (
        _A = array (a bunch of data),
        _B = array (a bunch of data)
    )
)   

当我在外部数组上运行以下函数时,它成功地在索引[0]的内部数组上返回结果,但不返回索引[1]。

$goods['arts'] = Goods_Model_Ajax::convertLettered_arrays($flattened_pics);

我需要它来返回$ flattened_pics [0]和$ flattened_pics [1]的结果。

我尝试过单个foreach循环(如下所示),嵌套的foreach循环和其他变体。也许我需要在其中加上一个索引,但我不确定。

    if($flattened_pics) {
        foreach($flattened_pics AS $goods) {
            $goods['pics'] = array(Goods_Model_Ajax::convertLettered_arrays($goods)
            );

        }
    }

这是convertLettered_arrays函数的样子。

public static function convertLettered_arrays($pics)
{
    $out_pictures = array();
    $out_pictures['picture_a'] = array();
    $out_pictures['picture_b'] = array();
    $out_pictures['picture_c'] = array();
    $out_pictures['picture_d'] = array();

    $out_pictures['picture_a']['image'] = $pics[0]['_A']['picture_temp.image'];
    $out_pictures['picture_a']['original'] = $pics[0]['_A']['picture_temp.original'];
    $out_pictures['picture_a']['width'] = $pics[0]['_A']['picture_temp.width'];
    $out_pictures['picture_a']['height'] = $pics[0]['_A']['picture_temp.height'];
    $out_pictures['picture_a']['mime'] = $pics[0]['_A']['picture_temp.mime'];

    $out_pictures['picture_b']['image'] = $pics[0]['_B']['picture_temp.image'];
    $out_pictures['picture_b']['original'] = $pics[0]['_B']['picture_temp.original'];
    $out_pictures['picture_b']['width'] = $pics[0]['_B']['picture_temp.width'];
    $out_pictures['picture_b']['height'] = $pics[0]['_B']['picture_temp.height'];
    $out_pictures['picture_b']['mime'] = $pics[0]['_B']['picture_temp.mime'];

    $out_pictures['picture_c']['image'] = $pics[0]['_C']['picture_temp.image'];
    $out_pictures['picture_c']['original'] = $pics[0]['_C']['picture_temp.original'];
    $out_pictures['picture_c']['width'] = $pics[0]['_C']['picture_temp.width'];
    $out_pictures['picture_c']['height'] = $pics[0]['_C']['picture_temp.height'];
    $out_pictures['picture_c']['mime'] = $pics[0]['_C']['picture_temp.mime'];

    $out_pictures['picture_d']['image'] = $pics[0]['_D']['picture_temp.image'];
    $out_pictures['picture_d']['original'] = $pics[0]['_D']['picture_temp.original'];
    $out_pictures['picture_d']['width'] = $pics[0]['_D']['picture_temp.width'];
    $out_pictures['picture_d']['height'] = $pics[0]['_D']['picture_temp.height'];
    $out_pictures['picture_d']['mime'] = $pics[0]['_D']['picture_temp.mime'];

    return $out_pictures;
}

1 个答案:

答案 0 :(得分:0)

你应该像这样循环。

if ($flattened_pics) {
    foreach ($flattened_pics AS $goods) {
        $converted[] = Goods_Model_Ajax::convertLettered_arrays($goods);
    }
}

然后在convertLettered_arrays函数中,您只需删除所有[0]。像这样:

$out_pictures['picture_a']['image'] = $pics['_A']['picture_temp.image'];
$out_pictures['picture_a']['original'] = $pics['_A']['picture_temp.original'];
$out_pictures['picture_a']['width'] = $pics['_A']['picture_temp.width'];
$out_pictures['picture_a']['height'] = $pics['_A']['picture_temp.height'];
$out_pictures['picture_a']['mime'] = $pics['_A']['picture_temp.mime'];

您可以通过转储转换后的数组来检查输出

var_dump($converted);

我还建议您循环使用convertLettered_arrays功能,这样就可以避免使用粗码