PHP Specific Array Organization Function - php

I have a few session arrays which I happen to be removing specific indexes from. For example, I have a session named $_SESSION['products'], this session has these elements: $_SESSION['products'][0], $_SESSION['products'][1], and $_SESSION['products'][2].
I am trying to remove any one of those variables, the problem is when you remove the second variable, you mess up the array so that it cannot be displayed in a for loop. Is there a way to rearrange the following: $_SESSION['products'][0] and $_SESSION['products'][2] to $_SESSION['products'][0] and $_SESSION['products'][1] with a PHP built-in function? If not, is it even possible?

You can achieve this with array_values like:
unset($_SESSION['products'][2]); // assuming the product key
// exist in product array sess
$_SESSION['products'] = array_values($_SESSION['products']);
Manual
array_values() returns all the values from the array and **indexes the array numerically.**

Related

Laravel, getting specific array index from session

Currently, I'm dumping an array that is held within my laravel session, which successfully dumps the array:
<?php dd(Session::get('Tokens'));?>
This dumps an array with three elements, each with its own index
array(
"userToken":"value",
"secondToken":"value",
"thirdToken":"value",
);
I keep running into errors trying to get specifically the userToken. I've tried get('Tokens[userToken]') but It's expecting a string only
How should I change this to be able to access any array key specifically
You can use array dereferencing and add a required index directly to value, returned by Session::get('Tokens'):
echo Session::get('Tokens')['userToken'];
you can try this too
$value = session('Tokens');
and you can use $value like and array and you'll be able to call each value with the index like
$value['userToken']
hope it help

Push or Merge data into existing Array

When working with existing code, it takes one array and places it into another in the fashion shown below.
I believe the empty brackets are the same thing as simply pushing it and appending it to the first available index.
$g['DATA'][] = $p;
After this is done, I have my own array that I would like to append to this as well. I tried using array_merge() with $g['DATA'][]as a parameter, but this is invalid for obvious reasons.
My only thought is to create a foreach loop counter so I can figure out the actual index it created, however I have to assume there is some cleaner way to do this?
Just simply use the count() of your $g["DATA"] array as index and then you can merge it like this:
$g['DATA'][count($g["DATA"])-1] = array_merge($g['DATA'][count($g["DATA"])-1], $ownArray);
//^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^
// -1 Because an array is based index 0 -> means count() - 1 = last key

Check if same value exist in an array if not store into array

I can't figure out the logic to how to check if a value exist in an array and if it doesn't store it.
I have a array with information and I'm trying to just get the first instance since some of the values repeats, for example username it will show up more than once but I only need the first username I don't need to store multiple values of the same username in an array.
Righh now I'm using pushing all the valuse into the array as so
for ($i=0; $i<count($info); $i++) {
array_push($users, $info[$i]->username);
}
There are several ways to do this:
Loop through the array and check to see if the value exists. If not, add it.
Use in_array() to check to see if the value exists. If not, add it.
Add the value to the array and then use array_unique() to eliminate duplicate values.

Function to assign sequence keys to an array

I have an array of unique colors which is selected from a table. But keys of that particular array is not in sequence order due to some calculations. Now I wish to assign a sequence numbers to that array... Is any function to change keys of array..
Thanks...
Array(
[81]=>yellow
[86]=>gray
[93]=>wine
[103]=>marigold
[125]=>maroon
[134]=>pewter
[142]=>forestgreen
[151]=>grey
)
i wish to change this array to
Array(
[1]=>yellow
[2]=>gray
[3]=>wine
[4]=>marigold
[5]=>maroon
[6]=>pewter
[7]=>forestgreen
[8]=>grey)
If you want to sort your array by some calculation, you can use usort(), which sorts an array by using a callback function. In this callback function, you can compare two elements of the array and decide (by any means you need) which one goes first. Read through the examples on the page I linked to learn more!
use: sort($color); check the examples from this link: sort

Session array accessing on next button click of next button

How to get next array element from session array on next button click?
I tried next($_SESSION['qid']) it didn't work
if((int)$_SESSION['qn']<=20) {
$_SESSION['qn']=$_SESSION['qn']+1;
$_SESSION['qid']++;
}
I also tried
$_SESSION['qid']=next($_SESSION['qid']);
But this neither worked. Can someone help me?
_SESSION array is an associative array. You can't access it by numeric index, but you must specify the index name (e.g. in your code, $_SESSION['qid']). Anyway, you can still use next() function, passing the array $_SESSION (see here: http://php.net/manual/en/function.next.php). The correct way to use it is:
$element = next($_SESSION)
you'd likely want to put this code in a cycle.
Additionally, your code:
$_SESSION['qn']=$_SESSION['qn']+1;
means: assign to $_SESSION['qn'] the value of $_SESSION['qn'] plus 1, which is not what you want.
In case you'd want the next element in a NON associative array, you should use:
$arr = $arr[$i+1]
where $i is a integer value.
Update: regarding your comment, why don't you save a regular array (non associative) inside $_SESSION['questions']? This way you'd have the questions accessible this way:
$_SESSION['questions'][0], $_SESSION['questions'][1]...
Now you can use it within a cycle, or whatever you want. E.g.:
echo $_SESSION['questions'][$current_question_id+1];
where $current_question_id would be the current question index, which will be updated (+1) every clic on the next button

Categories