I have a small script that sets multiple cookies, they all have this format item_1928 item_3847 item_5782 etc.
I need to get all the values for the cookies that start with item and store them in an array.
Here's is some code I found on SO but I'm not sure it's what I'm looking for. It just stores the key, but not the values:
$matches = array();
foreach($_COOKIE as $key => $value) {
if(substr($key, 0, 20) == 'wordpress_logged_in_') {
$matches[] = $key;
}
}
You can try this:
foreach($_COOKIE as $key => $value) {
if(strstr($key ,"item_")) {
$matches[$key] = $value;
}
}
You should be able to modify that code like this:
$matches = array();
$values = array();
foreach($_COOKIE as $key => $value) {
if(substr($key, 0, 20) == 'wordpress_logged_in_') {
$matches[] = $key;
$values[] = $_COOKIE[$key];
}
}
Then you'll have all the values (and NOT the keys) in the $values array.
Related
How can i know the variables ?
foreach($tree as $key => $value){
if (in_array($value['name'], $arr_folders)) {
${'id_'.$value['name']} = $value['id'];
}
//how to know variables?
$id_???
Currently I know the $value['name'] ie. it may be one,two, three, etc. but how to use them
echo $id_one;
I wanted to know here to split them in an array. So i can use
print_r($vars); which would result $id_one, $id_two, etc..
Something like this?
<?php
$array = [];
foreach($tree as $key => $value){
if (in_array($value['name'], $arr_folders)) {
$array[] = $value['id'];
}
}
print_r($array);
You can find variables by code:
foreach (get_defined_vars() as $var_name => $var_value) {
if(strpos($var_name, 'id_') ===0 ){
//it's your variable
}
}
But store variable in local scope look wrong.
May be better store to an other array:
$arIds = array();
foreach($tree as $key => $value){
if (in_array($value['name'], $arr_folders)) {
$arIds['id_'.$value['name']] = $value['id'];
}
}
If I have several post results that are like this:
$_POST["ResponseA"] = 1, $_POST["ResponseB"] = 1, $_POST["ResponseC"] = 2, $_POST["ResponseD"] = 3, $_POST["ResponseE"] = 1, etc.
How can I perform a loop that gets an array based upon the values? So If I'm checking for a value of 1, I get ResponseA, ResponseB, ResponseE ?
<?php
$results = array_keys($_POST, 1);
var_dump($results);
?>
Use array_flip() like this...
$flipped = array_flip($_POST);
echo $flipped['1']; // ResponseA
You will get issues doing this though as your values are not unique
simple build a loop
<?php
$fields = array('ResponseA','ResponseB','ResponseC','ResponseD','ResponseE')
function searchValue(array $fields, $value) {
$out = array();
foreach($fields as $name) {
if(isset($_POST[$name]) && $_POST[$name] == $value) $out[]=$name;
}
return $out;
}
var_dump(searchValue($fields,1));
You can loop through the $_POST:
foreach ($_POST as $key => $value) {
if ($value == 1) {
// $key will equal the value ResponseA if $_POST["ResponseA"] = 1
}
}
I found a way to search my multidimensional array and output the result and it works, however it only finds the first match and stops. If I have more than one match in the array I want to be able to show them all.
My array looks like this (the first layer of keys goes from 0, 1, 2 etc):
Array
(
[0] => Array
(
[mydevice] => blahblah
[ipadd] => 10.10.10.209
[portnum] => 16040
)
function searcharray($value, $key, $array) {
foreach ($array as $k => $val) {
if ($val[$key] == $value) {
return $k;
}
}
return null;
}
$myoutput = searcharray($ptn2, mydevice, $newresult);
I can then echo the results using something like $newresult[$myoutput][mydevice].
However if I have more than one entry in the array with a matching data in the 'mydevice' key it doesn't return them (just the first one).
That is because return breaks the function. You could use something like this:
function searcharray($value, $key, $array) {
$result = array();
foreach ($array as $k => $val) {
if ($val[$key] == $value) {
$result[] = $k;
}
}
return $result;
}
Now you will always get an array as result - empty if nothing was found. You can work with this like this e.g.
$mydevicekeys = searcharray($ptn2, "mydevice", $newresult);
foreach ($mydevicekeys as $mydevicekey) {
// work with $newresult[ $mydevicekey ]["mydevice"]
}
So add the results to an array :)
function searcharray($value, $key, $array) {
$res = array();
foreach ($array as $k => $val) {
if ($val[$key] == $value) {
$res[] = $key;
}
}
return $res;
}
I have two array.
1st array is $newarray = ('489289', '536516', '332833', '536516')
2nd array is
$rockin = array(
'489289' => array('536516','value1'),
'332833' => array('536516'),
);
I want to delete some value of $newarray.
Suppose we are looping from $newarray
Initially 489289 is assigned value.
I want to check whether the value associated to 489289 from $rockin array (i.e. value1 or 536516) also exist in $newarray.
If there is exist 'value1' or '536516' in $newarray then, delete 489289 from array!
So in above case 489289 would be deleted (from $newarray)
AS 536516 is associated value of 489289 in $rockin array AND 536516 also exist in $newarray
Till now I have tried this code
foreach ($newarray as $group_id) {
foreach ($rockin as $myfrcikingcl) {
foreach ($myfrickingcl as $myfrickingleader) {
if($group_id==$myfrickingleader)
{
unset($newarray[$group_id]);
}
}
}
}
This is what I understood you want to do:
$newarray = array('489289', '536516', '332833', '536516');
$rockin = array(
'489289' => array('536516','332833'),
'332833' => array('536516'),
);
foreach ($rockin as $array) {
foreach ($array as $value) {
if (in_array($value, $newarray)) {
$key = array_search($array, $rockin);
$newarray = array_diff($newarray, array($key));
}
}
}
foreach ($newarray as $k => $v) {
if(is_array($rockin[$v])){
foreach ($rockin[$v] as $key => $value) {
if(in_array($value, $newarray)){
unset($newarray[$k]);
}
}
}
}
You're using $group_id as a key, but it's a value. You have to unset by key, like this:
foreach ($i = 0; $i < count($newarray); $i++) {
foreach ($rockin as $myfrcikingcl) {
foreach ($myfrickingcl as $myfrickingleader) {
if ($newarray[$i] == $myfrickingleader) {
unset($newarray[$i]);
}
}
}
}
I am trying to build an array that is a-z, 0-9 and each one of those has a sub set of the same array.
IE:
array(
"a"=>array("a","b","c"..."0","1","2")
"b"=>array("a","b","c"..."0","1","2")
"c"=>array("a","b","c"..."0","1","2")
"d"=>array("a","b","c"..."0","1","2")
"0"=>array("a","b","c"..."0","1","2")
"1"=>array("a","b","c"..."0","1","2")
"2"=>array("a","b","c"..."0","1","2")
"3"=>array("a","b","c"..."0","1","2")
)
Where I am trying this
$finalArr = array();
$letterArr = range('a', 'z');
$numericArr = range(0,9);
$startArr = array_merge($letterArr, $numericArr);
foreach($startArr as $key => $val)
{
$finalArr[$val] = $startArr;
foreach($finalArr[$val] as $key2 => $val2)
{
$finalArr[$val][$val2] = $startArr;
}
}
But it only works on the first pass... after that it just starts making a mess. Any Idea's?
You're modifying the array as you loop over it; this is not a good practice, and in this case it's not even necessary.
That said, you can just use array_fill_keys() like this:
$arr = str_split('abcdefghijklmnopqrstuvwxyz0123456789');
$final = array_fill_keys($arr, $arr);
This is because you have used the same array name under inner loop
foreach($finalArr[$val] as $key2 => $val2)
{
$finalArr[$val][$val2] = $startArr;
}
Declare a new array $finalArr2 = array() and change the code to
foreach($finalArr[$val] as $key2 => $val2)
{
$finalArr2[$val][$val2] = $startArr;
}
It should work now.
To use your existing logic you need to unset($finalArr[$val][$key2]); to clear out the array you have temporally stored.
$finalArr = array();
$letterArr = range('a', 'z');
$numericArr = range(0,9);
$startArr = array_merge($letterArr, $numericArr);
foreach($startArr as $key => $val)
{
$finalArr[$val] = $startArr;
foreach($finalArr[$val] as $key2 => $val2)
{
unset($finalArr[$val][$key2]);
$finalArr[$val][$val2] = $startArr;
}
}
If you want some cleaner logic, this makes a lot more sense.
foreach($startArr as $val)
{
foreach($startArr as $val2)
{
$finalArr[$val][$val2] = $startArr;
}
}