pushing string into empty array gives warning - php

I created empty array and pushing val into it.
$var = array();
function printTag($tags) {
foreach($tags as $t) {
echo $t['token'] . "/" . $t['tag'] . " ";
if($t['tag'] == 'NN' OR $t['tag']== 'JJ'){
array_push($var, $t['token']) ;
}
}
echo "\n";
}
code looks fine for me but gives error:
array_push() expects parameter 1 to be array, null given in /var/www/html/postag/poscall.php on line 9
what is wrong here?
entire code:
<?php
// little helper function to print the results
include ("postag.php");
$var = array();
function printTag($tags) {
foreach($tags as $t) {
echo $t['token'] . "/" . $t['tag'] . " ";
if($t['tag'] == 'NN' OR $t['tag']== 'JJ'){
array_push($var, $t['token']) ;
}
}
echo "\n";
}
$tagger = new PosTagger('lexicon.txt');
$tags = $tagger->tag('The quick brown fox jumped over the lazy dog. this is really yummy and excellent pizza I have seen have really in love it it');
printTag($tags);
?>

Your $var = array(); statement is outside the function and out of the scope of the function. Put that inside the function and it'll remove the warning
function printTag($tags) {
$var = array();
foreach($tags as $t) {
echo $t['token'] . "/" . $t['tag'] . " ";
if($t['tag'] == 'NN' OR $t['tag']== 'JJ'){
array_push($var, $t['token']) ;
}
}
echo "\n";
}

The problem in your case is that $var is not in the scope of your function, so it gets implicitly declared as null (and this raises a notice too).
That said, this seems like a good case array_reduce():
$var = array_reduce($tags, function(&$result, $t) {
if (in_array($t['tag'], ['NN', 'JJ'])) {
$result[] = $t['token'];
}
// you could do some output here as well
return $result;
}, []);
It filters and maps at the same time and the return value is the array you want.
Alternatively, just declare $var inside the function and return it:
function printTag(array $tags)
{
$var = [];
foreach($tags as $t) {
// ...
}
return $var;
}
// ...
$var = printTag($tags);

Related

invoking closure within another closure?

