I am using php 5.2.10 i want to do the array_map on the array and i created a function for array mapping
function get_result(){
$result = mysql_query("Select * from table");
while($cr = mysql_fetch_array($result)){
$b = array_map(`calc`,$cr);
$rr_id = $cr['batch_id'].$cr['seq_id'];
$mqrrid = '999'.$rr_id;
$question_id = $cr['question_id'];
foreach ($b as $k => $v){
if(preg_match('{^Item \d+$}',$k)){
$new_insert[] = array(
'r_id'=>$mqrrid,
'q_id' =>$q_id,
'c_id' =>$k,
'rank'=>$v
);
}
}
}
}
function calc($n){
foreach($n as $m=> &$x) {
if (preg_match('{^Item \d+$}', $m)) {
if($x == null){
$x = $x;
}else {
$x = $x - 1;
}
}
}
return $n;
}
I don't know why I cannot call the function calc in array_map.....I cannot figure out the reason.....
Can anyone help me ?
original array :( actually the output after the array_map(calc,$cr) are same as follow)
array(23) {
["batch_id"]=>
string(1) "1"
["seq_id"]=>
string(1) "1"
["question_id"]=>
string(4) "2086"
["Item 1"]=>
string(1) "1"
["Item 2"]=>
string(1) "2"
["Item 3"]=>
string(1) "3"
["Item 4"]=>
string(1) "4"
["Item 5"]=>
string(1) "5"
["Item 6"]=>
NULL
what i need is : (minus the value of Item 1 to 6 by 1, if its null just leave it ~)
array(23) {
["batch_id"]=>
string(1) "1"
["seq_id"]=>
string(1) "1"
["q_id"]=>
string(4) "2086"
["Item 1"]=>
string(1) "0"
["Item 2"]=>
string(1) "1"
["Item 3"]=>
string(1) "2"
["Item 4"]=>
string(1) "3"
["Item 5"]=>
string(1) "4"
["Item 6"]=>
NULL
Finally, the result will become like this:(example of Item 1 and Item 6)
array(4) {
["r_id"]=>
string(5) "99911"
["q_id"]=>
string(4) "2086"
["c_id"]=>
string(6) "Item 1"
["rank"]=>
string(1) "0"
}
array(4) {
["r_id"]=>
string(5) "99916"
["q_id"]=>
string(4) "2086"
["c_id"]=>
string(6) "Item 6"
["rank"]=>
string(4) NULL
}
calc should be global, otherwise it cannot be found. Also, you should pass a string (no ` but rather enclose in ' or ").
Additionally, in general (if you used PHP 5.3), it is better to pass a function reference to the array_map function, instead of a string:
$func = function calc() { ... }
array_map($func, $cr);
I think you don't have to prepare the function for array_map.
function get_result($link_identifier = NULL) {
$result = mysql_query('Select * from table', $link_identifier);
$new = array();
while ($rows = mysql_fetch_assoc($result)) {
$r_id = '999' . $rows['batch_id'] . $rows['seq_id'];
foreach ($rows as $k => $v) {
if ($v !== null && preg_match('#^Item \\d+$#', $k)) {
$v = (string)((int)$v + 1);
}
$new[] = array(
'r_id' => $r_id,
'q_id' => $rows['question_id'],
'c_id' => $k,
'rank' => $v,
);
}
}
return $new;
}
Related
So I have a variable $offices['results'] when var dumped will output like this:
array() {
[0]=>
object(stdClass)#16067 (24) {
["id"]=>
string(1) "4"
["blog_id"]=>
string(2) "10"
["office_name"]=>
string(0) "Japan"
}
[1]=>
object(stdClass)#16064 (24) {
["id"]=>
string(1) "5"
["blog_id"]=>
string(2) "11"
["office_name"]=>
string(0) "USA"
}
[2]=>
object(stdClass)#16064 (24) {
["id"]=>
string(1) "5"
["blog_id"]=>
string(2) "12"
["office_name"]=>
string(0) "USA"
}
}
I only want to create a new array variable where the blog_id is 10 and 12, which will return:
array() {
[0]=>
object(stdClass)#16067 (24) {
["id"]=>
string(1) "4"
["blog_id"]=>
string(2) "10"
["office_name"]=>
string(0) "Japan"
}
[1]=>
object(stdClass)#16064 (24) {
["id"]=>
string(1) "5"
["blog_id"]=>
string(2) "12"
["office_name"]=>
string(0) "USA"
}
}
I tried array_filter but I cant make it work.
$array = $offices['results'];
$like = '11','12';
$result = array_filter($array, function ($item) use ($like) {
if (stripos($item['blog_id'], $like) !== false) {
return true;
}
return false;
});
var_dump($result);
I hope you can help me. Thanks
You could iterate through this array and check if the blog_id exists or not
but first, let's assign an array with your blog_id you want
$blogs = [10, 12];
after that, you can start iterate through your data
$result = [];
foreach($data as $blog){
if(in_array($blog->blog_id, $blogs)){
$result[] = $blog;
}
}
Not tested but how about this way with array_filter()?
$filterBy = [10,12];
$array = $offices['results'];
$newArray = array_filter($array, function ($var) use ($filterBy) {
return in_array($var->blog_id,$filterBy);
});
This may be a basic question but I am struggling with a way to count values without doing multiple foreach loops. I have this array of objects (partial list included):
array(51) {
[0]=>
object(stdClass)#971 (4) {
["hole"]=>
string(1) "2"
["club"]=>
string(1) "6"
["shot_type"]=>
string(1) "6"
["shot_loc"]=>
string(1) "6"
}
[1]=>
object(stdClass)#970 (4) {
["hole"]=>
string(1) "2"
["club"]=>
string(2) "16"
["shot_type"]=>
string(1) "8"
["shot_loc"]=>
string(1) "1"
}
[2]=>
object(stdClass)#969 (4) {
["hole"]=>
string(1) "2"
["club"]=>
string(2) "19"
["shot_type"]=>
string(1) "3"
["shot_loc"]=>
string(1) "2"
}
[3]=>
object(stdClass)#968 (4) {
["hole"]=>
string(1) "1"
["club"]=>
string(1) "1"
["shot_type"]=>
string(1) "6"
["shot_loc"]=>
string(1) "6"
}
[4]=>
object(stdClass)#967 (4) {
["hole"]=>
string(1) "1"
["club"]=>
string(2) "15"
["shot_type"]=>
string(1) "5"
["shot_loc"]=>
string(1) "3"
}
The number of objects in the list vary but each object will have the key=>values as shown. What I would like to return is an array with count of each of the values of "hole". Something like this:
`array(18) {
[1]=> 4
[2]=> 5
[3]=> 6`
and so on where the key is each of the values of "hole" and the new array value is the count.
I have attempted forms of count, count(get_object_vars($)), and others but all examples I find are counting the objects. Thanks in advance.
Your question is a bit confusing, but this should do the trick for you:
$holes = [];
foreach ($array as $object) {
if (isset($object->hole)) {
$hole = $object->hole;
if (!isset($holes[$hole])) {
$holes[$hole] = 0;
}
$holes[$hole]++;
}
}
I tested it using this:
$object1 = (object)['hole'=>'2'];
$object2 = (object)['hole'=>'3'];
$object3 = (object)['hole'=>'1'];
$object4 = (object)['hole'=>'3'];
$array = [$object1,$object2,$object3,$object4];
$holes = [];
foreach ($array as $object) {
if (isset($object->hole)) {
$hole = $object->hole;
if (!isset($holes[$hole])) {
$holes[$hole] = 0;
}
$holes[$hole]++;
}
}
echo "<pre>";
print_r($holes);
Prints
Array
(
[2] => 1
[3] => 2
[1] => 1
)
It sounds like you want an array of 18 (?) elements returned with the count of number of occurences of each hole.
$new_array = array_reduce($your_array, function ($carry, $item) {
if (property_exists($item, "hole") && array_key_exists($item->hole - 1, $carry)) {
$carry[$item->hole - 1]++;
}
return $carry;
}, array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0));
I have an array which is like:
array(2) { ["y"]=> string(1) "A" ["z"]=> string(3) "1,2" }
I want to print them as:
array(2) { ["y"]=> string(1) "A" ["z"]=> string(1) "107"},array(2) { ["y"]=> string(1) "A" ["z"]=> string(1) "2"}
Here is my code:
$a = explode(",",$row['z']);
Assuming that you want to print it like...
array(2) { ["y"]=> string(1) "A" ["z"]=> string(1) "1"}
array(2) { ["y"]=> string(1) "A" ["z"]=> string(1) "3"}
...
then that should work.
$rowSplitted = array();
$zValues = explode(',', $row['z'])
$yValue = $row['y']
foreach ($zValues as $zValue) {
$rowSplitted[] = array(
'y' => $yValue,
'z' => $zValue,
];
}
$row = array("y"=>"A","z"=>"1,2"}
$zArrayFromCSV = explode(",",$row['z']);
$newArray = array();
foreach ($zArrayFromCSV as $valueZ) {
$newArray[] = array("y"=>$row['y'],"z"=>$valueZ);
}
var_dump($newArray);
//array(2)
//{
// [0]=>array(2) { ["y"]=> string(1) "A" ["z"]=> string(1) "1"}
// [1]=>array(2) { ["y"]=> string(1) "A" ["z"]=> string(1) "2"}
//}
I have this array and I was wondering how can I :
Sum qty so finaly I receive only unique products ids with their qty-s, for example:
product 805 - 1 piece
product 1118 - 2+3+4 = 9pieces
array(2){
["product"]=> array(4){
[0]=> string(3) "805"
[1]=> string(4) "1118"
[2]=> string(4) "1118"
[3]=> string(4) "1118"
}
["qty"]=> array(4) {
[0]=> string(1) "1"
[1]=> string(1) "2"
[2]=> string(1) "3"
[3]=> string(1) "4"
}
}
Thank you in advance,
$productQuantities = array();
$products = array("805","1118","1118","1118");
$quantities = array(1,2,3,4);
foreach($products AS $key=>$productId){
$quantity = (int) $quantities[$key];
if(isset($productQuantities[$productId])){
$productQuantities[$productId] += $quantity;
} else {
$productQuantities[$productId] = $quantity;
}
}
var_dump($productQuantities);
You could try this:
$zipped=array_map(
null,
$your_array['product'],
$your_array['qty']
);
$compact = array();
foreach ($zipped as $k => $v){
if(!array_key_exists($v[0], $compact)){
$compact[$v[0]] = $v[1];
} else {
$compact[$v[0]] += $v[1];
}
}
Then you will find your result in $compact
I have a session that looks like this:
array(3) {
["counter"]=>
int(0)
["currentItem"]=>
string(1) "2"
["addedToCart"]=>
array(12) {
[0]=>
array(11) {
["aantal"]=>
int(1)
["id"]=>
string(1) "1"
["filmtitel"]=>
string(11) "a_bugs_life"
["film_id"]=>
string(1) "2"
["zaal_id"]=>
string(1) "1"
["zaaltitel"]=>
string(6) "zaal 1"
["tijdstip"]=>
string(8) "15:00:00"
["stoeltjes"]=>
string(2) "21"
["dag"]=>
string(8) "woensdag"
["verwijder"]=>
int(2)
["vertoningId"]=>
string(1) "3"
}
[1]=>
array(11) {
["aantal"]=>
int(1)
["id"]=>
string(1) "1"
["filmtitel"]=>
string(11) "a_bugs_life"
["film_id"]=>
string(1) "2"
["zaal_id"]=>
string(1) "1"
["zaaltitel"]=>
string(6) "zaal 1"
["tijdstip"]=>
string(8) "15:00:00"
["stoeltjes"]=>
string(1) "7"
["dag"]=>
string(8) "woensdag"
["verwijder"]=>
int(2)
["vertoningId"]=>
string(1) "3"
}
[2]=>
array(11) {
["aantal"]=>
int(1)
["id"]=>
string(1) "1"
["filmtitel"]=>
string(11) "a_bugs_life"
["film_id"]=>
string(1) "2"
["zaal_id"]=>
string(1) "1"
["zaaltitel"]=>
string(6) "zaal 1"
["tijdstip"]=>
string(8) "15:00:00"
["stoeltjes"]=>
string(2) "22"
["dag"]=>
string(8) "woensdag"
["verwijder"]=>
int(2)
["vertoningId"]=>
string(1) "3"
}
}
}
Now, from $_SESSION['addedToCart] I would like to remove arrays if they meet to certain conditions. I have tried the following.
foreach ($_SESSION["addedToCart"] as $arr) {
if ($arr["stoeltjes"] == $stoeltje && $arr['film_id'] == $id) {
unset($arr);
}
}
This doesn't seem to work, it doesn't remove anything, I did a var_dump to check if the variables $stoeltje and $id were fine and they were fine so that cant be the problem.
Am I able to use unset in this kind of situation?
foreach ($_SESSION["addedToCart"] as &$arr)
& turns your variable into a reference instead of a copy. Normally this would be sufficient. unset() only works on data within the current scope (so your foreach loop) leaving the original unchanged (See unset() for details).
Instead you can do:
foreach ($_SESSION["addedToCart"] as $key => $val)
{
if ($val["stoeltjes"] == $stoeltje && $val['film_id'] == $id) {
unset($_SESSION["addedToCart"][$key]);
}
}
Even if the suggested way with the reference should work normally, here's an example without it:
foreach ($_SESSION["addedToCart"] as $key => $arr) {
if ($arr["stoeltjes"] == $stoeltje && $arr['film_id'] == $id) {
unset($_SESSION["addedToCart"][$key]);
}
}
It doesn't work, because foreach is working on a copy, therefore $arr is just a copy of each element in the main table.
from php.net:
As of PHP 5, you can easily modify array's elements by preceding $value with &. This will assign reference instead of copying the value.
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
Try this:
$arr = array(1, 2, 3, 4);
foreach ($arr as $key => &$value) {
if ($value == 2)
{
unset($arr[$key]);
}
}
print_r($arr);
remove_array_key("film_id", $array);
function remove_array_key($key, &$array)
{
$result = array_key_exists($key, $array);
if ($result) {
unset($array[$key]);
return $array;
}
foreach ($array as &$v) {
if (is_array($v)) {
$result = remove_array_key($key, $v);
}
if (is_array($result)) {
unset($v[$key]);
return $array;
}
}
return false;
}
Link to Github Explanation