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);
}
Related
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;
}
This question already has answers here:
PHP Remove elements from associative array
(9 answers)
Closed 5 years ago.
The title says it all. I want to delete the highlighted section in yellow as shown in picture below. And rest remain unchanged. What is the best way to do it? Is there a method that does't use foreach?
you can do this just with one foreach!
foreach ($data as $key => $subArr) {
unset($subArr['id']);
$data[$key] = $subArr;
}
You can use the following
$filteredArray = array_map(function($array) {
unset($array['id']);
return $array;
}, $dataArray);
Instead of doing foreach() loop on the array, You can go with array_search()
$results=array_search($unwantedValue,$array,true);
if($results !== false) {
unset($array[$result]);
}
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
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"
This question already has answers here:
PHP Recursively unset array keys if match
(11 answers)
Closed 9 years ago.
Say I have an array that looks somewhat like this: array("a"=>array("a"=>"b"), "b"=>array("a"=>"d")).
I would like to unset all vars with the key "a" within the array and it sub-arrays. Assume that the data's structure is unknown. All I want is that if the key "a" exists somewhere within the parent array or it sons - it'll be unset. Is it possible?
function unsetKey (&$array, $key) {
foreach ($array as $k => $v)
if (is_array($v))
unsetKey($array[$k], $key);
if (isset($array[$key])) unset(array[$key]);
}
That should do it.