0

I am working with PHP array to store the comma separated values of product Ids and then in turn use them to show as recent seen products.

Implementation is as below:

$product_data = array();
$array_ids_a_display = array();   
$check_status = array();
$array_check= array();
$_COOKIE['Pr'] = [1,2,3,4,5,6,7,8]

Then i am Taking the last point of the stored string

$array_check = explode(',',substr($_COOKIE['IdProduto'],0,-1));

Now, i check of products are enabled then store them into one other array

foreach($array_check as $id_a_checar){
    $check_status = $this->model->getProduct($id_a_checar);
    if($check_status['status']){ // If status is 1
        $array_ids_a_display[$contprods++] = $id_a_checar;
    }
}

if($s['limit']>count($array_ids_a_display)){ 
    //If the number of valid products < number of products of module
    $s['limit'] = count($array_ids_a_display);  
      //will show,then reconfigures the number of products that will show
  }   
}

where $s['limit'] comes from backend , let us say 6 to limit the number of products.

Now, i will reverse the array to get latest visited product at first place like as

$last_ended = array_reverse($array_ids_a_display);
array_splice($last_ended,0,(int)$s['limit']);
foreach ($last_ended as $result) {
    $product_data[$result] = $this->product->getProduct($result);
}     

Now here comes the problem, as i am only getting 3 products in $product_data array but is shall get 6 products.

I hope that there is issue with array_splice because if i will comment array_splice then i am getting all stores products in cookies as result. Mysql query is working very fine.

Please advice how to get latest 6 values from array

4
  • if I were you the first thing I would do is some echo debugging to see where your data is getting lost Commented Jun 15, 2016 at 20:39
  • I have written that my data has problems with array_splice Commented Jun 15, 2016 at 20:41
  • Till array reverse i am getting all 8 IDs Commented Jun 15, 2016 at 20:42
  • Your data is what you need to show. Nobody knows what your data is. Commented Jun 15, 2016 at 20:43

3 Answers 3

2

Here you are:

$last_ended = array(1, 2, 3, 4, 5, 6, 7, 8);
$last_ended = array_reverse($last_ended);
//here is what you missed:
$last_ended = array_splice($last_ended, 0, 6);

print_r($last_ended);
//returns Array ( [0] => 8 [1] => 7 [2] => 6 [3] => 5 [4] => 4 [5] => 3 )

You need to assign your $last_ended var into array_splice result.

Sign up to request clarification or add additional context in comments.

Comments

1
<?php
// Get the last 6 entries of an array...
$arr = array(
      1,
      2,
      3,
      4,
      5,
      6,
      7,
      8
);

$entries = array_splice($arr, -6);
// returns [3, 4, 5, 6, 7, 8]

}

Comments

0
$maxResultLength = 6;
$someArray = [1,2,3,4,5,6,7,8,9,10,'a','b','c','d','e','f','g','h'];
$startIndex = (count($someArray) >= $maxResultLength) ? count($someArray) - $maxResultLength : 0;
$lastSixElements = array_slice($someArray, $startIndex, $maxResultLength);

or

$maxResultLength = 6;
$someArray = [1,2,3,4,5,6,7,8,9,10,'a','b','c','d','e','f','g','h'];
$lastSixElements = array_splice($someArray, -6);

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.