Bulk friend Request insertion based on array input in php - php

I have an array,
Array (
[0] => Array ( [did] => 12 [uid] => 21 )
[1] => Array ( [did] => 10 [uid] => 12 )
[2] => Array ( [did] => 9 [uid] => 11 )
[3] => Array ( [did] => 10 [uid] => 11 )
[4] => Array ( [did] => 12 [uid] => 11 )
)
where I want all the uids who have the same did value to be connected with each other. For example- for did=12, uids- 21 & 11 should be connected with each other. I am struggling to figure out how to do this in PHP and MySQL? Please note that array gets generated dynamically using query and can have many rows.

You could loop over the array and use the did as the key. If the key already exists, add to the array, else create a new entry using the key:
$items = [
["did" => 12, "uuid" => 21],
["did" => 10, "uuid" => 12],
["did" => 9, "uuid" => 11],
["did" => 10, "uuid" => 11],
["did" => 12, "uuid" => 11],
];
$result = [];
foreach ($items as $i) {
array_key_exists($i["did"], $result) ? $result[$i["did"]][] = $i["uuid"] : $result[$i["did"]] = [$i["uuid"]];
}
print_r($result);
Result:
Array
(
[12] => Array
(
[0] => 21
[1] => 11
)
[10] => Array
(
[0] => 12
[1] => 11
)
[9] => Array
(
[0] => 11
)
)
Php demo

Related

Combined Strings and sum numeric two multidimensional arrays in PHP

I have below two arrays with strings and numbers,
I want to keep only one strings header and sum numeric value from each key value with another array.
I have tried many of the solutions available online but nothing found as required.
$array1 =
Array
(
[0] => Array
(
[0] => Out Of Warranty
[1] => Total Orders
[2] => Total Qty
[3] => Canceled Orders
)
[1] => Array
(
[0] => Today<br/>(04-26-2020)
[1] => 1
[2] => 1
[3] => 0
)
[2] => Array
(
[0] => Yesterday<br/>(04-25-2020)
[1] => 0
[2] => 0
[3] => 0
)
[3] => Array
(
[0] => This Week<br/>(04-20-2020 - 04-26-2020)
[1] => 22
[2] => 39
[3] => 0
)
[4] => Array
(
[0] => Last Week<br/>(04-13-2020 - 04-19-2020)
[1] => 7
[2] => 7
[3] => 0
)
[5] => Array
(
[0] => This Month<br/>(04-01-2020 - 04-26-2020)
[1] => 29
[2] => 46
[3] => 0
)
[6] => Array
(
[0] => This Year<br/>(01-01-2020 - 04-26-2020)
[1] => 30
[2] => 47
[3] => 0
)
)
$array2 =
Array
(
[0] => Array
(
[0] => Out Of Warranty
[1] => Total Orders
[2] => Total Qty
[3] => Canceled Orders
)
[1] => Array
(
[0] => Today<br/>(04-24-2020)
[1] => 10
[2] => 10
[3] => 0
)
[2] => Array
(
[0] => Yesterday<br/>(04-23-2020)
[1] => 7
[2] => 7
[3] => 0
)
[3] => Array
(
[0] => This Week<br/>(04-20-2020 - 04-24-2020)
[1] => 51
[2] => 51
[3] => 0
)
[4] => Array
(
[0] => Last Week<br/>(04-13-2020 - 04-19-2020)
[1] => 31
[2] => 31
[3] => 0
)
[5] => Array
(
[0] => This Month<br/>(04-01-2020 - 04-24-2020)
[1] => 93
[2] => 93
[3] => 0
)
[6] => Array
(
[0] => This Year<br/>(01-01-2020 - 04-24-2020)
[1] => 1281
[2] => 1281
[3] => 1
)
)
Expected output as Strings should be use only once and numbers should be added to each other.
For example output should be 6 index i.e sum of 6 index from array1 and array2 -
[6] => Array
(
[0] => This Year<br/>(01-01-2020 - 04-26-2020)
[1] => 1311
[2] => 1328
[3] => 1
)
If your arrays are always sorted in the same order:
$newItems = [];
foreach ($array1 as $key => $item) {
$newItems[] = [
$item[0],
$item[1] + $array2[$key][1],
$item[2] + $array2[$key][2],
$item[3] + $array2[$key][3],
];
}
If keys in arrays are in distinct orders:
$newItems = [];
foreach ($array1 as $item) {
$name = $item[0];
$newItems[$name] = $item;
}
foreach ($array2 as $item) {
$name = $item[0];
$newItems[$name][1] += $item[1];
$newItems[$name][2] += $item[2];
$newItems[$name][3] += $item[3];
}
// apply array_values to get 0-indexed array
$newItems = array_values($newItems);
Only iterate the elements that are necessary for your summing logic. Using 2 loops will be the most concise and deliberate way to sum columns [1], [2], and [3].
You can create a new output array, but merely adding the second array values to the first affords a simpler addition-assignment syntax.
Code: (Demo)
for ($i = 1, $size = count($array2); $i < $size; ++$i) { // don't iterate [0] subarray (headers)
for ($col = 1; $col <= 3; ++$col) { // don't iterate [0] element (name)
$array1[$i][$col] += $array2[$i][$col];
}
}
var_export($array1);
Output:
array (
0 =>
array (
0 => 'Out Of Warranty',
1 => 'Total Orders',
2 => 'Total Qty',
3 => 'Canceled Orders',
),
1 =>
array (
0 => 'Today<br/>(04-26-2020)',
1 => 11,
2 => 11,
3 => 0,
),
2 =>
array (
0 => 'Yesterday<br/>(04-25-2020)',
1 => 7,
2 => 7,
3 => 0,
),
3 =>
array (
0 => 'This Week<br/>(04-20-2020 - 04-26-2020)',
1 => 73,
2 => 90,
3 => 0,
),
4 =>
array (
0 => 'Last Week<br/>(04-13-2020 - 04-19-2020)',
1 => 38,
2 => 38,
3 => 0,
),
5 =>
array (
0 => 'This Month<br/>(04-01-2020 - 04-26-2020)',
1 => 122,
2 => 139,
3 => 0,
),
6 =>
array (
0 => 'This Year<br/>(01-01-2020 - 04-26-2020)',
1 => 1311,
2 => 1328,
3 => 1,
),
)

