So i have this 2D array.
array (size=6)
'US-20150018889-A1' =>
array (size=5)
0 => string 'US-13795596' (length=11)
1 => string 'US-13713626' (length=11)
2 => string 'US-11361942' (length=11) // match 1
3 => string 'WO-CH2003000577' (length=15)
4 => string 'US-20150018889' (length=14)
'US-20140018803-A1' =>
array (size=2)
0 => string 'US-11159064' (length=11)
1 => string 'US-20140018803' (length=14)
'US-8523858-B2' =>
array (size=17)
0 => string 'US-20060287652' (length=14)
1 => string 'CN-101296665' (length=12)
2 => string 'CA-2613278' (length=10)
3 => string 'ZA-200711136' (length=12)
4 => string 'KR-101334253' (length=12)
5 => string 'JP-5138587' (length=10)
6 => string 'TW-200709800' (length=12)
7 => string 'EP-1893110' (length=10)
8 => string 'IN-10213DELNP2007' (length=17)
9 => string 'NZ-564765' (length=9)
11 => string 'KR-1020080041619' (length=16)
13 => string 'WO-2007001945' (length=13)
14 => string 'TW-I392473' (length=10)
15 => string 'AU-2006262507' (length=13)
17 => string 'BR-PI0612275' (length=12)
18 => string 'US-8523858' (length=10)
19 => string 'JP-2008546479' (length=13)
'US-8852245-B2' =>
array (size=5)
0 => string 'US-13713626' (length=11)
1 => string 'US-11361942' (length=11) // match 1
2 => string 'WO-CH0300577' (length=12) // match 2
3 => string 'US-20130197589' (length=14)
5 => string 'US-8852245' (length=10)
'US-8845698-B2' =>
array (size=4)
0 => string 'US-11361942' (length=11) // match 1
1 => string 'WO-CH0300577' (length=12) // match 2
2 => string 'US-20130116735' (length=14)
3 => string 'US-8845698' (length=10)
'US-8343196-B2' =>
array (size=29)
0 => string 'WO-CH03000577' (length=13)
1 => string 'CH-0300577' (length=10)
2 => string 'EP-03818256' (length=11)
3 => string 'WO-2005018472' (length=13)
5 => string 'US-20060235400' (length=14)
6 => string 'AU-2003254686' (length=13)
8 => string 'DE-50312893' (length=11)
9 => string 'EP-1731107' (length=10)
11 => string 'BR-PI0318428' (length=12)
12 => string 'EP-1741397' (length=10)
14 => string 'IN-395DELNP2006' (length=15)
15 => string 'CA-2536960' (length=10)
16 => string 'JP-2007506450' (length=13)
17 => string 'CN-1819799' (length=10)
18 => string 'AR-045493' (length=9)
19 => string 'TW-I329506' (length=10)
20 => string 'DE-20321245' (length=11)
21 => string 'DE-20321551' (length=11)
22 => string 'DE-20321552' (length=11)
26 => string 'EP-1658015' (length=10)
27 => string 'IN-226296' (length=9)
29 => string 'US-8343196' (length=10)
30 => string 'JP-4999327' (length=10)
33 => string 'CL-2004002168' (length=13)
34 => string 'CL-21682004' (length=11)
35 => string 'TW-200514540' (length=12)
36 => string 'DE-20321151' (length=11)
37 => string 'DE-20321244' (length=11)
38 => string 'NZ-545392' (length=9)
I want to compare all the elements of each index with elements of all the other indexes. And if any of the elements from one index matches any of the elements from another index, I want to group those two indexes together, but i cannot lose the index values either.
i know array_intersect can do the comparing and grouping can be done after that, but it will require 2 loops, (n2 complexity)
is there a better way to do it that can reduce the complexity of the logic?
i basically want to see which indexes are related to each other. (relation being common elements)
P/S:
Provide answers only if you have a more optimized way, because i've already implemented it via normal logic.
PHP's inbuilt functions would be helpful.
Related
How to get 'syn' and 'sim' values as a string from the given arrays, as array could vary i only want to extract 'syn' and 'sim' as an example following arrays are given ,i know it might be a simple question but i am new to multidimensional array that's why can't seem to solve it.
array (size=1)
'adjective' =>
array (size=2)
'syn' =>
array (size=7)
0 => string 'cagey' (length=5)
1 => string 'cagy' (length=4)
2 => string 'canny' (length=5)
3 => string 'apt' (length=3)
4 => string 'cunning' (length=7)
5 => string 'ingenious' (length=9)
6 => string 'adroit' (length=6)
'sim' =>
array (size=4)
0 => string 'adroit' (length=6)
1 => string 'artful' (length=6)
2 => string 'intelligent' (length=11)
3 => string 'smar
array (size=2)
'noun' =>
array (size=3)
'syn' =>
array (size=23)
0 => string 'passion' (length=7)
1 => string 'beloved' (length=7)
2 => string 'dear' (length=4)
3 => string 'dearest' (length=7)
4 => string 'honey' (length=5)
5 => string 'sexual love' (length=11)
6 => string 'erotic love' (length=11)
7 => string 'lovemaking' (length=10)
8 => string 'making love' (length=11)
9 => string 'love life' (length=9)
10 => string 'concupiscence' (length=13)
11 => string 'emotion' (length=7)
12 => string 'eros' (length=4)
13 => string 'loved one' (length=9)
14 => string 'lover' (length=5)
15 => string 'object' (length=6)
16 => string 'physical attraction' (length=19)
17 => string 'score' (length=5)
18 => string 'sex' (length=3)
19 => string 'sex activity' (length=12)
20 => string 'sexual activity' (length=15)
21 => string 'sexual desire' (length=13)
22 => string 'sexual practice' (length=15)
'ant' =>
array (size=1)
0 => string 'hate' (length=4)
'usr' =>
array (size=1)
0 => string 'amour' (length=5)
'verb' =>
array (size=2)
'syn' =>
array (size=29)
0 => string 'love' (length=4)
1 => string 'enjoy' (length=5)
2 => string 'roll in the hay' (length=15)
3 => string 'make out' (length=8)
4 => string 'make love' (length=9)
5 => string 'sleep with' (length=10)
6 => string 'get laid' (length=8)
7 => string 'have sex' (length=8)
8 => string 'know' (length=4)
9 => string 'do it' (length=5)
10 => string 'be intimate' (length=11)
11 => string 'have intercourse' (length=16)
12 => string 'have it away' (length=12)
13 => string 'have it off' (length=11)
14 => string 'screw' (length=5)
15 => string 'jazz' (length=4)
16 => string 'eff' (length=3)
17 => string 'hump' (length=4)
18 => string 'lie with' (length=8)
19 => string 'bed' (length=3)
20 => string 'have a go at it' (length=15)
21 => string 'bang' (length=4)
22 => string 'get it on' (length=9)
23 => string 'bonk' (length=4)
24 => string 'copulate' (length=8)
25 => string 'couple' (length=6)
26 => string 'like' (length=4)
27 => string 'mate' (length=4)
28 => string 'pair' (length=4)
'ant' =>
array (size=1)
0 => string 'hate' (length=4)
Any help would be appreciated thanks!
Create a function:
function getArrayAsString($array, $key) {
if (isset($array[$key])) {
return is_array($array[$key]) ? implode(', ', $array[$key]) : $array[$key];
}
return '';
}
Usage example:
$synData = getArrayAsString($array['adjective'], 'sim');
$simData = getArrayAsString($array['noun'], 'syn');
I have this array:
array (size=263)
0 => string 'abend' (length=5)
1 => string 'andorra' (length=7)
2 => string 'united_arab_emirates' (length=20)
3 => string 'afghanistan' (length=11)
4 => string 'antigua_and_barbuda' (length=19)
5 => string 'anguilla' (length=8)
6 => string 'albania' (length=7)
7 => string 'armenia' (length=7)
8 => string 'netherlands_antilles' (length=20)
9 => string 'angola' (length=6)
10 => string 'antarctica' (length=10)
11 => string 'american_samoa' (length=14)
12 => string 'austria' (length=7)
13 => string 'australia' (length=9)
14 => string 'aruba' (length=5)
15 => string 'azerbaijan' (length=10)
16 => string 'bosnia_and_herzegovina' (length=22)
17 => string 'barbados' (length=8)
18 => string 'bangladesh' (length=10)
19 => string 'belgium' (length=7)
20 => string 'burkina_faso' (length=12)
...
this is a list of countries that later is converted to a dropdown...
now I have the user country and I want to remove his country from the list
BUT
to prepend it to the start so it will be first.
Example:
lets say that the user country is "belgium" so the id of it is 19.
so the new array should be:
array (size=263)
19 => string 'My country' (length=5)
0 => string 'abend' (length=5)
1 => string 'andorra' (length=7)
2 => string 'united_arab_emirates' (length=20)
3 => string 'afghanistan' (length=11)
4 => string 'antigua_and_barbuda' (length=19)
5 => string 'anguilla' (length=8)
6 => string 'albania' (length=7)
7 => string 'armenia' (length=7)
8 => string 'netherlands_antilles' (length=20)
9 => string 'angola' (length=6)
10 => string 'antarctica' (length=10)
11 => string 'american_samoa' (length=14)
12 => string 'austria' (length=7)
13 => string 'australia' (length=9)
14 => string 'aruba' (length=5)
15 => string 'azerbaijan' (length=10)
16 => string 'bosnia_and_herzegovina' (length=22)
17 => string 'barbados' (length=8)
18 => string 'bangladesh' (length=10)
20 => string 'burkina_faso' (length=12)
...
As you can see belgium was erased from the array, and "my country" was added to the top.
Any ideas how to achieve that?
try this
since you know the user country unset it from the initial array, while keeping the user country in another array, then you can combine it to form the new array.
$first = array(1 => brazil, 2 => china, 3 => germany);
$second = array(3 => germany);
unset($ss[3]);
$combine = $first + $second;
print_r($combine);
result:
Array
(
[3] => germany
[1] => brazil
[2] => china
)
for achieving this you need a collection interface upon array or a truly collection thing in php . i suggest check this out
collection classes in php - sitepoint
I have an array like this
0 => string '' (length=0)
1 => string 'nameMiddle' (length=10)
2 => string 'dsa' (length=3)
3 => string '' (length=0)
4 => string 'phoneFirst' (length=10)
5 => string 'addressStreet' (length=13)
6 => string 'addressCity' (length=11)
7 => string 'addressState' (length=12)
8 => string 'addressZip' (length=10)
9 => string 'status' (length=6)
10 => string 'gender' (length=6)
11 => string 'dateOfBirth' (length=11)
12 => string 'numberOfDependents' (length=18)
13 => string 'salary' (length=6)
14 => string 'startDate' (length=9)
15 => string 'ltdClass' (length=8)
16 => string 'deductionMedical' (length=16)
17 => string 'deductionDental' (length=15)
18 => string 'deductionVision' (length=15)
19 => string 'suppLyfCost' (length=11)
20 => string 'stdCost' (length=7)
21 => string 'effectiveDate' (length=13)
These values would eventually be keys using array_combine but I cannot have the same key on an array_combine. How can I check if the array has the same value if so how can I make the value unique into like this
0 => string '0'
3 => string '3'
thanks
To replace values that are empty or not unique with their key, try:
foreach($array as $k => &$v) {
if(empty($v) || count(array_search($v, $array)) > 1) {
$v = $k;
}
}
I have the following multidimensional array:
array (size=2)
10 =>
array (size=7)
1 =>
array (size=4)
0 => string '10view1' (length=7)
1 => string '10ins1' (length=6)
2 => string '10edit1' (length=7)
3 => string '10del1' (length=6)
2 =>
array (size=4)
0 => string '10view2' (length=7)
1 => string '10ins2' (length=6)
2 => string '10edit2' (length=7)
3 => string '10del2' (length=6)
3 =>
array (size=4)
0 => string '10view3' (length=7)
1 => string '10ins3' (length=6)
2 => string '10edit3' (length=7)
3 => string '10del3' (length=6)
11 =>
array (size=7)
1 =>
array (size=4)
0 => string '11view1' (length=7)
1 => string '11ins1' (length=6)
2 => string '11edit1' (length=7)
3 => string '11del1' (length=6)
2 =>
array (size=4)
0 => string '11view2' (length=7)
1 => string '11ins2' (length=6)
2 => string '11edit2' (length=7)
3 => string '11del2' (length=6)
3 =>
array (size=4)
0 => string '11view3' (length=7)
1 => string '11ins3' (length=6)
2 => string '11edit3' (length=7)
3 => string '11del3' (length=6)
I want to convert it to the array like:
1 => array(10 => array(
0 => string '10view1' (length=7)
1 => string '10ins1' (length=6)
2 => string '10edit1' (length=7)
3 => string '10del1' (length=6)),
11 => array(
0 => string '11view1' (length=7)
1 => string '11ins1' (length=6)
2 => string '11edit1' (length=7)
3 => string '11del1' (length=6)
)
),
2 => array(
10 => array(
0 => string '10view2' (length=7)
1 => string '10ins2' (length=6)
2 => string '10edit2' (length=7)
3 => string '10del2' (length=6)
),
11 => array(
0 => string '11view2' (length=7)
1 => string '11ins2' (length=6)
2 => string '11edit2' (length=7)
3 => string '11del2' (length=6)
)
),
3 => array(
10 => array(
0 => string '11view3' (length=7)
1 => string '11ins3' (length=6)
2 => string '11edit3' (length=7)
3 => string '11del3' (length=6)
),
11 => array(
0 => string '11view1' (length=7)
1 => string '11ins1' (length=6)
2 => string '11edit1' (length=7)
3 => string '11del1' (length=6)
)
)
please help me i confuse to solve this problem thanks in advance.
It's quite obvious for me how to implement this using foreach loop, but I wonder if it's possible to do this with PHP array functions like array_map or array_walk. Please use PHP 5.3 for the callback function. Thank you!
Using array map you can achieve like this
<?php
function merge_array($n, $m)
{
$data = array();
$data[10] = $n;
$data[11] = $m;
return $data;
}
$c = array_map("merge_array", $array[10], $array[11]);
print_r($c);
Using for loop it is very simple you can achieve like this
$count_10 = count($array[10]);
$count_11 = count($array[11]);
$count = ($count_10>$count_11)?$count_10:$count_11;
$data = array();
for($i=0; $i<$count; $i++)
{
if(isset($array[10][$i]))
{
$data[$i][] = $array[10][$i];
}
if(isset($array[11][$i]))
{
$data[$i][] = $array[11][$i];
}
}
I remove dupliactes from array (list #1) with array_uniqe but how to recalculate kyes of it's output (list #2) to [0-x] (list #3):
list #1:
array (size=24)
0 => string '2013-09-18' (length=10)
1 => string '2013-09-18' (length=10)
2 => string '2013-09-18' (length=10)
3 => string '2013-09-18' (length=10)
4 => string '2013-09-19' (length=10)
5 => string '2013-09-19' (length=10)
6 => string '2013-09-19' (length=10)
7 => string '2013-09-19' (length=10)
8 => string '2013-09-19' (length=10)
9 => string '2013-09-19' (length=10)
10 => string '2013-09-19' (length=10)
11 => string '2013-09-20' (length=10)
12 => string '2013-09-20' (length=10)
13 => string '2013-09-20' (length=10)
14 => string '2013-09-20' (length=10)
15 => string '2013-09-21' (length=10)
16 => string '2013-09-21' (length=10)
17 => string '2013-09-21' (length=10)
18 => string '2013-09-21' (length=10)
19 => string '2013-09-21' (length=10)
20 => string '2013-09-21' (length=10)
21 => string '2013-09-22' (length=10)
22 => string '2013-09-22' (length=10)
23 => string '2013-09-22' (length=10)
List #2:
array (size=5)
0 => string '2013-09-18' (length=10)
4 => string '2013-09-19' (length=10)
11 => string '2013-09-20' (length=10)
15 => string '2013-09-21' (length=10)
21 => string '2013-09-22' (length=10)
List #3:
array (size=5)
0 => string '2013-09-18' (length=10)
1 => string '2013-09-19' (length=10)
2 => string '2013-09-20' (length=10)
3 => string '2013-09-21' (length=10)
4 => string '2013-09-22' (length=10)
$list3 = array_values($list2);