我想使用下面的代码将值添加到数组,但它似乎只会导致1项(最后一个在循环中)被添加到数组中。向嵌套数组中添加值
$awin_products = array();
foreach($awinprices as $value){
foreach($value as $obj){
$awin_products[name] = (string)$obj->sName;
$awin_products[imageUrl] = (string)$obj->sAwThumbUrl;
}
}
print_r($awin_products);
这可能是很简单的修复,但到目前为止我还没有找到答案。
编辑:我在寻找这样的输出:
Array
(
[0] => Array
(
[name] => Item 1 Name
[imageUrl] => http://example.com/item1.jpg
)
[1] => Array
(
[name] => Item 2 Name
[imageUrl] => http://example.com/item2.jpg
)
[2] => Array
(
[name] => Item 3 Name
[imageUrl] => http://example.com/item3.jpg
)
[3] => Array
(
[name] => Item 4 Name
[imageUrl] => http://example.com/item4.jpg
)
)
不是我正在寻找的结构。看到我上面的编辑。 – sr83
@ sr83由于您没有发布期望的结构,因此很难获得您想要的结果。见编辑的答案 –