How to check is php array have different value of key

I have this array
Array
(
[0] => Array
(
[id] => 1
[job_id] => 100
)
[1] => Array
(
[id] => 2
[job_id] => 100
)
[2] => Array
(
[id] => 3
[job_id] => 101
)
)
Now here how to check if "job_id" is different. eg all have same job_id or not and how many different job_id array have.
Thanks
Try this using array function:
$data = Array
(
[0] => Array
(
[id] => 1
[job_id] => 100
)
[1] => Array
(
[id] => 2
[job_id] => 100
)
[2] => Array
(
[id] => 3
[job_id] => 101
)
)
$totalDifferentJobId = count(array_unique(array_column($data, 'job_id')));
It will give you total unique job
Well, if it is not a too large array, you can do the following:
<?php
$arr = [
['id' => 1, 'job_id' => 100],
['id' => 2, 'job_id' => 100],
['id' => 3, 'job_id' => 101]
];
$jobids = array_unique(array_column($arr, 'job_id'));
var_dump($jobids);
This looks like a db-result, in wich case it would be better to use a group-by statement?

Combine 3 level multi dimensional array according to index

I have an array.in which i want to combine value according to same index value of sub array.it's a multi dimensional dynamic array which contain some same and some different indexes like sports footer etc.Please check the below array
Array
(
[0] => Array
(
[0] => Array
(
[Sport] => 15
)
[1] => Array
(
[Sport] => 14
)
[2] => Array
(
[Sport] => 29
)
)
[1] => Array
(
[0] => Array
(
[Surgical] => 11
)
[1] => Array
(
[Surgical] => 12
)
[2] => Array
(
[Surgical] => 13
)
[3] => Array
(
[Footwear] => 10
)
)
)
Below it the array format which i want as an output
Array
(
[0] => Array
(
[0] => 15
[1] => 14
[2] => 29
)
[1] => Array
(
[0] => 11
[1] => 12
[2] => 13
),
[2] => Array(
[0] => 10
)
)
Assuming your input array is called $data, you could do this:
foreach ($data as $row) {
foreach ($row as $pair) {
foreach ($pair as $key => $value) {
$result[$key][] = $value;
}
}
}
This will provide the $result as follows:
[
"Sport" => [15, 14, 29],
"Surgical" => [11, 12, 13],
"Footwear" => [10]
]
If you really want to throw away the "labels" and just keep the values, then add the following conversion at the end:
$result = array_values($result);
Which will give you the desired result:
[
[15, 14, 29],
[11, 12, 13],
[10]
]
... but that would seem less useful to me.

