0

I have an array in the below format which I am showing in a datatable:

Array
(
    [0] => Array
        (
            [id] => 67
            [name] => test1
            [contactnumber] => 1234567890
            [status] => 1
        )

    [1] => Array
        (
            [id] => 73
            [name] => test2
            [contactnumber] => 1232123432
            [status] => 2
        )

    [2] => Array
        (
            [id] => 65
            [name] => test3
            [contactnumber] => 7890987890
            [status] => 1
        )

)

I am using a foreach to access all the records:

foreach($resultleads as $resultleadsdata)

And the below code to get the previous iteration id, which is not working, I am getting some random ids instead of the previous one:

$previd=0;
foreach($resultleads as $resultleadsdata) {
    echo $previd;
    $previd=$resultleadsdata['id'];
}

Have tried with the next() function also, which is giving the name instead of id.

srikanta mondal
  • 119
  • 2
  • 15
  • your code is working fine : https://3v4l.org/PsrT (I covered both example, echo first and then assignment and assign first and then echo ) – Alive to die - Anant Oct 19 '20 at 04:41
  • In datatable it is not working? – srikanta mondal Oct 19 '20 at 04:42
  • then kindly add datable related code in your question. Also explain what you exactly trying to achieve with this code in your data table – Alive to die - Anant Oct 19 '20 at 04:43
  • 1
    tell me what kind of result u needed? – Muhammad Kamran Oct 19 '20 at 04:44
  • Does this answer your question? [get previous array values in foreach](https://stackoverflow.com/questions/4148501/get-previous-array-values-in-foreach) – kelvin Oct 19 '20 at 04:46
  • I have a dropdown to filter the result with status, and the result will disappear if the status changed from the dropdown of the table. Now as there are lots of records in the datatable I need to highlight the next record so that user can check where they were – srikanta mondal Oct 19 '20 at 04:53
  • also why you are not thinking about `for` loop where you can easily reference to old row just using `i-1` identifer – Dark Knight Oct 19 '20 at 05:02
  • @JitendraYadav, Yes I thought of that, but this is a old legacy code and if I change the loop lots of other things in that page need to be changed, so trying to get some way out of this foreach – srikanta mondal Oct 19 '20 at 05:03
  • okay if that is reasonable, if so, now as asked previously show us what you want and what is not working by including input and expected output. also your `$previous` is not an index in your main array, so you won't get correct data. Instead you need to search for it. Or you can do a quick loop first by changing the array in which you'll keep the structure as it is and just set `id` as `key` of the array and then your code will work perfectly. – Dark Knight Oct 19 '20 at 05:04

1 Answers1

-1

Try this

$previd=0;
foreach($resultleads as $key => $resultleadsdata) {
    if($key > 0){
        $previd= $resultleads[$key - 1]['id'];
    }
    echo $previd;
}
Muhammad Kamran
  • 151
  • 1
  • 4