1

I have an object value

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;
}

how I make an array from that result, like

array
(
  [0] => 314
  [1] => 314
  [2] => 315
)

2 Answers 2

1

You can add elements to an array like this:

$foo = array(); // start with empty array
$foo[] = 'bar'; // added element to array => array([0] => "bar")

So in your case you can do this:

$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 will contain:

array
(
  [0] => 314
  [1] => 314
  [2] => 315
)
Sign up to request clarification or add additional context in comments.

Comments

1

create new array and push the required value to that array with [] try this

 $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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.