PHP seems very inconsistent and (compiler) fails to generate the logic. Let's start our investigation, first with a simple set of JSON data.
[
{
"customer": "cust01",
"assembly": "assem01",
"date_received": "02-08-2015",
"time_received": "09:15",
"date_completed": "02-23-2015",
"time_completed": "10:27"
},
{
"customer": "lov_01",
"assembly": "lov_02",
"date_received": "lov_03",
"time_received": "lov_04",
"date_completed": "lov05",
"time_completed": "lov_06"
}
]
Then in the PHP, we retrieve an array of that data
$t_json_string = file_get_contents($t_json_file_path);
$t_json_arr = json_decode($t_json_string, true);
Assume we retrieve post values in an array like this
$t_new_entry = [
"customer" => "lov_01",
"assembly" => "lov_02",
"date_received" => "lov_03",
"time_received" => "lov_04",
"time_completed"=> "lov_05",
"time_completed"=> "lov_06"
];
and the goal is to verify as if new entry exists in the json array yet, by a condition whether both arrays have more than 2 similar values, for that I'm using $t_count to count the number of similar occurrences.
I wrote up 2 methods for checking that while passing the same data into the data pool.
// $t_boo = $db_entry_check($t_new_entry, $t_json_arr); echo $t_boo;
// true, $t_count shows 3.
$t_bool = $db_entry_exist($t_new_entry, $t_json_arr); echo $t_bool;
// False. It has to be true with the $t_count printed out at 3.
The first one employs call_user_function_array, which I tested and it works so I commented it out. Code for it here:
$db_entry_check = function($needle, $haystack){
$t_exist = 'false';
$t_count = 0;
function h_loop (&$t_count, $value, $array){
foreach ($array as $key => $val){
if (is_array($val)){
h_loop($t_count, $value, $val);
} else {
echo "<br/> --- value: ". $value. "<br/> --- val: ". $val . "<br/><br/>";
if ($val === $value){
$t_count += 1;
echo "<br/>" . $t_count . "<br/>";
continue;
}
}
}
}
function n_loop (&$t_count, $arr, $array){
foreach ($arr as $key => $value){
if (is_array($value)){
n_loop($t_count, $value, $array);
} else {
if ($t_count > 2) continue;
call_user_func_array('h_loop', [&$t_count, $value, $array]);
}
}
}
n_loop($t_count, $needle, $haystack);
echo "<br/>" . $t_count . "<br/>";
if ($t_count > 2) $t_exist = 'true';
return $t_exist;
};
The second one is my attempt to use lambdas on every component functions. I tried playing around putting $value, $array, and $t_count into use() part as those variables exist within the scope of $db_entry_exist for data binding & dependencies injection. When it comes to considering parameters (for the function) vs dependencies (for the use) Of the h_loop, I find it confusing, what an entire mess in PHP efforts to implement concepts of Javascript.
No matter what parameters I am passing onto the function part and no matter what variables got injected in the use() part. Many variations have been tested but none of them work. I usually get an error of 'Function name must be a string'. Invoking a closure within another closure in PHP seems not working as the logic in Javascript. It fails me whenever I tries to pass $h_loop($t_count, $value, $array); or echo $factorial(5); in the else part of the n_loop function. What I don't understand is that $db_entry_exist itself is a lambda (Closure as what PHP calls it) and n_loop function can be called inside without any error but calling/invoking a grandchild (h_loop) function by the same approach does not work, often resulting in the same error above.
$db_entry_exist = function($needle, $haystack){
$t_exist = 'false';
$t_count = 0;
// n_loop($t_count, $needle, $haystack);
$h_loop = function (&$t_count, $value, $array) use (&$h_loop) {
foreach ($array as $key => $val){
if (is_array($val)){
h_loop($t_count, $value, $val);
} else {
echo "<br/> --- value: ". $value. "<br/> --- val: ". $val . "<br/><br/>";
if ($val === $value){
$t_count += 1;
echo "<br/>" . $t_count . "<br/>";
continue;
}
}
}
};
$factoral = function($n) use (&$factoral) {
if ($n <= 1)
return 1;
else
return $n * $factoral($n - 1);
}; // source: https://gist.github.com/superic/8290704
$n_loop = function (&$t_count, $arr, $array) use (&$n_loop) {
foreach ($arr as $key => $value){
if (is_array($value)){
$n_loop($t_count, $value, $array);
} else {
if ($t_count > 2) continue;
$h_loop($t_count, $value, $array);
}
}
};
/*$n_loop = function ($arr, $array) use (&$n_loop, &$t_count){
// echo "<br/> --- nloop.t_count: " . $t_count . "<br/>";
foreach ($arr as $key => $value){
if (is_array($value)){
$n_loop($value);
} else {
if ($t_count > 2) continue;
// $h_loop($value, $array);
}
}
};*/
$n_loop($t_count, $needle, $haystack);
echo "<br/>" . $t_count . "<br/>";
if ($t_count > 2) $t_exist = 'true';
return $t_exist;
};
and here is the link to view my entire code:
<script src="http://ideone.com/e.js/YjLkZF" type="text/javascript" ></script>
To sum up, there are primarily 2 issues I don't understand and can't figure:
$n_loop is invoked fine within $db_entry_exist method but $h_loop isn't.
In the context of $db_entry_exist, how to pass and pass what variables to the function() and pass what as dependencies to the use() part.
$n_loop = function (&$t_count, $arr, $array) use (&$n_loop){}
// ------ OR ------- many other variations are there too.
$n_loop = function ($arr, $array) use (&$n_loop, &$t_count){}
Please investigate the code and let me know your thoughts. Thank you.
You have two misconceptions in your code that are affecting your understanding.
First: PHP does not actually have nested functions. When you say:
function outer()
{
function foo() {}
function bar() {}
}
what you are really saying is, when outer() is called, define foo() and bar() in the global scope. This means that once you call outer() once, anyone (not just outer()) can call foo() and bar(). This also means that calling outer() a second time results in a Cannot redeclare foo() error.
Second: Closures in PHP do not automatically close over any variables in their parent scope. Any variables intended to be part of the closure must be explicitly included in the use() list. This means that when you write:
$n_loop = function (&$t_count, $arr, $array) use (&$n_loop) {
//...
$h_loop($t_count, $value, $array);
//...
};
the call to $h_loop will always fail, because in the scope of that function, there is no variable named $h_loop. If you add $h_loop to your use() list, then you will be able to call it as expected.

