How to echo 5 Different Arrays? - php

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>";
}

Related

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.

combine or intersect two arrays different length in php

First array is $niz:
Array (
[Swansea] => 4
[Stoke City] => 3
[Sunderland] => 3
[Southampton] => 5
[Liverpool] => 3
[Manchester United] => 2
[Hull City] => 1
[Tottenham] => 2
[Newcastle Utd] => 1
[Aston Villa] => 1
[West Ham] => 2
[Crystal Palace] => 3
[Chelsea] => 3
)
Second array is $niz1:
Array (
[Stoke City] => 2
[Sunderland] => 2
[Liverpool] => 1
[Hull City] => 1
[Tottenham] => 1
[Manchester United] => 1
[Newcastle Utd] => 1
[Crystal Palace] => 3
[Chelsea] => 1
)
How to combine these arrays to get $niz2 (keys are ordered like array $niz1 and values are from matching array $niz) like:
$niz2:
Array (
[Stoke City] => 3
[Sunderland] => 3
[Liverpool] => 3
[Hull City] => 1
[Tottenham] => 2
[Manchester United] => 2
[Newcastle Utd] => 1
[Crystal Palace] => 3
[Chelsea] => 3
)
I tried with function array_merge() but I get empty values and I tried with array_intersect_key().
try this
foreach ($niz1 as $k=>$n)
{
if(in_array($k,$niz1))
{
$niz2[$k]=$niz[$k];
}
}
print_r($niz2);
Try this:
$temp = array_intersect_key($niz, $niz1);
foreach ($niz1 as $k => $v) {
$niz2[$k] = $temp[$k];
}
//(overwrites the values of $niz1 with those of $niz2)
$bif=array_merge($niz1,$niz);
//(removes everything from $bif that is not in $niz1)
$result=array_intersect($niz1,$bif);

updating php multidimensional array and resaving to db

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.

Splitting one Array into many Array in PHP

I'm in need of splitting of the single array into multiple array for some report generating purpose.
I have an array like this which I have given below.
Array
(
[blah_1] => 1
[blahblah_1] => 31
[blahblahblah_1] => 25
[blah_3] => 1
[blahblah_3] => 3
[blahblahblah_3] => 5
[blah_10] => 1
[blahblah_10] => 10
[blahblahblah_10] => 2
)
I want to split the above to,
Array
(
[blah_1] => 1
[blahblah_1] => 31
[blahblahblah_1] => 25
)
Array
(
[blah_3] => 1
[blahblah_3] => 3
[blahblahblah_3] => 5
)
Array
(
[blah_10] => 1
[blahblah_10] => 10
[blahblahblah_10] => 2
)
How can I do this in PHP ??
$oldarray=array('blah_1'=>1,'blahblah_1'=>31,'blahblahblah_1'=>25,
'blah_3'=>1,'blahblah_3'=>3,'blahblahblah_3'=>5,
'blah_10'=>1,'blahblah_10'=>10,'blahblahblah_10'=>2
)
$newarray=array();
foreach ($oldarray as $key=>$val) { //Loops through each element in your original array
$parts=array_reverse(explode('_',$key)); //Splits the key around _ and reverses
$newarray[$parts[0]][$key]=$val; //Gets the first part (the number) and adds the
//value to a new array based on this number.
}
The output will be:
Array (
[1]=>Array
(
[blah_1] => 1
[blahblah_1] => 31
[blahblahblah_1] => 25
)
[3]=>Array
(
[blah_3] => 1
[blahblah_3] => 3
[blahblahblah_3] => 5
)
[10]=>Array
(
[blah_10] => 1
[blahblah_10] => 10
[blahblahblah_10] => 2
)
)
Use array_chunk. Example:
<?php
$chunks = array_chunk($yourarray, 3);
print_r($chunks);
?>
Use array_chunk
i.e
$a = array(1,2,3,4,5,6,7);
var_dump(array_chunk($a, 3));
Not sure if you are looking for array_slice
but take a look at this example:
<?php
$input = array("a", "b", "c", "d", "e");
print_r(array_slice($input, 2, -1));
print_r(array_slice($input, 2, -1, true));
?>
will result in this:
Array
(
[0] => c
[1] => d
)
Array
(
[2] => c
[3] => d
)
array_chunk ( array $input , int $size);

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