-1

有一个这样的数组:

Array
(
    [0] => stdClass Object
        (
            [url] => https://is0.Z0V.jpg
            [width] => 370
            [height] => 370
        )

    [1] => stdClass Object
        (
            [url] => https://is1.et/derived_pi0V_300x300.jpg
            [width] => 300
            [height] => 300
        )

    [2] => stdClass Object
        (
            [url] => https://is1.n100x100.jpg
            [width] => 100
            [height] => 100
        )

    [3] => stdClass Object
        (
            [url] => https://is1.t/dZ0V_36x36.jpg
            [width] => 36
            [height] => 36
        )

)

我如何只从这个 foreach 中获取 [2] 并将其插入:

foreach($fotoSuggSizes as $fotoSuggSize100)
    {

        $image_urlSugg100[] = array("urlx100"=>$fotoSuggSize100->url);

    }
4

4 回答 4

1

您可以使用以下方法在现有数组中的特定偏移处插入另一个数组array_splice

$insert = array("urlx100"=>$fotoSuggSize100->url);
array_splice($fotoSuggSizes, 2, 0, array($insert));

但是,如果这就是您要寻找的,我不能完全确定您的问题。

另一种方式是:

$image_urlSugg100[] = array("urlx100" => $fotoSuggSizes[2]->url);
于 2013-01-06T23:26:51.297 回答
1

另一种方法如下:

foreach($fotoSuggSizes as $n => $fotoSuggSize100)
{
   if ($n != 2) continue;
   $image_urlSugg100[] = array("urlx100"=>$fotoSuggSize100->url);
}
于 2013-01-06T23:28:13.687 回答
1

原来的数组叫$fotoSuggSize100?

$image_urlSugg100[] = array("urlx100"=>$fotoSuggSize100[2]->url);
于 2013-01-06T23:28:20.723 回答
1

也许我错过了重点,但是:

$image_urlSugg100[] = array("urlx100"=>$fotoSuggSizes[2]->url);

或者,如果由于某种原因您特别想使用该foreach()方法:

foreach ($fotoSuggSizes as $k => $v)
{
    if ($k == 2)
    {
         $image_urlSugg100[] = array("urlx100"=>$v->url);
    }
}
于 2013-01-06T23:28:51.067 回答