Passing array reference to function PHP [duplicate] - php

This question already has answers here:
How to filter an array by a condition
(9 answers)
Closed 1 year ago.
I'm fairly new to PHP. I'm trying to filter out an array of items that contain a specific string. In order to then push those items to array, I need to pass the array to the function as a reference (or so my research has told me) however I can't seem to get it to work, any ideas? Thanks all.
$filteredS3Results = array();
function filterResults($var, &$filteredS3Results) {
if (strpos($var, '008-20160916') !== false) {
array_push($filteredS3Results, $var);
};
};
array_filter($s3Results,"filterResults");

The callback function just receives one argument, the array element being tested. It should return a boolean value that indicates whether the element should be included in the filtered result.
$filteredS3Results = array_filter($s3Results, "filterResults");
function filterResults($var) {
return strpos($var, '008-21060916') !== false;
}

Related

Calling recursive function from a function return only first value [duplicate]

This question already has answers here:
How to use return inside a recursive function in PHP
(4 answers)
Closed 9 months ago.
I have this recursive function that calls itself until the number reaches total. I have made this demo to show the working, actually the secondFunc() conatins database queries. I'm saving values in array that is passed to secondFunc() from firstFunc(). The problem is when i call first function it shows only 1 value i.e. 1. When i uncomment the var_dump in second func it shows all the values. I know i'm doing something wrong. Please point out my mistake .What is the problem here?
function firstFunc($total){
$array=array();
$num=0;
return secondFunc($total,$num,$array);
}
function secondFunc($total,$num,$array){
$num++;
$array[$num]=$num;
if($num<$total){
secondFunc($total,$num,$array);
}
//var_dump($array);
//exit();
return $array;
}
var_dump(firstFunc(5));
Demo http://codepad.viper-7.com/Bic8ce
When you call a function recursively you must make sure to propagate the recursive call's return value back to the original caller:
if($num<$total) {
return secondFunc($total,$num,$array); // return added
}

Associate return value to a function parameter (CALL BY REFERENCE) [duplicate]

This question already has answers here:
difference between call by value and call by reference in php and also $$ means?
(5 answers)
Closed 6 years ago.
I am wondering about returning value to a parameter instead of using =
example of "normal" way how to get return value
function dummy() {
return false;
}
$var = dummy();
But I am looking for a method how to associate return value without using =, like preg_match_all()
function dummy($return_here) {
return false;
}
dummy($var2);
var_dump($var2); //would output BOOLEAN(FALSE)
I am sure it has something to do with pointers but in preg_match_all() I never send any pointer to variable or am I mistaken?
preg_match_all('!\d+!', $data, $matches); // I am sending $matches here not &$matches
//I didn't know what to search for well it is simple CALL BY REFERENCE
It is CALL BY REFERENCE:
function dummy(&$var) { //main difference is in this line
$var = false;
}
dummy($var);
Use references
i.e.
function dummy(&$return_here) {
$return_here = false;
}
dummy($var2);
var_dump($var2);

is there an equalant to PHP array_key_exists in javascript or jquery [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Checking if an associative array key exists in Javascript
I have a PHP code block . For a purpose I am converting this to a JavaScript block.
I have PHP
if(array_key_exists($val['preferenceIDTmp'], $selected_pref_array[1]))
now I want to do this in jQuery. Is there any built in function to do this?
Note that objects (with named properties) and associative arrays are the same thing in javascript.
You can use hasOwnProperty to check if an object contains a given property:
o = new Object();
o.prop = 'exists'; // or o['prop'] = 'exists', this is equivalent
function changeO() {
o.newprop = o.prop;
delete o.prop;
}
o.hasOwnProperty('prop'); //returns true
changeO();
o.hasOwnProperty('prop'); //returns false
Alternatively, you can use:
if (prop in object)
The subtle difference is that the latter checks the prototype chain.
In Javascript....
if(nameofarray['preferenceIDTmp'] != undefined) {
// It exists
} else {
// Does not exist
}
http://phpjs.org/functions/array_key_exists:323
This is a great site for PHP programmers moving to js.

Recursive function: echo works, return doesn't [duplicate]

This question already has answers here:
How to use return inside a recursive function in PHP
(4 answers)
Closed 9 months ago.
The function aims at finding an item in a range of arrays, and then returning its key.
Problem is that the function doesn't return anything, whereas it would echo the expected result...
Here is my code:
function listArray($tb, $target){
foreach($tb as $key => $value){
if(is_array($value)){ // current value is an array to explore
$_SESSION['group'] = $key; // saving the key in case this array contains the searched item
listArray($value, $target);
}else {
if ($target == $value) { // current value is the matching item
return $_SESSION['group']; //Trying to return its key
break; // I'd like to close foreach as I don't need it anymore
}
}
}
}
By the way, an other little thing: I'm not used to recursive function, and I didn't find any other solution than using a session variable. But there might be a nicer way of doing it, as I don't use this session variable elsewhere...
You need a return before the recurring listArray call.
Thank about it ..
return;
break;
That break is never reached (I don't believe you can use break to exit a function in php anyway)
The second return returns from a recursive call. Let's say that this was not two separate functions:
function foon() {
barn();
}
function barn() {
return true;
}
foon has no return statement.
I finally bypassed the problem by storing my result in a $_SESSION variable.
So, no return anymore...
$_SESSION['item'][$target] = $_SESSION['group'];

Check if an array is multi-dimensional [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Checking if array is multidimensional or not?
How do I check whether an array is multidimensional or not in PHP?
Use count twice, one with single parameter, and one with recursive mode
if (count($myarray) == count($myarray, COUNT_RECURSIVE))
{
echo 'MyArray is not multidimensional';
}
else
{
echo 'MyArray is multidimensional';
}
count(array,mode)
array ---Required. Specifies the array or object to count.
mode ---Optional. Specifies the mode of the function. Possible values:
0 - Default. Does not detect multidimensional arrays (arrays within arrays)
1 - Detects multidimensional arrays
Note: This parameter was added in PHP 4.2
Multi-dimensional arrays in PHP are simply arrays containing arrays. So a simple function for this could be written as
function is_multidim_array($arr) {
if (!is_array($arr))
return false;
foreach ($arr as $elm) {
if (!is_array($elm))
return false;
}
return true;
}
This will run through every element of $arr and check whether it's an array. Should it encounter an element that is not an array, it will return false. Otherwise, return true.

Categories