PHP get a variable's name in foreach loop [duplicate] - php

This question already has answers here:
How to loop through an associative array and get the key?
(12 answers)
Closed 4 years ago.
i'm making a project in PHP and i have a problem. My problem is that i don't know how to get a variable's name in $_GET foreach loop.
My code looks like this:
foreach($_GET as $get) {
$v_name = //I want to be this variable the $get variable's name
}
So example: if someone make a post request like this: http://www.example.com/?something=example
In this case i want to get the "something".
Thanks everyone who will help me - Jumpak

You need to print the key of array $_GET
You should read basics of foreach loop.
foreach($_GET as $key=>$get) {
// ^^^ // You can get the index key from Array
//$v_name = //I want to be this variable the $get variable's name
echo $key;
}

Related

Show a single item of an arrray [duplicate]

This question already has answers here:
get a single value of array items in php
(3 answers)
Closed 7 years ago.
I am having a bit of trouble getting a single item shown/echoed in a foreach loop. What I mean I have an array (filled from database).
return array(
'FriendName' => $FriendName,
'FriendImage' => $FriendImage
);
What I want is to show only FriendName while in a loop.
I have tried and this is an example:
foreach($UserFriends as $item) {
echo $item['FriendName'];
}
But it shows NULL and not the data I expect.
No need for a loop if you just need one value, you can use this instead:
echo $UserFriends['FriendName']
Learn more about php Arrays
Its been better to use it directly instead of looping an array if its not multidimensional array you can simply get it using
$UserFriends['FriendName']
you can checked using if condition like
foreach($UserFriends as $key => $item) {
if($key == 'FriendName'){
echo $item;
}
}
your array is not multidimensional so you can access vlaues like that.
or else you can get a value directly like
echo $userFriends['FriendName'];
I hope this is working for you

Unset element in array [duplicate]

This question already has answers here:
PHP array delete by value (not key)
(20 answers)
Closed 9 years ago.
I want to unset 1 element in the array.
If for example I use GET and ?group=k
How do I unset "k" in the array?
This is the array:
$groups_array = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','a2','b2','c2','d2','e2','f2');
I have tried
if(isset($_GET['group'])) {
unset($groups_array[1]);
$new_groupps_array = array_values($groups_array);
}
which works fine but where it shows [1] it needs to be a letter so I know how to unset it?
Hope you understand
many thanks
Example, if you wanted to delete 'a' value, you simply do:
$key = array_search('a', $groups_array); // search for key of my value
if($key !== false){
unset($groups_array[$key]);
}
Can you try this, You can use array_search function to retrieve the value based key and unset the array accordingly.
if(isset($_GET['group'])) {
$key = array_search ($_GET['group'], $groups_array);
unset($groups_array[$key ]);
$new_groupps_array = array_values($groups_array);
}

PHP - Unsetting array element in a foreach loop [duplicate]

This question already has answers here:
How do you remove an array element in a foreach loop?
(6 answers)
Closed 6 years ago.
Hi I am trying to remove an array element using a foreach loop, but it does nothing. I need the index to be completely gone rather than making it null. Here is what I have tried:
foreach ($_SESSION['cart'] as &$arrays3) {
if($arrays3['id'] == $id){
unset($arrays3);
}
}
Note, the array value for each key contains an associative array.
You need to use the key from your foreach, and unset the variable directly (from the session):
foreach ($_SESSION['cart'] as $key => $arrays3) {
if($arrays3['id'] == $id){
unset($_SESSION['cart'][$key]);
}
}
Unsetting $arrays3 or any of its children will only be effective until the next iteration of the foreach loop, when it will be set again.
You are using a dangerous form of the foreach loop. You MUST always unset the reference variable after the loop:
foreach ($_SESSION['cart'] as &$arrays3) {}
unset($arrays3);
Otherwise things will break if that loop is used again.
And the reference really is not needed. foreach operates on a copy of the array, so changes to the key or value will not go back into the original array, but you can always access the original array, as shown in the answer of #scrowler.

PHP - Nested associative array in a $_SESSION array [duplicate]

This question already has answers here:
Array as session variable
(4 answers)
Closed 9 years ago.
I was wondering is it possible to have an associative array within a session array? If so, what would be the best way to do it and how can I loop through it? I have tried the following but it doesnt seem to work:
//The variables are post variables from a form
$_SESSION['users'][$id] = array('name'=>$name, 'status'=>$status, 'salary'=>"20000");
Here is how I am trying to loop through the session array:
foreach ($_SESSION['users'] as $id=>$value) {
echo $value;
}
Also, If I knew an id how can I get the name? Can I do $_SESSION['users']['1234']['name']?
Yes you can have an associative array within a session array. You can also loop through it with a for or a foreach loop. e.g.:
$array = $_SESSION['users'][$id];
foreach($array as $key => $value) {
var_dump($array[$key]); //Will dump info about a single element
}
However, it would be helpful to see your error message or additional details of what you are trying to do and what about it that isn't working.
EDIT
Based on your updated question, since you are accessing and array of arrays (theoretically) you would need to nest a foreach with another foreach to get at your values.
foreach($_SESSION['users'] as $arrays) {
foreach($arrays as $arrKey => $arrVal) {
var_dump($arrays[$arrKey]);
}
}
Running that on your data would output (with my own fake data to fill variables):
string(7) "johndoe"
string(6) "active"
string(5) "20000"

Creating json from array, problem with loop [duplicate]

This question already has answers here:
Generate json string from multidimensional array data [duplicate]
(5 answers)
Closed 9 months ago.
I need help in how to remove the last comma from this looped var, please.
$image_meta .= "{\"id\":\"".$img_id."\",\"client\":\"".$img_desc."\",\"desc\":\"Test\"},";
FireFox doesn't seem to mind it, but IE does.
If there is any way to even get rid of the .= and loop my data in another way, i would be most thankful.
We would need to see the rest of the script.
But from what I understand of your situation, when you echo $image_meta (after the loop I suppose) you could do one of the two:
echo rtrim($image_meta,',');
or
echo substr($image_meta,0,-1);
You can do like this:
$ar_image_meta = array();
for/foreach() // your loop
{
$ar_image_meta[] = '{"id":"'.$img_id.'","client":"'.$img_desc.'","desc":"Test"}';
}
$image_meta = implode(", ", $ar_image_meta);
If your goal is to "convert" a PHP variable to a Javascript one, have a look at json_encode().
look at implode:
http://php.net/manual/en/function.implode.php
Just to clarify, I'm guessing your doing something similar to this:
$image_meta = '';
foreach($blahs as $blah){
$image_meta .= "{\"id\":\"".$img_id."\",\"client\":\"".$img_desc."\",\"desc\":\"Test\"},";
}
Something like this should work:
$image_meta_arr = array();
foreach($blahs as $blah){
array_push($image_meta, "{\"id\":\"".$img_id."\",\"client\":\"".$img_desc."\",\"desc\":\"Test\"}";
}
$image_meta = implode(',', $image_meta_arr);

Categories