How to echo variable outside of a function?

I have this code that echoes values from an array. I'd like to be able to echo the variable elsewhere in my code though. I've tried wrapping a function around it and echoing the function but I can't get anything to work. Here's the code that I have now...
function team(){
if ( ($items = field_get_items('entityform', $entityform, 'field_team_members')) ) {
$values = array_map($items, function($x) { return $x['value']; });
$comma_separated = implode(', ', $values);
foreach ($items as $item) {
$members = $prefix . $item['safe_value'];
$prefix = ', ';
echo $members;
}
}
}
If I simply do team(); it outputs nothing, as well as echo team();. What am I doing wrong here?
Your team function doesn't return anything which is why echo team(); doesn't print anything.
See the documentation on functions for more info: http://php.net/manual/en/functions.returning-values.php
function team(){
$output = '';
if ( ($items = field_get_items('entityform', $entityform, 'field_team_members')) ) {
$values = array_map($items, function($x) { return $x['value']; });
$comma_separated = implode(', ', $values);
$prefix = '';
$members = '';
foreach ($items as $item) {
$members .= $prefix . $item['safe_value'];
$prefix = ', ';
}
$output = $members;
}
return $output;
}
To clarify what i wrote as a comment, i just created a small script:
<?php
function team () {
$string = "hi";
return $string;
}
echo team();
Functions cant work unless until they are called, so you have to call team(); in some way, once called it will for sure display what ever you are trying to echo. Also above function will not display anything unless you force fully exit in that function. Or return some value.

PHP: Need an alternative to eval() for dynamically building multidimensional array

