updating php multidimensional array and resaving to db - php

I have the following structure for my array which is dynamically generated:
Array
(
[0] => Array
(
[friend_request_sender_id] => 1
[friend_request_date] => 07-08-2014
[friend_request_time] => 11:12:19
[friend_request_recipient_id] => 5
[friend_request_sent] => 1
[friend_request_accepted] => 0
)
[1] => Array
(
[friend_request_sender_id] => 1
[friend_request_date] => 07-08-2014
[friend_request_time] => 11:12:47
[friend_request_recipient_id] => 2
[friend_request_sent] => 1
[friend_request_accepted] => 0
)
)
I would like to update the first array value [friend_request_accepted] => 0 to this: [friend_request_accepted] => 1 which i have achieved by doing the following:
$test = get_user_meta($current_user->ID,'get_user_friends', true );
foreach($test as &$values){
if($values['friend_request_recipient_id'] === '5'){
$values['friend_request_accepted'] = '1';
break; // Stop the loop after we've found the item
}
}
However, i would like to actually save this new value into the database, overwriting the existing value. The array should then look like this:
Array
(
[0] => Array
(
[friend_request_sender_id] => 1
[friend_request_date] => 07-08-2014
[friend_request_time] => 11:12:19
[friend_request_recipient_id] => 5
[friend_request_sent] => 1
[friend_request_accepted] => 1
)
[1] => Array
(
[friend_request_sender_id] => 1
[friend_request_date] => 07-08-2014
[friend_request_time] => 11:12:47
[friend_request_recipient_id] => 2
[friend_request_sent] => 1
[friend_request_accepted] => 0
)
)
How should I do this?

this line doesn't work:
if($values['friend_request_recipient_id'] === '5'){
$values['friend_request_accepted'] = '1';
break; // Stop the loop after we've found the item
}
because
[friend_request_recipient_id] => 5
is an integer I think you just need to sure in condition because '===' is also checked the types of variable.

Related

unset array key and rearrange the key

This is how my array looks like :
Array
(
[0] => Array
(
[unit] => 10
[harga] => 15000
)
[1] => Array
(
[unit] => 7
[harga] => 10000
)
[2] => Array
(
[unit] => 12
[harga] => 123123
)
)
I want to unset the 0 key array when the unit is 0 and rearrange the key so the 1 key will replace the 0.
This is how I do it :
$jumlah_penjualan = $data - > unit;
while ($jumlah_penjualan > 0) {
$persediaan_pertama = $persediaan[0]['unit'];
$harga_persediaan = $persediaan[0]['harga'];
if ($persediaan_pertama < $jumlah_penjualan) {
$dijual = $persediaan_pertama;
$penjualan[] = array(
'unit' => $dijual,
'harga' => $harga_persediaan,
'total' => $dijual * $harga_persediaan);
$persediaan[0]['unit'] = $persediaan[0]['unit'] - $dijual;
} else {
$dijual = $jumlah_penjualan;
$penjualan[] = array(
'unit' => $dijual,
'harga' => $harga_persediaan,
'total' => $dijual * $harga_persediaan);
$persediaan[0]['unit'] = $persediaan[0]['unit'] - $dijual;
}
if ($persediaan[0]['unit'] == 0) {
unset($persediaan[0]);
$persediaan = array_values($persediaan);
}
$jumlah_penjualan = $jumlah_penjualan - $dijual;
}
But the result looks like it continues looping before rearranging the array.
This is how the array should look like after unset:
Array(
[0] => Array
(
[unit] => 9
[harga] => 123123
)
)
If you want to remove the first elements until the unit is not 0, you can
$arr = array
(
array
(
"unit" => 0,
"harga" => 15000
),
array
(
"unit" => 0,
"harga" => 10000
),
array
(
"unit" => 12,
"harga" => 123123
)
);
while( $arr[0]["unit"] == 0 ) { //Loop until $arr[0]["unit"] is not 0
unset($arr[0]); //Remove $arr[0] since unit is 0
$arr = array_values($arr); //Make Make element 1 to element 0
}
echo "<pre>";
print_r( $arr );
echo "</pre>";
This will result to:
Array
(
[0] => Array
(
[unit] => 12
[harga] => 123123
)
)
To remove the first element of an array and reindex the elements, use array_shift. First, check that the number of units are zero, then remove the first element if that's the case.
if ($arr[0]['unit'] == 0) {
array_shift($arr);
}
Since it's impossible to say what your other variables even mean because of the language, you probably want to move this outside of your while loop, so that the first element is only removed after you've processed the array.

