I'm trying to use filter_var_array() and FILTER_CALLBACK to format some numbers, I thought this would work, but it does not:
$item_arr = filter_var_array($item_arr, array(
'item_number' => array(
'filter' => FILTER_CALLBACK,
'options' => array($this, 'number_format')
)
) );
though this does work:
$item_arr = filter_var_array($item_arr, array(
'item_number' => array(
'filter' => FILTER_CALLBACK,
'options' => function( $num ){
return number_format( $num );
}
)
) );
What's the difference between these two? What's the point of assigning an array() to options?
In the first example you are trying to create a callback for $this->number_format, but I guess you want the global function number_format instead. If you passing a function (unlike an object method) as callback just the function name as a string should getting passed, like this:
$item_arr = filter_var_array($item_arr, array(
'item_number' => array(
'filter' => FILTER_CALLBACK,
'options' => 'number_format'
)
));
Check the documentation page about callbacks to get more information.
If you want to format an array of numbers, the function array_walk() seems fitting better:
array_walk($item_arr, 'number_format');
Related
I cannot for the life of me figure out how to search through a multidimensional array for a key => value pair, and then either A) Return the array it belongs to or a different specific key => value pair that exists in the same array.
My array is:
$pages = array(
array(
'pageId' => 10,
'title' => 'Welcome',
'theme' => 'basic'
),
array(
'pageId' => 11,
'title' => 'Home',
'theme' => 'basic'
),
array(
'pageId' => 12,
'title' => 'Login',
'theme' => 'basic'
)
);
I've tried
$theme = array_search(10, array_column($search, 'pageId'));
but it keeps returning an int and not the value basic as I am wanting.
I would like either just the value or an array with the key => value pair or the whole array it belongs to.
Try this simplest one, hope this will be helpful. Here we are using array_column
Try this code snippet here
$result=array_column($pages,"theme" ,'pageId');
if(isset($result[$toSearch]))
{
echo $result[$toSearch];
}
You can use array_filter, live demo.
array_filter($array, function($v) use($searchKey, $searchValue) {
return $v[$searchKey] == $searchValue;
});
I am doing Jira REST API calls and I am wondering how I can dynamically add more than one component to the components field using REST API in PHP. I have the following code, works when I set it static, but not sure how to do it dynamically.
Example of static component set:
$data = array(
'fields' => array(
'project' => array(
'key' => $rowAnswers["Key"]
),
'summary' => $rowAnswers["Summary"],
'description' => $rowAnswers["Description"],
'issuetype' => array(
'name' => $rowAnswers["IssueType"]
),
'components' => array(
array(
"name" => "component1"
),
array(
"name" => "component2"
)
)
),
);
My array that I want to replace the static content with:
$components = explode(",", $rowAnswers["Components"]);
$arr = array();
foreach($components as $value){
$array = array("name"=>$value);
array_push($arr,$array);
}
Replacing
'components' => array(
array(
"name" => "component1"
),
array(
"name" => "component2"
)
)
with
'components' => [
$arr
]
doesn't work, I get:
"{"error":false,"error_msg":"","data":"{\"errorMessages\":[],\"errors\":{\"components\":\"expected Object\"}}"}"
I see on an api call to get a request it looks like this:
[components] => Array
(
[0] => stdClass Object
(
[name] => component1
)
[1] => stdClass Object
(
[name] => component2
)
)
But I am unsure how to transform an array into this type of object or request in PHP. Calling with PHP-cURL and json_encoding the data it sends.
Thanks in advance!
you need to decode your json as associative array by setting the second parameter to true
check out the json_decode
assoc
When TRUE, returned objects will be converted into associative arrays.
To fix this I had to do the following:
When creating the array from the DB:
$components = explode(",", $rowAnswers["Components"]);
$arr = array();
foreach($components as $value){
$array = json_decode(json_encode(array("name"=>$value)), FALSE);
array_push($arr,$array);
}
Then to set the component in the request:
'components' => $arr
Thanks
I'm trying to get the number of users with the same email address.
I've followed the php documentation for MongoCollection Aggregate. I've also compared my query to the others on stackoverflow. As far as I can tell my pipeline matches the one in the examples. And yet I get the error:
exception: pipeline element 0 is not an object
Which I presume is the $group element, which is an object (once converted to the json format mongo uses, isn't it?
$pipeline = array(
array(
'$group' => array(
'_id' => array( 'email' => '$email' ),
'uniqueIds' => array( '$addToSet' => '$_id' ),
'count' => array( '$sum' => 1 )
)
),
array(
'$match' => array(
'count' => array( '$gt' => 1 )
)
)
);
$options = array(
'allowDiskUse' => true
);
$results = $mongo->users_collection->aggregate( $pipeline, $options );
What have I done wrong? I've even tried running the query from php.net, I of course expected no results, but it resulted in the same error!?
EDIT so turns out it's when I allowDiskUse, but if I remove the $options from the function I get an error telling me to use it.
Thanks
I found a solution to this
$results = $mongo->command(array(
'aggregate' => 'users_collection',
'pipeline' => $pipeline,
'allowDiskUse' => true
));
It seems to me that because php allows two methods of doing this (from php.net)
public array MongoCollection::aggregate ( array $pipeline [, array $options ] )
public array MongoCollection::aggregate ( array $op [, array $op [, array $... ]] )
It is seeing my $options array as another operation $op. Maybe this is to do with the version of Mongo Module we're using or something along those lines, but changing to $mongo->command() works as a workaround.
im currently trying to generate a dynamic subnavigation. Therefor i pull data off of the database and store some of it an an array. Now i want to do an foreach in this array. Well as far as I know, this isn't possible.
But maybe I'm wrong. I would like to know if it would be possible, and if so how do i do it?
This is my code, which wont work since it hands out an syntax error.
$this->subnav = array(
'' => array(
'Test Link' => 'login.php',
'Badged Link' => array('warning',10023,'check.php')
),
'benutzer' => array(
'Benutzer suchen' => '/mother/index.php?page=benutzer&subpage=serach_user',
'Benutzer hinzufügen' => '/mother/index.php?page=benutzer&subpage=add_user',
'Rechtevergabe' => '/mother/index.php?page=benutzer&subpage=user_rights'
),
'logout' => array(
'Login' => '/mother/login.php',
'Logout' => '/mother/index.php?page=logout'
),
'datenbank' => array(
(foreach($this->system->get_databases() as $db){array($db->name => $db->url)}),
'Deutschland' => '/mother/login.php',
'Polen' => '/mother/index.php',
'Spanien' => '/mother/index.php',
'Datenbank hinzufügen' => '/mother/index.php?page=datenbank&subpage=add_database'
)
);
}
You can't place foreach loop inside an array like this. You can do something like this though.
foreach($this->system->get_databases() as $db)
{
$this->subnav['datenbank'][$db->name] = $db->url;
}
This is not possible. but you can do it in other way like you can place the foreach outsite and top of this array and assign to an array and then you can use that array variable.
e.g.
$arrDB = array();
foreach($this->system->get_databases() as $db) {
$arrDB[$db->name] = $db->url;
}
Now assign it to:
'datenbank' => $arrDB
I have array like this:
array(
'person0' => array( 'name'=>'name0','address'=>'address0' ),
'person1' => array( 'name'=>'name1','address'=>'address1' ),
'person2' => array( 'name'=>'name2','address'=>'address2' )
);
I want to change it like this. (just append a new value in each sub-array)
array(
'person0' => array( 'name'=>'name0','address'=>'address0','type'=>'type0' ),
'person1' => array( 'name'=>'name1','address'=>'address1','type'=>'type1' ),
'person2' => array( 'name'=>'name2','address'=>'address2','type'=>'type2' )
);
Is there any related function in php to perform this action? What is the shortest way to do this. Is it possible without loop?
Thanks
Browse the PHP manual when you wonder if a function exists to do something... it probably does.
http://www.php.net/manual/en/function.array-walk.php
http://php.net/manual/en/function.array-map.php
I'd just write the loop, but you can use those functions if you don't want to.