When I query the data within the foreach loop it works, but makes a duplicate for each pass in the loop. I try to var_dump it anywhere else outside the loop and the data isn't there. Why won't my data persist outside the forEach loop?
<?php
$old_array = [10-2, 13=>"3452", 4=>"Green",
5=>"Green", 6=>"Blue", "green"=>"green",
"two"=>"green" ,"2"=>"green" , "rulebreak" =>"GrEeN",
"ninja"=>" Green ", ["blue" => "green", "green"=>"green", 2 => "itsGreen"] ];
$newArray = array();
function filter_Green($array) {
$find = "green";
$replace = "not green";
/* Same result as using str_replace on an array, but does so recursively for multi-dimensional arrays */
/* found here:
if (!is_array($array)) {
/* Used ireplace so that searches can be case insensitive */
return str_ireplace($find, $replace, $array);
}
foreach ($array as $key => $value) {
$newArray[$key] = $value;
if ($key == "green") {
$newArray[$key] = "not green";
}
if ($value == "green") {
$newArray[$value] = "not green";
}
}
return $newArray;
}
filter_Green($old_array);
var_dump($newArray);
?>
Expectation: When I run the function it should replace all instances of "green" with "not green" and save those into a $newArray. I have it returning $newArray but even then it doesn't seem to match up that the values are being saved into the newArray, hence why I'm doing var_dump to check if it's even working (it appears to not be)
Results: as it is setup, I get an empty array returned to me...It seems to work somewhat if I move var_dump($newArray) to within the foreach loop but that then duplicates the data for each pass.
if you want var_dump $newArray out side the function then you should declare $newArray as global in your function
<?php
$old_array = [10-2, 13=>"3452", 4=>"Green", 5=>"Green", 6=>"Blue", "green"=>"green", "two"=>"green" ,"2"=>"green" , "rulebreak" =>"GrEeN", "ninja"=>" Green ", ["blue" => "green", "green"=>"green", 2 => "itsGreen"] ];
$newArray = array();
function filter_Green($array) {
global $newArray;
$find = "green";
$replace = "not green";
/* Same result as using str_replace on an array, but does so recursively for multi-dimensional arrays */
if (!is_array($array)) {
/* Used ireplace so that searches can be case insensitive */
return str_ireplace($find, $replace, $array);
}
foreach ($array as $key => $value) {
$newArray[$key] = $value;
if ($key == "green") {
$newArray[$key] = "not green";
}
if ($value == "green") {
$newArray[$value] = "not green";
}
}
return $newArray;
}
filter_Green($old_array);
var_dump($newArray);
?>
But instead of declaring global in function, use returned value by filter_Green($old_array); as below
$result = filter_Green($old_array);
var_dump($result);
Related
Each string value within my $arr array contains 2 preceeding white spaces. I would like to remove these spaces using the trim() method. Furthermore, I would like to do the same for any arrays within the $arr array assuming an infinite number of arrays within arrays. I attempted to do this recursively with no success.
Although there might be a built in php method to do this for me, I am learning, and would like to know why this code block doesn't work as well as what altercation can be made to fix it.
$arr = array(" one", " two", array(" three"));
function trimAll(&$array) {
foreach($array as $key => $value) {
if(gettype($value) !== "array") {
$array[$key] = trim($value);
} else {
trimAll($value);
}
}
}
trimAll($arr);
echo $arr[0];//"one" (worked)
echo $arr[1];//"two" (worked)
echo $arr[2][0];// " three"(didn't work)
The best/simplest function to call in this case is: array_walk_recursive(). There is no need to reinvent a function that php has already designed for just this purpose. It only visits "leaf nodes" so you don't need to check if it is processing an array-type element.
You merely need to modify the elements by reference (using & in the anonymous function parameter). I'll demo ltrim() since your input strings only have leading spaces, but you can use trim() to handle spaces on both sides of the string.
Code: (Demo) (PHP7.4 and higher version)
array_walk_recursive(
$arr,
function(&$v) {
$v = ltrim($v);
}
);
Output:
array (
0 => 'one',
1 => 'two',
2 =>
array (
0 => 'three',
),
)
As for your custom function, it could be written like this to provide a successful result:
function trimAll(&$array) { // modify the original input array by reference
foreach ($array as &$value) { // modify each value by reference
if (!is_array($value)) { // if it is not an array
$value = trim($value); // trim the string
} else {
trimAll($value); // recurse
}
}
}
trimAll($arr); // modify the array (no return value from function call)
var_export($arr); // print the array
You see, the reason your subarray element is not being affected is because there is no assignment occuring between $value and trimAll($value). The way you have set up trimAll, it does not return a value. So, even if you used:
} else {
$array[$key] = trimAll($value);
}
You would find that $array[$key] would be replaced by NULL. The solution is to make $value modifiable by reference by using &$value in the foreach loop.
And as if this answer wasn't long enough already, here is a way to reconfigure your function so that it returns the modified array instead of modifying the input array with no return.
function trimAll($array){ // input array is not modifiable
foreach ($array as &$value) { // $value IS modifiable
if (is_array($value)) { // if an array...
$value = trimAll($value); // assign recursion result to $value
} else { // not an array...
$value = trim($value); // trim the string
}
}
return $array; // return the new trimmed array
}
var_export(trimAll($arr)); // now trimAll() can be directly printed to screen
or if you wish to avoid modifying by reference entirely:
function trimAll($array) {
foreach ($array as $key => $value) {
if (is_array($value)) {
$array[$key] = trimAll($value);
} else {
$array[$key] = trim($value);
}
}
return $array;
}
var_export(trimAll($arr));
Yes, there is a built-in PHP function which solves your problem. It is array_map()
But in your situation, you might need to do something like this:
Solution 1:
$input = array(" one", " two", array(" three"));
function removeSpaces($object)
{
if (is_array($object)) {
$object = array_map('trim', $object);
} else {
$object = trim($object);
}
return $object;
}
$output = array_map('removeSpaces', $input);
Solution 2:
$input = array(" one", " two", array(" three"));
function array_map_recursive(callable $func, array $array) {
return filter_var($array, \FILTER_CALLBACK, ['options' => $func]);
}
$output = array_map_recursive('trim', $input);
Cheers.
Simple method,
$arr = array(" one", " two", array(" three"));
function trimAll(&$array) {
foreach($array as $key => $value) {
if(gettype($value) !== "array") {
$array[$key] = trim($value);
} else {
$array[$key] = trimAll($value);
}
}
return $array;
}
trimAll($arr);
The following code uses foreach on an array and if the value is an array it does a for each on the nested array
foreach ($playfull as $a)
{
if (is_array($a))
{
foreach ($a as $b)
{
print($b);
print("<p>");
}
} else {
print($a);
print("<p>");
}
}
This only works if you know that the arrays may only be nested one level deep
If arrays could be nested an unknown number of levels deep how do you achieve the same result? (The desired result being to print the value of every key in every array no matter how deeply nested they are)
You can use array_walk_recursive. Example:
array_walk_recursive($array, function (&$val)
{
print($val);
}
This function is a PHP built in function and it is short.
Use recursive functions (that are functions calling themselves):
function print_array_recursively($a)
{
foreach ($a as $el)
{
if (is_array($el))
{
print_array_recursively($el);
}
else
{
print($el);
}
}
}
This is the way, print_r could do it (see comments).
You want to use recursion, you want to call your printing function in itself, whenever you find an array, click here to see an example
$myArray = array(
"foo",
"bar",
"children" => array(
"biz",
"baz"),
"grandchildren" => array(
"bang" => array(
"pow",
"wow")));
function print_array($playfull)
{
foreach ($playfull as $a)
{
if (is_array($a))
{
print_array($a);
} else {
echo $a;
echo "<p>";
}
}
}
echo "Print Array\n";
print_array($myArray);
You could use a recursive function, but the max depth will be determined by the maximum nesting limit (see this SO question, Increasing nesting functions calls limit, for details about increasing that if you need it)
Here's an example:
$array = array(1,array(2,3,array(4,5)),6,7,8);
function printArray($item)
{
foreach ($item as $a)
{
if (is_array($a))
{
printArray($a);
} else {
print($a);
print("<p>");
}
}
}
printArray($array);
I hope that helps.
Try this -
function array_iterate($arr, $level=0, $maxLevel=0)
{
if (is_array($arr))
{
// unnecessary for this conditional to enclose
// the foreach loop
if ($maxLevel < ++$level)
{ $maxLevel = $level; }
foreach($arr AS $k => $v)
{
// for this to work, the result must be stored
// back into $maxLevel
// FOR TESTING ONLY:
echo("<br>|k=$k|v=$v|level=$level|maxLevel=$maxLevel|");
$maxLevel= array_iterate($v, $level, $maxLevel);
}
$level--;
}
// the conditional that was here caused all kinds
// of problems. so i got rid of it
return($maxLevel);
}
$array[] = 'hi';
$array[] = 'there';
$array[] = 'how';
$array['blobone'][] = 'how';
$array['blobone'][] = 'are';
$array['blobone'][] = 'you';
$array[] = 'this';
$array['this'][] = 'is';
$array['this']['is'][] = 'five';
$array['this']['is']['five'][] = 'levels';
$array['this']['is']['five']['levels'] = 'deep';
$array[] = 'the';
$array[] = 'here';
$var = array_iterate($array);
echo("<br><br><pre>$var");
I am trying to search two arrays and return the index of matching words that match in array 1 from array 2. following are the arrays:
$array1 = array('hello how are you', 'hello I am fine');
$array2 = array('hello','how');
I am trying the following code and it return 0,1 which is fine. But i only want to return 0. I want it to return only where both words are present in the array.
foreach ($array1 as $reference => $array) {
foreach($array2 as $key => $word) {
if(strpos($array, $word) !== false) {
echo $reference, PHP_EOL;
break;
}
}
}
You need to keep track of each entity from $array2 being checked against each entity from $array1 then compare after the inner loop to decide whether all elements from $array2 are present in $array1. Here's an example:
foreach($array1 as $reference => $array) {
$contains = 0;
foreach($array2 as $key => $word) {
if(strpos($array, $word) !== false) {
$contains++;
} else {
// for performance reasons, e.g. if you have a large array,
// you should break the loop here if the word isn't in the
// original array
break;
}
}
if($contains == count($array2)) {
// $array contains all words from $array2
echo $reference . PHP_EOL;
} else {
// $array doesn't contain all the words
}
}
I'm trying to find a part of a string in a multidimentional array.
foreach ($invitees as $invitee) {
if (in_array($invitee, $result)){
echo 'YES';
} else {
echo 'NO';
}
}
the $invitees array has 2 elements:
and $result is what I get from my Drupal database using db_select()
What I'm trying to do is, if the first part from one of the emails in $invitees is in $result it should echo "YES". (the part before the "#" charather)
For example:
"test.email" is in $result, so => YES
"user.one" is not in $result, so => NO
How do i do this? How can I search for a part of a string in a multidimentional array?
Sidenote: I noticed that the array I get from Drupal ($result) has 2 "Objects" which contain a "String", and not arrays like I would expect.
For example:
$test = array('red', 'green', array('apple', 'banana'));
Difference between $result and $test:
Does this have any effect on how I should search for my string?
Because $result is an array of objects, you'll need to use a method to access the value and compare it. So, for instance you could do:
//1: create new array $results from array of objects in $result
foreach ($result as $r) {
$results[] = get_object_vars($r);
}
//2: expanded, recursive in_array function for use with multidimensional arrays
function in_array_r($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}
//3: check each element of the $invitees array
foreach ($invitees as $invitee) {
echo in_array_r($invitee, $results) ? "Yes" : "No";
}
Also, for some illumination, check out this answer.
You can search through the array using preg_grep, and use a wildcard for anything before and after it. If it returns a value (or values), use key to get the index of the first one. Then do a check if its greater than or equal to 0, which means it found a match :)
<?php
$array = array('test1#gdfgfdg.com', 'test2#dgdgfdg.com', 'test3#dfgfdgdfg');
$invitee = 'test2';
$result = key(preg_grep('/^.*'.$invitee.'.*/', $array));
if ($result >= 0) {
echo 'YES';
} else {
echo 'NO';
}
?>
Accepted larsAnders's answer since he pointed me in to direction of recursive functions.
This is what I ended up using (bases on his answer):
function Array_search($array, $string) {
foreach ($array as $key => $value) {
if (is_array($value)) {
Array_search($array[$key], $string);
} else {
if ($value->data == $string) {
return TRUE;
}
}
}
return FALSE;
}
array_walk_recursive($arr, function(&$val, $key){
if($val == 'smth'){
unset($val); // <- not working, unset($key) doesn't either
$var = null; // <- setting it to null works
}
});
print_r($arr);
I don't want it to be null, I want the element out of the array completely. Is this even possible with array_walk_recursive?
You can't use array_walk_recursive here but you can write your own function. It's easy:
function array_unset_recursive(&$array, $remove) {
$remove = (array)$remove;
foreach ($array as $key => &$value) {
if (in_array($value, $remove)) {
unset($array[$key]);
} elseif (is_array($value)) {
array_unset_recursive($value, $remove);
}
}
}
And usage:
array_unset_recursive($arr, 'smth');
or remove several values:
array_unset_recursive($arr, ['smth', 51]);
unset($val) will only remove the local $val variable.
There is no (sane) way how you can remove an element from the array inside array_walk_recursive. You probably will have to write a custom recursive function to do so.
The answer of #dfsq is correct but this function doesn't remove empty array. So you can end up with Tree of empty array, which is not what is expected in most cases.
I used this modified function instead:
public function array_unset_recursive(&$array, $remove) {
foreach ($array as $key => &$value) {
if (is_array($value)) {
$arraySize = $this->array_unset_recursive($value, $remove);
if (!$arraySize) {
unset($array[$key]);
}
} else if (in_array($key, $remove, true)){
unset($array[$key]);
}
}
return count($array);
}