How to echo 5 Different Arrays?

I need to collect all the lines with the numbers, And turn them into str and then echo each one separately,
How can i do it please?
array (
0 =>
array (
0 => 'id=4321',
1 => '4321',
),
1 =>
array (
0 => 'id=7777',
1 => '7777',
),
2 =>
array (
0 => 'id=0101',
1 => '0101',
),
3 =>
array (
0 => 'id=1213',
1 => '1213',
),
)
Example:
4321
7777
0101...
Maybe:
echo $you_array[0][1]."<br>";
echo $you_array[1][1]."<br>";
echo $you_array[2][1]."<br>";
.....................
You can also use nl2br(), google it or stackoverflow it.
-Edit:
or better:
foreach ($your_array as $subarr) {
echo $subarr[1]."<br>";
}

How to compare two array with condition in php

I have two arrays like below :
Array_1 : ($array1 - after print the array)
Array ( [companyid] => 589 [company_name] => log_tracking_24 [username] => admin#log24.com [emp_count] => 0 [user_id] => 764 [module_expenses_benefit] => 1 [module_time_attendance] => 1 [module_bank_ftp] => 0 )
Array_2 : ($array2 - after print the array )
Array ( [company_name] => log_tracking_241 [username] => admin#log241.com [password] => [cpassword] => [emp_count] => [module_expenses_benefit] => on [user_id] => 764 [companyid] => 589 )
I compare the two arrays and get different values . for this i try the following,
array_diff($array1,$array2);
Finally i get the following result :
Array ( [company_name] => log_tracking_24 [username] => admin#log24.com [emp_count] => 0 [module_expenses_benefit] => 1 [module_time_attendance] => 1 [module_bank_ftp] => 0 )
but what i want is ,
In my first array : $array1['emp_count'] having the value 0 and my second array : $array2['emp_count'] having the value Null ( '' ).
In this type of situation i want to remove the key and vlaue while the time of array_diff.
How to do this. i try the unset() function . but no use.
You can use array_filter to remove empty elements:
$emptyRemoved = array_filter($linksArray);
To remove value 0 and empty. you may use the following:
$emptyRemoved = remove_empty($linksArray);
function remove_empty($array) {
return array_filter($array, '_remove_zero');
}
function _remove_zero($value) {
return !empty($value) && $value === 0;
}
Pass your both array to remove_empty() function then use array_diff()
$arr1 = remove_empty($array1);
$arr2 = remove_empty($array2);
array_diff($arr1, $arr2);
Yes array_filter did the work for you. Just use:
array_diff(array_filter($array1), array_filter($array2));
See the example:
$entry = array(
0 => 'foo',
1 => false,
2 => -1,
3 => null,
4 => '',
5 => 0
);
print_r(array_filter($entry)); //Array ( [0] => foo [2] => -1 )
So array_filter removes the false, null, ''and 0.

PHP: How can i combine 2 different array into one

Is there a way to combine 2 array into one array?
My first array shows the amount calculated money per day.
$ArrayBefore[] = $amountOfTransactions_prDayArrayBefore;
Output:
Array (
[0] => Array (
[09/02] => 102.83
[08/02] => 3852.49
[07/02] => 2619.23
[06/02] => 1135.24
[05/02] => 2391.4
[04/02] => 2376.15
[03/02] => 2760.05
[02/02] => 1946.41
)
)
My second array shows the amount transactions per day.
$ArrayTrans[] = $amountOfTransactions_prDayArrayTrans;
Output:
Array (
[0] => Array (
[09/02] => 2
[08/02] => 30
[07/02] => 30
[06/02] => 15
[05/02] => 36
[04/02] => 31
[03/02] => 28
[02/02] => 14
)
)
Is there a way to combine both of them to one array. I want them to look like this, so i can spit them out in a table.
Array (
[0] => Array (
[09/02] => 102.83 => 2
[08/02] => 3852.49 => 30
[07/02] => 2619.23 => 30
[06/02] => 1135.24 => 15
[05/02] => 2391.4 => 36
[04/02] => 2376.15 => 31
[03/02] => 2760.05 => 28
[02/02] => 1946.41 => 14
)
)
i call first array $a , second array $b
foreach ( $a[0] as $key=>$value )
{
$c[0]["$value"] = $b[0][$key];
}
and
$c = array (size=1)
0 =>
array (size=8)
'102.83' => int 2
'3852.49' => int 30
'2619.23' => int 30
'1135.24' => int 15
'2391.4' => int 36
'2376.15' => int 31
'2760.05' => int 28
'1946.41' => int 14
Use array_merge():
$result = array_merge($ArrayBefore, $ArrayTrans);
EDIT:
Understand that my php is rusty, but how about something along the lines of:
foreach($ArrayBefore[] as $key => $value) {
$tmpPair[$value] = $ArrayTrans[0][$key];
$newArray[$key] = $tmpPair;
}
You may also be able to simplify it
foreach($ArrayBefore[] as $key => $value) {
$newArray[$key][$value] = $ArrayTrans[0][$key];
}

PHP split array based on search results

I have a multidimensional array which is created by a MySQL query which collects results based on a number of groups and sums. The array is below.
I'm interested in the costtotal and hitcount for each type of 'ad_type', 'click_status' and 'link_status' variation.
The possible values of the 3 types of variable are known:
i.e.
ad_type 0 / 1
click_status 2 / 3
link_status 1 / 2
I would like to create a new array based on the results of each combination.
I'm guessing a search or split would do it but I'm not having much luck.
How would I go about doing this?
Array
(
[0.261346210037681] => Array
(
[costtotal] => 0.0015
[hitcount] => 1
[ad_type] => 0
[click_status] => 2
[link_status] => 1
)
[0.190427019438173] => Array
(
[costtotal] => 0.001
[hitcount] => 1
[ad_type] => 0
[click_status] => 3
[link_status] => 1
)
[0.563596305962276] => Array
(
[costtotal] => 0.007
[hitcount] => 5
[ad_type] => 1
[click_status] => 2
[link_status] => 1
)
[0.893211513658251] => Array
(
[costtotal] => 0
[hitcount] => 3
[ad_type] => 1
[click_status] => 2
[link_status] => 2
)
[0.209184847035617] => Array
(
[costtotal] => 0.004
[hitcount] => 2
[ad_type] => 1
[click_status] => 3
[link_status] => 1
)
[0.73545002260753] => Array
(
[costtotal] => 0
[hitcount] => 1
[ad_type] => 1
[click_status] => 3
[link_status] => 2
)
)
If I fully understand what you want, then this code should satisfy you:
function generateClickCounterInfo() {
return array(
'costTotal' => 0.0,
'hitCount' => 0
);
}
function generateLinkStatusStructure() {
return array(
1 => generateClickCounterInfo(),
2 => generateClickCounterInfo()
);
}
function generateClickStatusStructure() {
return array(
2 => generateLinkStatusStructure(),
3 => generateLinkStatusStructure()
);
}
function generateAdTypeArrayStructure() {
return array(
0 => generateClickStatusStructure(),
1 => generateClickStatusStructure()
);
}
function getClickCounterReport(array $data) {
$result = generateAdTypeArrayStructure();
foreach ($data as $key => $value) {
$adType = $value['ad_type'];
$clickStatus = $value['click_status'];
$linkStatus = $value['link_status'];
if (!isset($result[$adType])
|| !isset($result[$adType][$clickStatus])
|| !isset($result[$adType][$clickStatus][$linkStatus])) {
throw new Exception(
"Input data does not conform to expected format. " .
"ad_type = {$adType}, click_status = {$clickStatus}, link_status = ${linkStatus}"
);
}
$costTotal = $value['costtotal'];
$hitCount = $value['hitcount'];
$result[$adType][$clickStatus][$linkStatus]['costTotal'] += $costTotal;
$result[$adType][$clickStatus][$linkStatus]['hitCount'] += $hitCount;
}
return $result;
}
And than getClickCounterReport($data) (where $data is data provided by you) will produce following array: http://pastebin.ubuntu.com/607464/
P.S. Knowing disadvantages:
No OOP (but these functions will be easy to transform to methods)
Magick numbers (0, 1, 2, 3 etc)
No array splitting is necessary. Simply create the variables that will store the totals for each of the permutation you want to measure and iterate through your array. Add to appropriate variables based upon the value you observe in ad_type, click_status, and link_status.

Categories