Okay, so I know that using eval() isn't great, but I haven't been able to come up with a better solution to my problem, and until recently, there wasn't a performance reason not to use it. However, I am now passing enough data to the function that it is taking unacceptably long.
The function that is being called is:
public static function makeAMultiDimensionalArrayWithSumsBasedOnMultipleFields($inArray, $dimensionFieldNames, $sumFieldNameArray, $staticFieldNameArray = array())
{
$outArray = array();
// Just in case the array has indices, sort it so array_pop works as expected.
ksort($dimensionFieldNames);
foreach ($inArray as $row)
{
// make sure each row in the inArray has all keys specified by $dimensionFieldNames
$allFieldsPresent = TRUE;
foreach ($dimensionFieldNames as $keyFieldName)
{
if (!array_key_exists($keyFieldName, $row))
{
// Note that alternatively we could set the field to a specified default value.
$allFieldsPresent = FALSE;
}
}
if ($allFieldsPresent)
{
$indexString = '';
$keyFieldNameArrayCopy = $dimensionFieldNames;
foreach ($dimensionFieldNames as $keyFieldName)
{
$indexString .= "['" . $row[$keyFieldName] . "']";
// lets sum values
foreach ($sumFieldNameArray as $sumFieldName)
{
eval ('$outArray' . $indexString . '[' . $sumFieldName . '] += $row[' . $sumFieldName . '];');
}
foreach ($staticFieldNameArray as $staticFieldName)
{
eval ('$outArray' . $indexString . '[' . $staticFieldName . '] = $row[' . $staticFieldName . '];');
}
}
}
}
return $outArray;
}
It is being called like this:
makeAMultiDimensionalArrayWithSumsBasedOnMultipleFields($data, $dimensionArray, $sumArray, $staticArray);
And the variables being passed to the function are similar to:
$dimensionArray = array("firstLevelID", "secondLevelID", "thirdLevelID", "fourthLevelID", "fifthLevelID");
$sumArray = array("revenue", "cost", "profit", "sales", "inquires", "cost", "walkins");
$staticArray = array("date", "storeID", "storeName", "productID", "productName", "managerID", "managerName", "salesperson");
So I want to rewrite the function so that I'm not using eval() any more. I've spent a considerable amount of time on this, and feel that it's time to seek some advice.
The goal is to take an array of arrays, and turn it into a multidimensional array based on the dimensions in the $dimensionArray.
I don't want to bore you with too many details right now, so please ask if you need more or have any other questions
Wow, okay. First time through I missed your indexing concatenation. Try this:
if ($allFieldsPresent) {
$keys = array();
foreach ($dimensionFieldNames as $keyFieldName) {
$keys[] = $row[$keyFieldName];
// lets sum values
foreach ($sumFieldNameArray as $sumFieldName)
self::deepAssign($outArray, $keys, $sumFieldName, $row[$sumFieldName], true);
foreach ($staticFieldNameArray as $staticFieldName)
self::deepAssign($outArray, $keys, $staticFieldName, $row[$staticFieldName]);
}
}
protected static function deepAssign(&$array, $keys, $fieldName, $value, $sum = false) {
$target =& $array;
foreach ($keys as $key) {
if (!isset($target[$key]))
$target[$key] = array();
$target =& $target[$key];
}
if($sum)
$target[$fieldName] += $value;
else
$target[$fieldName] = $value;
}
You should look at create_function() (docs here)

php foreach loop use variable in function name

