我有一个对象值
foreach($this->promo_cart->contents() as $produks){
foreach($this->login_sess->contents() as $val):
$idex = array_unique(explode('_', $produks['prod_name']));
if($val['id']==$idex[2]) {
echo $val[id]; //result is 314314315
}
endforeach;
}
我如何根据该结果制作数组,例如
array
(
[0] => 314
[1] => 314
[2] => 315
)
答案 0 :(得分:1)
您可以将元素添加到数组中,如下所示:
$foo = array(); // start with empty array
$foo[] = 'bar'; // added element to array => array([0] => "bar")
所以在你的情况下你可以这样做:
$my_array = array();
foreach($this->promo_cart->contents() as $produks)
{
foreach($this->login_sess->contents() as $val)
{
$idex = array_unique(explode('_', $produks['prod_name']));
if($val['id']==$idex[2]) {
$my_array[] = $val[id];
}
}
}
$my_array
将包含:
array
(
[0] => 314
[1] => 314
[2] => 315
)
答案 1 :(得分:1)
创建新数组并使用[]
将所需值推送到该数组
试试这个
$tempArray=array(); //<---create new array
foreach($this->promo_cart->contents() as $produks){
foreach($this->login_sess->contents() as $val):
$idex = array_unique(explode('_', $produks['prod_name']));
if($val['id']==$idex[2]) {
$tempArray[] = $val[id]; //<----push value to array.
}
endforeach;
}
print_r($tempArray);