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
Related
I simply want to know how to access array elements retrieved from a database. I have the following code to get the names of each item in my database.
$plat_options = $this->db->get('tblplatform_options')->select('name')->result();
How do I go about accessing the name from the array $plat_options? Typically I would do $plat_options[0] for the first element in C#, how is this done in php/codeigniter?
In PHP/Codeigniter, can be done in the same way:
$plat_options[0] //if you have this element, usually is better to check if exists.
You can retrieve all the elements with foreach($plat_options as $option){...}
You can cast to object: https://www.kathirvel.com/php-convert-or-cast-array-to-object-object-to-array/
Or use a Codeigniter Helper (assuming you are using CI3): http://www.codeigniter.com/user_guide/helpers/array_helper.html
I recomend to know which is your array format and retrieve that way (if you don't know, you can do a: var_dump($plat_options) ) to know if is an associative array.
You can use the result_array() function:
$data = $plat_options->result_array();
echo($data[0]['name']);
or:
$data = array_shift($q->result_array());
echo($data['name']);
I extracted this last part from: Codeigniter $this->db->get(), how do I return values for a specific row? that you could check too.
If you don't know a lof of CI, the best you can do is do a simple tutorial to understand how the data + ActiveRecord works.
Hope it helps!
I have created array variable in .php file
like
$arImagePath[TE] = 'XYZ';
in my .tpl
{$carnumber} is giving 'T' and {$carinitial} is giving 'E'.
I am trying to get value 'XYZ' as follows
{$arImagePath[{$carnumber}+{$carinitial}]}
I tried many combination still unavailable to get array value.
smarty version -2.6.26
Hoping for any help.
From documentation (Smarty v2) :
{$foo[bar]} <-- syntax only valid in a section loop, see {section}
So, if you want to access the array variable directly and you are not in a loop, you have to do it this way:
{$foo.bar} <-- display the "bar" key value of an array, similar to
PHP $foo['bar']
Now, to archive what you need:
// This assignment could change dynamically
{assing var="carnumber" value="T"}
{assing var="carinitial" value="E"}
// For the sake of clarity, I'm going to concat in one variable the above assignments
{assing var="index" value=$carnumber|cat:$carinitial}
//Now access the array at the index we need
{$arImagePath.$index} // XYZ
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
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.**
Without looping a set of array keys acquired via array_keys($array), how else can I select the key of an array such that $array["key"] where "key" associates to a second subsequent array -- PHP otherwise outputs a notice stating that "key" is undefined.
Any help is sincerely appreciated.
I think you're looking for isset(), for example if( isset($array['key'])) ...
isset() works for variables, but the error your likely getting is for an undefined key/index. You'll want to try array_key_exists() before trying to use the key (and based on the results, either use or create the key).
http://www.php.net/manual/en/function.array-key-exists.php