I have a foreach loop that creates a function for each array item. I need a unique name for each function created and would like to include the current array item in the function name but don't know how to do this.
At the moment I have the function named as "function ttm_global__shortcode()" however I would like it to be "function ttm_global_$acf_field_shortcode()"
$acf_fields = array("telephone_number", "fax_number", "email_address", "skype");
foreach($acf_fields as $acf_field) {
function ttm_global__shortcode() {
ob_start();
echo get_field($acf_field, 'options');
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_shortcode($acf_field, 'ttm_global_'.$acf_field.'_shortcode');
}
Thanks
Using PHP 5.3 this can be done with anonymous functions
foreach($acf_fields as $acf_field) {
add_shortcode($acf_field, function() use($acl_field) {
// ...
});
}
If the second parameter of add_shortcode is a call back I think you can do it without creating such function.
$output = get_field($acf_field, 'options');
add_shortcode($acf_field, create_function('',"return '$output';"));
If you still want to create function.
Before PHP 5.3.0
Use create_function
foreach($acf_fields as $acf_field)
{
$funcs[$acf_field] = create_function('$acf_field', '
ob_start();
echo get_field($acf_field, 'options');
$output = ob_get_contents();
ob_end_clean();
return $output;'
);
....
}
After PHP 5.3.0
foreach($acf_fields as $acf_field)
{
$funcs[$acf_field] = function($acf_field){
ob_start();
echo get_field($acf_field, 'options');
$output = ob_get_contents();
ob_end_clean();
return $output;
};
....
}
Note: A parameter is added to your function.
You can use the create_function method to creates an anonymous function from the parameters passed, and returns a unique name for it.
<?php
function process($var1, $var2, $farr)
{
foreach ($farr as $f) {
echo $f($var1, $var2) . "\n";
}
}
// create a bunch of math functions
$f1 = 'if ($a >=0) {return "b*a^2 = ".$b*sqrt($a);} else {return false;}';
$f2 = "return \"min(b^2+a, a^2,b) = \".min(\$a*\$a+\$b,\$b*\$b+\$a);";
$f3 = 'if ($a > 0 && $b != 0) {return "ln(a)/b = ".log($a)/$b; } else { return false; }';
$farr = array(
create_function('$x,$y', 'return "some trig: ".(sin($x) + $x*cos($y));'),
create_function('$x,$y', 'return "a hypotenuse: ".sqrt($x*$x + $y*$y);'),
create_function('$a,$b', $f1),
create_function('$a,$b', $f2),
create_function('$a,$b', $f3)
);
echo "\nUsing the first array of anonymous functions\n";
echo "parameters: 2.3445, M_PI\n";
process(2.3445, M_PI, $farr);
// now make a bunch of string processing functions
$garr = array(
create_function('$b,$a', 'if (strncmp($a, $b, 3) == 0) return "** \"$a\" '.
'and \"$b\"\n** Look the same to me! (looking at the first 3 chars)";'),
create_function('$a,$b', '; return "CRCs: " . crc32($a) . ", ".crc32($b);'),
create_function('$a,$b', '; return "similar(a,b) = " . similar_text($a, $b, &$p) . "($p%)";')
);
echo "\nUsing the second array of anonymous functions\n";
process("Twas brilling and the slithy toves", "Twas the night", $garr);
?>
check out http://us.php.net/manual/en/function.create-function.php for more details.

PHP Array foreach question

I have a question about arrays and foreach.
If i have an array like this:
$test_arr = array();
$test_arr['name1'] = "an example sentence";
$test_arr['anything'] = "dsfasfasgsdfg";
$test_arr['code'] = "4334refwewe";
$test_arr['empty1'] = "";
$test_arr['3242'] = "";
how can I do a foreach and "pick" only the ones that have values? (in my array example, would only take the first 3 ones, name1, anything and code).
I tried with
foreach ($test_arr as $test) {
if (strlen($test >= 1)) {
echo $test . "<br>";
}
}
but it doesn't work. Without the "if" condition it works, but empty array values are taken into consideration and I don't want that (because I need to do a <br> after each value and I don't want a <br> if there is no value)
Sorry if I don't explain myself very well, I hope you understand my point. Shouldn't be too difficult I guess..
Thanks for your help !
Maybe will work
foreach ($test_arr as $test) {
if (strlen($test)!=="") {
echo $test . "<br>";
}
}
Your solution with corrected syntax:
foreach ($test_arr as $test) {
if (strlen($test)>=1) {
echo $test . "<br>";
}
}
Since empty strings are false, you could just do this (but you'd exclude 0's with the if):
foreach ($test_arr as $key => $val) {
if ($val) {
echo $val. "<br>";
}
}
If it has to be an empty string then (excluding 0 and FALSE):
foreach ($test_arr as $key => $val) {
// the extra = means that this will only return true for strings.
if ($val !== '' ) {
echo $val. "<br>";
}
}
Since it looks like you're using an associative array, you should be able to do this:
foreach( $test_arr as $key => $value )
{
if( $value != "" )
{
echo $value . "<br />";
}
}
As shown, you can test $value for an empty string directly. Since this is precisely the test you are trying to accomplish, I would hope that this would solve your problem perfectly.
On another note, this is pretty straight forward and should be very maintainable in the future when you've forgotten exactly what it was that you were doing!
You are better off to use a while loop like this:
while(list($test_key, $test_value) = each($test_arr))
{
if($test_value != "") { echo $test_value . "<br/>"; }
}
reset($test_arr);
If your array gets large, the while will be much faster. Even on small arrays, I have noticed a big difference in the execution time.
And if you really don't want the array key. You can just do this:
while(list(, $test_value) = each($test_arr))
{
if($test_value != "") { echo $test_value . "<br/>"; }
}
reset($test_arr);
You can check if the value is emtpy with empty().
Note that values like 0 or false are considered empty as well, so you might have to check for string length instead.
just a simple typing error:
foreach ($test_arr as $test) {
if (strlen($test) >= 1) {
echo $test . "<br>";
}
}
Try this:
foreach ($test_arr as $test) {
if (strlen($test) > 0) {
echo $test . "<br>";
}
}

Categories