I need to find array values by key and sum this values if key exists in other array. I tried to different combination but I havent a good idea.
array(2) {
["www.test.pl"]=>
array(3) {
["category"]=>
array(3) {
}
["category2"]=>
array(3) {
}
}
["www.test2.pl"]=>
array(3) {
["category"]=>
array(3) {
}
["category2"]=>
array(3) {
}
["category3"]=>
array(3) {
}
}
}
I need to compare keys -"category", "category2" Of every URL ... and sum values if I have keys in both array of URLs.
I tries to do this in this example
link to compare array code
You can summ values in ne array:
// $arr1 - starting array
$arr2 = [];
foreach ($arr1 as $arr){
foreach ($arr as $arKey => $arVal) {
if (isset($arr2[$arKey])) {
$arr2[$arKey]['clicks'] += $arVal['clicks'];
$arr2[$arKey]['impressions'] += $arVal['impressions'];
$arr2[$arKey]['ctr'] += $arVal['ctr'];
} else {
$arr2[] = [
'clicks' => $arVal['clicks'],
'impressions' => $arVal['impressions'],
'ctr' => $arVal['ctr'],
];
}
}
}
I hope it is what you want to do
Like this:
foreach($array as $key => $value){
if(isset($array2[$key])){
$sum = $array2[$key]+$value;
}
}
Related
I'm fairly new to PHP and I'm having some trouble with arrays and combining data. I have the following array which has been created from a foreach loop:
array(1) {
[36868]=> int(3)
}
array(1) {
[2112]=> int(3)
}
array(1) {
[35901]=> int(3)
}
array(1) {
[6496]=> int(3)
}
array(1) {
[87]=> int(3)
}
array(1) {
[36868]=> int(3)
}
array(1) {
[68]=> int(3)
}
array(1) {
[9068]=> int(3)
}
array(1) {
[47]=> int(3)
}
The key in each array is a user ID, so I need to preserve this, but I only want one instance of each key and where there are duplicate keys, sum the values. Like so:
array(1) {
[36868]=> int(6)
}
array(1) {
[2112]=> int(3)
}
array(1) {
[35901]=> int(3)
}
array(1) {
[6496]=> int(3)
}
array(1) {
[87]=> int(3)
}
array(1) {
[68]=> int(3)
}
array(1) {
[9068]=> int(3)
}
array(1) {
[47]=> int(3)
}
The I've tried looping through the array:
foreach ($array as $key => &$value) {
if ($value[0] == $value[1]) {
$value[1] += $value[1];
}
}
But with no luck. I've also tried rendering the arrays differently i.e. [userid]=>1,[score]=>3 and I feel like I'm going round in circles a bit, so any help would be hugely appreciated.
$data <-- this is your original array
$result = array_reduce(
$data,
function($carry, $item) {
foreach ($item as $id => $score) {
if (array_key_exists($id, $carry)) {
$carry[$id] += $score;
} else {
$carry[$id] = $score;
}
}
return $carry;
},
[]
);
If you are sure that each item only contains 1 entry you could also simplify the callback to not use foreach:
$result = array_reduce(
$data,
function ($carry, $item) {
$score = reset($item);
$id = key($item);
if (array_key_exists($id, $carry)) {
$carry[$id] += $score;
} else {
$carry[$id] = $score;
}
return $carry;
},
[]
);
You could also keep using foreach instead:
/** foreach to create a $data array like described below and afterwards do this: **/
$result = [];
foreach($data as $row) {
$score = reset($row);
$id = key($row);
if (array_key_exists($id, $result)) {
$result[$id] += $score;
} else {
$result[$id] = $score;
}
}
This will take an array $data like this:
array(
array('1' => 3),
array('1' => 3),
array('2' => 3),
);
and creates the variable $result like this:
array(
'1' => 6,
'2' => 3,
);
Here is a clean method that will not produce Notices. When merge-summing array data the efficient method is to generate temporary keys and use the very fast isset() function. I could have used current() and key() to access the lone subarray element, but the second foreach control structure is actually faster and more compact. (Ref:
https://stackoverflow.com/a/21219594/2943403 )
Code: (Demo)
$array = [
[36868 => 3],
[2112 => 3],
[35901 => 3],
[6496 => 3],
[87 => 3],
[36868 => 3],
[68 => 3],
[9068 => 3],
[47 => 3]
];
$result = [];
foreach ($array as $subarray) {
foreach ($subarray as $k => $v) {
if (!isset($result[$k])) {
$result[$k] = $subarray;
} else {
$result[$k][$k] += $v;
}
}
}
var_export(array_values($result));
Output:
array (
0 =>
array (
36868 => 6,
),
1 =>
array (
2112 => 3,
),
2 =>
array (
35901 => 3,
),
3 =>
array (
6496 => 3,
),
4 =>
array (
87 => 3,
),
5 =>
array (
68 => 3,
),
6 =>
array (
9068 => 3,
),
7 =>
array (
47 => 3,
),
)
I have a multidimensional array and i need to merge all the child values back into its parent.
Say i have an array like this:
array(2) {
[XY] =>
array(3) {
[A]=> 20
[B]=> 30
[2]=>
array(2) {
[0]=> 1
[1]=> 2
}
}
[YZ] =>
array(3) {
[A]=> 60
[B]=> 50
[2]=>
array(2) {
[0]=> 3
[1]=> 4
}
}
}
and i want an output like this:
array(2) {
[XY] =>
array(4) {
[A]=> 20
[B]=> 30
[2]=> 1
[3]=> 2
}
[YZ] =>
array(4) {
[A]=> 60
[B]=> 50
[2]=> 3
[3]=> 4
}
}
How can I do this?
here is your solution its work recursive and give any child to meta parent (lvl 1 parent) note:end level Childs give to lvl 1 parent
function giveChildToParentLevel($array)
{
$countOfArray = count($array);
foreach($array as $key=>$value)
{
if(is_array($value))
{
$childitems = giveChildToParentLevel($value);
if(count($childitems) > 0 )
{
unset($array[$key]);
$i = 0;
foreach($childitems as $child)
{
if($i == 0)
$array[$key] = $child;
else
$array[$key.$i] = $child;
$i++;
}
}
}
}
return $array;
}
$array = //your array;
foreach($array as $key=>$value)
{
$array[$key] = giveChildToParentLevel($value);
}
print_r($array);
This code will merge the entire child array into its main parent array, similarly you can do for your requirements.
foreach($allDataInarray as $key => $data) {
$array_keys = array_keys($data);
foreach($array_keys as $key_value) {
if(is_array($data[$key_value])) {
foreach($data[$key_value] as $sub_key => $sub_data) {
$allDataInarray[$key][$sub_key] = $sub_data;
}
unset($allDataInarray[$key][$key_value]);
}
}
}
Note: this is for 2D array.
I have an array containing two arrays. When I write var_dump($array):
array(7) {
["Article"]=>
array(1) {
[0]=>
string(8) "39-746У"
}
["Visible"]=>
array(1) {
[0]=>
string(1) "1"
}
}
array(7) {
["Article"]=>
array(1) {
[0]=>
string(6) "12-003"
}
["Visible"]=>
array(1) {
[0]=>
string(1) "1"
}
}
When I write var_dump($array[0]) i get NULL.
I want to change Visible in the second array, but it change in two arrays
Real code:
$sql2="select tblCurrencies.name as name,Price,tblArticleInfo.Name as Name,ArticleID,CategoryID,Article,Visible from tblArticles,tblArticleInfo,tblCurrencies where tblArticleInfo.ArticleID=tblArticles.Id and tblArticles.Id='{$tovar_id}' and tblArticles.currencyID=tblCurrencies.id";
$Array2=query_result_as_rows($sql2,$conn);
You have two different kinds of array here, an associative array, with key-value pairs, and a non-associative array, indexed by number.
Associative array:
$a_array = array(
"Article" => "foo",
"Visible" => " bar",
);
Access data in an associative array:
echo $a_array['Article'];
// prints 'foo'
Iterate through an associative array:
foreach ($a_array as $key => $value) {
echo "$key, $value; ";
}
// prints 'Article, foo; Visible, bar'
//
A numerically-indexed array:
$num_array = ('pip', 'pap', 'pop');
Access items in a numerically-indexed array:
echo $num_array[0] . ", " . $num_array[2];
// prints 'pip, pop'
Iterate through the array:
foreach ($num_array as $num) {
echo "$num! ";
}
// prints "pip! pap! pop! "
You have an associative array with a numerically-indexed array inside it:
$array = array(
'Article' => array( '39-746У' ),
'Visible' => array( '1' )
);
var_dump of $array:
array(2) {
["Article"]=>
array(1) {
[0]=>
string(8) "39-746У"
}
["Visible"]=>
array(1) {
[0]=>
string(1) "1"
}
}
To access data, you need to combine the two methods:
echo $array['Article'][0];
// prints "39-746У"
To iterate through the arrays:
foreach ($array as $key => $value) { // this is the outer associative array
echo "outer array key: $key...\n"; // $value is the inner array
foreach ($value as $v) { // $value is a numerically-indexed array
echo "inner array item: $v\n";
}
}
Output:
outer array key: Article
inner array item: 39-746У
outer array key: Visible
inner array item: 1
Here's a workaround:
$array = array(...);
$array_keys = array_keys($array);
$array_first_key = $array_keys[0];
var_dump($array[$array_first_key]);
also:
var_dump(array_shift($array));
With php 5.4+
array_values($array)[0];
I need to reconstruct an array. Here is the original array:
array(8) {
[0] => array(1)
{
["L_TRANSACTIONID0"] => string(17) "62M97388AY676841D"
}
[1] => array(1)
{
["L_TRANSACTIONID1"] => string(17) "9FF44950UY3240528"
}
[2] => array(1)
{
["L_STATUS0"] => string(9) "Completed"
}
[3] => array(1)
{
["L_STATUS1"] => string(9) "Completed"
}
}
I would like to reconstruct it to be as such:
array(2) {
[0] => array(2)
{
["L_TRANSACTIONID0"] => string(17) "62M97388AY676841D"
["L_STATUS0"] => string(9) "Completed"
}
[1] => array(1)
{
["L_TRANSACTIONID1"] => string(17) "9FF44950UY3240528"
["L_STATUS1"] => string(9) "Completed"
}
}
Notice that the KEYS both match with the numeric representation... Is this at all possible?
edit:
here is my code I am using:
foreach($comparison as $key => $val) {
$findme1 = 'L_TRANSACTID'.$i++;
$findme2 = 'L_STATUS'.$c++;
$arrDisable = array($findme1,$findme2);
if( in_array($key, $arrDisable ) ) {
unset( $comparison[ $key ][$val]);
}
if( in_array($key, $arrDisable) ) {
unset( $comparison[ $key ][$val]);
}
}
Try this
$labels = array('L_TRANSACTIONID', 'L_STATUS');
$res = array();
foreach($arr as $val) {
$key = str_replace($labels, '', key($val));
$res[$key] = isset($res[$key]) ? array_merge($res[$key], $val) : $val;
}
print_r($res);
http://codepad.org/MwqTPqtA
If you are certain the the vector cointains pairs L_TRANSACTIONIDn / L_STATUSn keys,that is to say, for each transactionID, there is a corresponding status, what you can do, is to get the number of id/status records (which should equal the length of the initial array, divided by two), and compose the resultin keys, by increasing the current element count.
Could look something like this:
$numItems = sizeof($myInitialArray) / 2;
$newArray = array();
for($i = 0; $i < $numItems; $i++)
{
$itemID = $i * 2; // since we're getting id/status pairs, we're using a step equal to 2
$newArray[] = array(
("L_TRANSACTIONID" . $i) => $myInitialArray[$itemID], // this is the id value
("L_STATUS" . $i) => $myInitialArray[$itemID + 1] // this is the status for that id
);
}
Hope this helps. Have a great day!
Just working on something and can't find a simple solution to this problem without just looping through the array with a foreach. Does anyone have a simple solution for this
I want to turn this
&array(4) {
["a"]=>int(0)
["b"]=>int(1)
["c"]=>int(2)
["d"]=>int(3)
}
Into this
array(1) {
["a"]=>
array(1) {
[0]=>
array(1) {
["b"]=>
array(1) {
[0]=>
array(1) {
["c"]=>
array(1) {
[0]=>
array(1) {
["d"]=> int(1) //this value does not matter
}
}
}
}
}
}
}
The values don't matter at all I just need the keys to run against a array_intersect_key_recursive function that I have.
EDIT : The array has changed after testing it needs to be nested as an array of an array
I don't know how this could possibly end up helping you, but it was a fun exercise nonetheless.
$newArray = array();
$last = &$newArray;
$array = array_reverse(array_keys($array));
while ($item = array_pop($array)) {
if (!is_array($last)) {
$last = array();
}
$last[$item] = array(array());
$last = &$last[$item][0];
}
NOTE: I made this answer with the int(1). You said the value is not important so I'm not going to bother changing it for now, but you would have to do a bit more work if the value was important (probably something like get the value from the original array with $item as the key).
Another approach using recursion:
function toNestedArray(array $array, $index = 0) {
$return = array();
if ($index < count($array)) {
$return[$array[$index]] = toNestedArray($array, ++$index);
}
return $return;
}
Usage Example:
$flatArray = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4);
$nestedArray = toNestedArray(array_keys($flatArray));
print_r($nestedArray);
Output:
Array
(
[a] => Array
(
[b] => Array
(
[c] => Array
(
[d] => Array
(
)
)
)
)
)