Merge 2 array same key

I'd like to merge two arrays on same key. I've tried my best to do it but unable to get success. I've added both the Arrays below. Kindly check both the Arrays in detail and help me how to merge it using same key.
Here's the 1st array :
Array
(
[1] => Array
(
[costprice1] => 500
[margin1] => 20
)
[2] => Array
(
[costprice2] => 600
[margin2] => 15
)
[3] => Array
(
[costprice3] => 700
[margin3] => 25
)
)
Here's the 2 array :
Array
(
[1] => Array
(
[entityType1] => Products1
)
[2] => Array
(
[entityType2] => Products2
)
[3] => Array
(
[entityType3] => Products3
)
)
i want to need like that array please suggestion me
Array
(
[1] => Array
(
[entityType1] => Products1
[costprice1] => 500
[margin1] => 20
)
[2] => Array
(
[entityType2] => Products2
[costprice2] => 600
[margin2] => 15
)
[3] => Array
(
[entityType3] => Products3
[costprice3] => 700
[margin3] => 25
)
)
please help me how to merge two array
Try this :
foreach($array1 as $key => $value) {
$array1[$key]['entityType'.$key] = $array2[$key]['entityType'.$key];
}
print_r($array1);
<?php
$array1 = [
1 => [
'costprice1' => 500,
'margin1' => 20
],
2 => [
'costprice2' => 600,
'margin2' => 15
],
3 => [
'costprice2' => 700,
'margin2' => 25
],
];
$array2 = [
1 => ['entityType1' => 'Products1'],
2 => ['entityType2' => 'Products2'],
3 => ['entityType3' => 'Products3'],
];
array_walk($array2, function(&$v, $k)use($array1){
$v = array_merge($v, $array1[$k]);
});
print_r($array2);
Output:
Array
(
[1] => Array
(
[entityType1] => Products1
[costprice1] => 500
[margin1] => 20
)
[2] => Array
(
[entityType2] => Products2
[costprice2] => 600
[margin2] => 15
)
[3] => Array
(
[entityType3] => Products3
[costprice2] => 700
[margin2] => 25
)
)
https://eval.in/618561

How to manipulate php arrays [duplicate]

This question already has answers here:
Group rows in an associative array of associative arrays by column value and preserve the original first level keys
(7 answers)
Closed 7 years ago.
How to manipulate php arrays. I have a $data variable below.
$data = Array
(
[0] => Array
(
[id] => 1
[capacity] => 900
[category] => users
)
[1] => Array
(
[id] => 2
[capacity] => 300
[category] => users
)
[2] => Array
(
[id] => 3
[capacity] => 900
[category] => students
)
[3] => Array
(
[id] => 4
[capacity] => 300
[category] => students
)
)
Now I want to group the data with same category like below. . I am not really familiar in php. Can somebody out there help me how to achieve this. What function should I used. Thanks
Array
(
[users] => Array
(
[0] => Array
(
[id] => 1
[capacity] => 900
[category] => users
)
[1] => Array
(
[id] => 2
[capacity] => 300
[category] => users
)
),
[students] => Array
(
[0] => Array
(
[id] => 1
[capacity] => 900
[category] => students
)
[1] => Array
(
[id] => 2
[capacity] => 300
[category] => students
)
)
)
Just iterate over the array and add it depending on the content of the category field to a new array:
$new = array();
foreach ($data as $val) {
$new[$val['category']][] = $val;
}
This does what you requested
<?php
$data = array(
0 => array (
"id" => 1,
"capacity" => 900,
"category" => "users"
),
1 => array (
"id" => 2,
"capacity" => 300,
"category" => "users"
),
2 => array (
"id" => 3,
"capacity" => 900,
"category" => "students"
),
3 => array (
"id" => 4,
"capacity" => 300,
"category" => "students"
)
);
$groups = array();
foreach ($data as $i) {
if ($i['category'] === "students") {
$groups['students'][] = $i;
}
else if ($i['category'] === "users") {
$groups['users'][] = $i;
}
}
echo "<pre>", print_r($groups), "</pre>";
Here is a working demo - http://codepad.viper-7.com/G4Br28

Categories