I have a simple function:
function test(){
//some code
$X_has_val = $Y_has_val= array();
foreach ($A as $id => $row){
if(is_take($id)){
$X_has_val[$id] = $row;
}
}
foreach ($B as $id => $row){
if(is_take($id)){
$Y_has_val[$id] = $row;
}
}
//some code
}
I did this for get the equivalence
function test(){
//some code
$X_has_val = $Y_has_val= array();
foreach(array($A, $B) as $key=>$value){
foreach ($value as $id => $row){
if(is_take($id)){
$X_has_val[$id] = $row;
continue;
$Y_has_val[$id] = $row;
}
}
}
//some code
}
Looks like all you need is array_filter()
$X_has_cc = array_filter($A, 'isTake');
$Y_has_cc = array_filter($B, 'isTake');
like e.g. in
<?php
test();
function test() {
$A = array(1,2,3,4,5,6,7,8,9,10);
$B = array(99,100,101,102,103,104);
$X_has_cc = array_filter($A, 'isTake');
$Y_has_cc = array_filter($B, 'isTake');
var_dump($X_has_cc, $Y_has_cc);
}
// select "even elements"
function isTake($x) {
return 0==$x%2;
}
(and sorry, no, your approach doesn't make much sense ;-))
Related
$s = $conn->prepare($q);
$s->execute([$tagIdValue]);
$d = $s->fetchAll();
return $d;
function x($d) {
foreach ($d as $row) {
$val = $row["id"];
$cont = trimContent($row);
return $row;
}
}
i have a query , which returns all the values in the table and a function to convert it into an assosiate array. But only getting the first row in the array
Because ur using return inside loop, it only take first value and return it.
$s = $conn->prepare($q);
$s->execute([$tagIdValue]);
$d = $s->fetchAll();
return $d;
function x($d) {
$arr =[]
foreach ($d as $row) {
$val = $row["id"];
$cont = trimContent($row);
array_push($arr, $val)
}
return $arr;
}
Because, you are returning the result set in the loop.
So, only the first row is returned from the loop.
As, in the first iteration, return statement is encountered, so, that function will return and control will get out of the function.
And next iterations will not loop over the result set.
We need to create an array before loop,
append results to that array
and return the array.
Corrected Code:
$s = $conn->prepare($q);
$s->execute([$tagIdValue]);
$d = $s->fetchAll();
return $d;
function x($d) {
$array = array();
foreach ($d as $row) {
$val = $row["id"];
$cont = trimContent($row);
$array[] = $row;
}
return $array;
}
Instead writing a return inside a loop, replace that line with an array push or array shift function.
Once the loop has ended return the final result obtained from array_push/array_shift.
Put your return outside the loop so only it returns only the first loop
function x($d) {
foreach ($d as $row) {
$val = $row["id"];
$cont = trimContent($row);
return $row;
}
}
You need to return outside the loop :
$s = $conn->prepare($q);
$s->execute([$tagIdValue]);
$d = $s->fetchAll();
return $d;
function x($d) {
$arr =array();
foreach ($d as $k=> $row) {
$val = $row["id"];
$cont = trimContent($row);
$arr[$k] = $row;
}
return $arr;
}
I ran into this question on a different site, after attempting to work on it for an hour (could be my Sunday brains) I gave up. The question is: If there is a function foo:
function foo(){}
The function can be called as (arguments can be >= 2, where the last is always the value and the previous are part of the array).
So calling the function as:
foo('arg1', 'value');
Should result in:
$array['arg1'] = 'value';
The same if it has more than 1 argument:
foo('arg1', 'argx', 'argz', 'value');
Should produce:
$array['arg1']['argx']['argz'] = 'value';
This was my sad attempt:
function foo()
{
$items = func_get_args();
$value = array_pop($items);
$array = array_shift($items);
// Construct first element
$array = array($array => array());
foreach ($items as $el) {
insert_last($array, $value);
}
return $array;
}
function insert_last(&$array, $value)
{
$copy = $array;
while (true) {
$keys = array_keys($copy);
$last = $copy[$keys[count($copy)-1]];
var_dump($last);
if (empty($last)) {
$last = $value;
break;
}
$copy = $last;
}
var_dump($array, $copy);
}
Pretty sure there is probably an easier solution that I just can't think of at the moment. Thanks!
function foo()
{
$items = func_get_args();
$value = array_pop($items);
$array = [];
$arrayPtr = &$array;
foreach ($items as $element) {
$arrayPtr[$element] = null;
$arrayPtr = &$arrayPtr[$element];
}
$arrayPtr[$element] = $value;
return $array;
}
var_dump(foo('arg1', 'argx', 'argz', 'value'));
Demo
Via recursion using call_user_func_array()
<?php
function foo() {
$items = func_get_args();
if ( 1==count($items) ) {
return array_shift($items);
}
else {
$key = array_shift($items);
return array( $key=>call_user_func_array('foo', $items) );
}
}
var_dump(foo('arg1', 'argx', 'argz', 'value'));
edit: same thing without func_get_args() but using a variadic function
<?php
function foo(...$items) {
if ( 1==count($items) ) {
return array_shift($items);
}
else {
$key = array_shift($items);
return array( $key=>call_user_func_array('foo', $items) );
}
}
var_dump(foo('arg1', 'argx', 'argz', 'value'));
What about something like
function foo()
{
$args = func_get_args();
$items = array_pop($args);
foreach (array_reverse($args) as $item) {
$items = array($item => $items);
}
return $items;
}
var_dump(foo('arg1', 'argx', 'argz', 'value'));
Demo
I'm using Laravel 4 and I have the following problem. I make an Ajaxrequest and I have a class GpsController with function a called GPSOnline.
public function GPSOnline () {
$gpsonline=File::get( Config::get('app.driverspath'));
$fstep = explode("|", $gpsonline);
foreach($fstep as $k => $v){
if(strlen($v)>0){
$sstep [] = explode(",", $v);
}
}
$array = array();
foreach ($sstep as $v) {
$tmp['ch'] = substr($v[0],0,1);
$tmp['poz'] = substr($v[0],1);
$tmp['la'] = $v[1];
$tmp['lo'] = $v[2];
$array[] = $tmp;
}
// echo json_encode($array);
return Response::json(array($array));
}
But it didn't work.What's wrong in my code?!
I wasn't so clear in my first question, so i deleted it and here is a reformulation;
I have those arrays:
$open = array(array("FAI1","34"),array("FAI2","34"),array("FAI3","34"));
$click = array(array("FAI2","52"),array("FAI1","68"),array("FAI3","99"));
$unsubscribe = array(array("FAI2","103"),array("FAI3","67"),array("FAI1","102"));
$def_sent = array(array("FAI1","34",24),array("FAI2","34",23),array("FAI3","34",27));
$SB = array(array("FAI2","103"),array("FAI3","67"),array("FAI1","102"));
$HB = array(array("FAI2","103"),array("FAI3","67"),array("FAI1","102"));
I searched for a function to merge them and get a result like this:
$result = array(array("FAI1",34,68,102,34,24,102,102)
,array("FAI2","34",23.....),
array("FAI3","34",27....));
and to do this, i used the function, in the php online documentation, and this is the function
function array_merge_recursive() {
$arrays = func_get_args();
$base = array_shift($arrays);
foreach ($arrays as $array) {
reset($base);
while (list($key, $value) = #each($array)) {
if (is_array($value) && #is_array($base[$key])) {
$base[$key] = array_merge_recursive($base[$key], $value);
} else {
$base[$key] = $value;
}
}
}
return $base;
}
But instead of getting the result above i got this:
FAI1|34
FAI2|34
FAI3|34
FAI2|52
FAI1|68
FAI3|99
...
So i need some help to reformulate this function to get the expected result.
Try this function:
function array_merge_rec() {
$arrays = func_get_args();
$result = array();
foreach ($arrays as $arg) {
if (is_array($arg)) {
foreach ($arg as $item) {
if (!isset($result[$item[0]])) {
$result[$item[0]] = $item;
} else {
$result[$item[0]][] = $item[1];
}
}
} else {
echo "$arg skippend because it isn't array\n";
}
}
return array_values($result);
}
Does it help?
I have an array like this
$a=array(0=>1,1=>1,2=>5,3=>5,4=>10)
Now I want to find out the duplicate values and add those in to an array like this:
array_push($arrayOfones,$a['0'],$a['1'];
array_push($arrayOfFive,$a['2'],$a['5'];
You could take a look at array_count_values
$ret = array_count_values($a);
// get the duplicate values
$ret = array_filter($ret, function ($var) {
return $var > 1;
});
array_walk($ret, function(&$var, $key) {
$var = array_fill(0, $var, $key);
});
var_dump($ret); // $ret[1] is $arrayOfOnes, $ret[5] is $arrayOfFive
little simpler with no array functions other than count():
foreach($a as $key=>$value){
$ip[$value][] = $key;
}
foreach($ip as $key=>$inner_arr){
if(count($inner_arr) > 1)
$dup[$key] = $inner_arr ;
}
$a=array(0=>1,1=>1,2=>5,3=>5,4=>10);
$c=0;
foreach ($a as $key => $row) {
if (!isset($rs[$row])) {
$rs[$row][$key]= $key;
$c = 1;
$res[$row]['count'] = $c;
$res[$row]['values'][$key] = $key;
}
else {
$res[$row]['count']++;
$res[$row]['values'][$key] = $key;
}
}