PHP - How to combine two arrays with different number of elements? - php

I have two arrays:
One:
array(4) {
[0]=> array(2) {
[0]=> string(19) "Ford"
[1]=> string(1) "1"
}
[1]=> array(2) {
[0]=> string(15) "Chevrolet"
[1]=> string(1) "1"
}
[2]=> array(2) {
[0]=> string(7) "VW"
[1]=> string(1) "1"
}
[3]=> array(2) {
[0]=> string(4) "Fiat"
[1]=> string(1) "3"
}
}
Two:
array(6) {
[0]=> string(7) "#581845"
[1]=> string(7) "#900C3F"
[2]=> string(7) "#C70039"
[3]=> string(7) "#FF5733"
[4]=> string(7) "#FFC300"
[5]=> string(7) "#DAF7A6"
}
Now, I need the first array combining with the second, excluding the elements of the second array that are not used by the first. At the end, I want to receive an array like:
[0]=> {
[0]=> "Ford",
[1]=> "1",
[2]=>"#581845"
}
[1]=>
...
}

Provided you always have more colors than auto makes, you can do this:
$makes = [
[
"Ford",
"1"
],
[
"Chevrolet",
"1"
],
[
"VW",
"1"
],
[
"Fiat",
"3"
]
];
$colors = [
"#581845",
"#900C3F",
"#C70039",
"#FF5733",
"#FFC300",
"#DAF7A6"
];
foreach($makes as &$currMakeTuple)
{
$currMakeTuple[] = array_shift($colors);
}
print_r($makes);
Array
(
[0] => Array
(
[0] => Ford
[1] => 1
[2] => #581845
)
[1] => Array
(
[0] => Chevrolet
[1] => 1
[2] => #900C3F
)
[2] => Array
(
[0] => VW
[1] => 1
[2] => #C70039
)
[3] => Array
(
[0] => Fiat
[1] => 3
[2] => #FF5733
)
)
You should probably check that condition and have a contingency for it.

Another one solution. Independent of the number of colors. Will add to brands only those colors which keys are equal to the brands' keys.
$brands = [
['Ford', 1],
['Chevrolet', 1],
['VW', 1],
['Fiat', 3],
];
$colors = [
'#581845',
'#900C3F',
'#C70039',
'#FF5733',
'#FFC300',
'#DAF7A6',
];
array_walk($brands, function(&$value, $key, $colors) {
if (isset($colors[$key])) {
$value = array_merge($value, [$colors[$key]]);
}
}, $colors);
print_r($brands);

I'm not saying you should do it this way, but I thought it was worth showing an alternative that used array functions instead:
var_dump(array_map(function($make, $color) {
$make[] = $color;
return $make;
}, $makes, array_slice($colors, 0, count($makes))));
I used:
array_slice to reduce the $colors array, should it be too long.
array_map with a lambda (anonymous function) that will apply the lambda to each element and return the final array upon completion.

Related

Get Unique from Multi-dimentional array based on one key - PHP [duplicate]

This question already has answers here:
Filter/Remove rows where column value is found more than once in a multidimensional array
(4 answers)
Closed 9 months ago.
Referring to this question & this, i have a little different problem here.
I have an array like this:
Array
(
[0] => Array
(
[name] => "Size"
[value] => "Large"
[id] => "1201"
)
[1] => Array
(
[name] => "Size"
[value] => "Small"
[id] => "1203"
)
[2] => Array
(
[name] => "Size"
[value] => "Medium"
[id] => "1204"
)
[3] => Array
(
[name] => "Size"
[value] => "Large"
[id] => "1205"
)
[4] => Array
(
[name] => "Size"
[value] => "Large"
[id] => "1206"
)
[5] => Array
(
[name] => "Size"
[value] => "Large"
[id] => "1207"
)
)
Above array have repetition of Large Three times, i want to identify unique on key based value. and remove that index (0,1,2,3,4,5) from that array.
Mentioned questions contains problems like this, but not the exact problem i am facing.
I am trying like this:
array_map("unserialize", array_unique(array_map("serialize", $input)));
but not working.
Since you have not answered my question yet I assume "id" is irrelevant.
By using array_column to make the array associative on "value" and it will delete any duplicates, then array_values will reset the keys to indexed.
This way you don't have to loop at all.
$arr = array_values(array_column($arr, NULL, "value"));
var_dump($arr);
output:
array(3) {
[0]=>
array(3) {
["name"]=>
string(4) "Size"
["value"]=>
string(5) "Large"
["id"]=>
string(4) "1207"
}
[1]=>
array(3) {
["name"]=>
string(4) "Size"
["value"]=>
string(5) "Small"
["id"]=>
string(4) "1203"
}
[2]=>
array(3) {
["name"]=>
string(4) "Size"
["value"]=>
string(6) "Medium"
["id"]=>
string(4) "1204"
}
}
https://3v4l.org/aOhVS
If you want to keep the lowest "id", and the "id" is higher the further down in the array you go (as in your example), then you can use rsort($arr); before the code.
rsort($arr);
$arr = array_values(array_column($arr, NULL, "value"));
var_dump($arr);
output:
array(3) {
[0]=>
array(3) {
["name"]=>
string(4) "Size"
["value"]=>
string(5) "Small"
["id"]=>
string(4) "1203"
}
[1]=>
array(3) {
["name"]=>
string(4) "Size"
["value"]=>
string(6) "Medium"
["id"]=>
string(4) "1204"
}
[2]=>
array(3) {
["name"]=>
string(4) "Size"
["value"]=>
string(5) "Large"
["id"]=>
string(4) "1201"
}
}
https://3v4l.org/VtgAM
You could try make a foreach creating another array like you need
$arrayOrdenado = array();
foreach($array as $a){
$arrayOrdenado[$a["value"]][] = $a;
}

nested array into simple nested array in php

I have an array
[0] => array(3) {
["id"] => string(1) "2"
["name"] => string(10) "Contractor"
["statuses"] => array(4) {
[1] => array(3) {
["id"] => string(1) "1"
["name"] => string(3) "NEW"
["count"] => string(2) "32"
}
[3] => array(3) {
["id"] => string(1) "3"
["name"] => string(8) "RETURNED"
["count"] => string(2) "20"
}
[5] => array(3) {
["id"] => string(1) "5"
["name"] => string(6) "FAILED"
["count"] => string(2) "46"
}
[58] => array(3) {
["id"] => string(2) "58"
["name"] => string(6) "REVISE"
["count"] => string(3) "197"
}
}
}
now when I convert into JSON it look like this
"items":[{"id":"2","name":"Contractor","statuses":{"1":{"id":"1","name":"NEW","count":"32"},"3":{"id":"3","name":"RETURNED","count":"20"},"5":{"id":"5","name":"FAILED","count":"46"},"58":{"id":"58","name":"REVISE","count":"197"}}}...
how to I remove the preceding 1, 3, 6 and 58 from array or JSON
I have tried array_values() but it is not converting the nested part of the array
how to i remove the preceding 1, 3, 6 and 58 from array or json
If you want json_encode() to return JSON array all the array keys must:
be numeric
be in sequence
the sequence must start from 0
For example:
$a = [1,2,3];
echo json_encode($a);
outputs desired
[1,2,3]
same with explicitly set indexes:
$a = [0=>1,2,3];
but
$a = [1=>1,2,3];
would output object:
{"1":1,"2":2,"3":3}
because sequence does not start from 0. Same for your case:
$a = [1,2,58=>3];
which produces
{"0":1,"1":2,"58":3}
because continuity of the key sequence is broken by index 58.
So depending on how you build your source array simply remove own keys with i.e. array_values():
$a = [1,2,58=>3];
echo json_encode(array_values($a)]);
would produce
[1,2,3]
so use array_values() on your $data['statuses'] and you are done.

split array based on value

I have array like this:
array(4) {
[0]=>
array(2) {
[0]=>
string(2) "L2"
[1]=>
string(5) "Apple"
}
[1]=>
array(2) {
[0]=>
string(2) "L3"
[1]=>
string(3) "Cat"
}
[2]=>
array(2) {
[0]=>
string(2) "L2"
[1]=>
string(6) "Orange"
}
[3]=>
array(2) {
[0]=>
string(2) "L3"
[1]=>
string(3) "Dog"
}
}
I want make a conclusion:
a) what are content of L2?
b) what are content of L3?
So I make array $L2 and $L3.
But, I have no idea how to push some array in one array.
I already try this one:
for($j=0; $j<count($arrL); $j++){
if($arrL[$j][0] == "L2"){
$L2[] = $arrL[$j][1];
}else if($arrL[$j][0] == "L3"){
$L3[] = $arrL[$j][1];
}
}
But, the result is:
array(0) {
}
you have any idea?
Your code is just fine. If I understand what do you want correctly then perhaps this might help you out. This is the code that I am using:
$arrL = array(
0 => array(
0 => "L2", 1 => "Apple"
),
1 => array(
0 => "L3", 1 => "Cat"
),
2 => array(
0 => "L2", 1 => "Orange"
),
3 => array(
0 => "L3", 1 => "Dog"
)
);
for($j=0; $j<count($arrL); $j++){
if($arrL[$j][0] == "L2"){
$L2[] = $arrL[$j][1];
}else if($arrL[$j][0] == "L3"){
$L3[] = $arrL[$j][1];
}
}
echo 'My L2 Array: ';
print_r($L2);
echo 'My L3 Array: ';
print_r($L3);
And here is the output:
My L2 Array: Array ( [0] => Apple [1] => Orange ) My L3 Array: Array ( [0] => Cat [1] => Dog )

Confusion with multidimensional arrays and merging

I have had success merging two arrays by difference using the following code:
$a=array("2013-08-22"=>"12","2013-08-25"=>"5","2013-08-27"=>"10");
$b=array("2013-08-22"=>"1","2013-08-23"=>"3","2013-08-25"=>"5","2013-08-27"=>"10","2013-08-29"=>"5");
foreach ($b as $key => $value){
if(!array_key_exists($key, $a)){
$a[$key]=0;
}
}
This will return:
Array
(
[2013-08-22] => 0
[2013-08-23] => 0
[2013-08-25] => 5
[2013-08-27] => 10
[2013-08-29] => 0
[2013-12-22] => 12
)
The idea is for a to additionally hold the elements from b that are not present in a.
I am having issues now doing the same thing for the following array format:
$a=array(array("2013-12-22","12"),array("2013-08-25","5"),array("2013-08-27","10"));
$b=array(array("2013-08-22","1"),array("2013-08-23","3"),array("2013-08-25","5"),array("2013-08-27","10"),array("2013-08-29","5"));
I went to try this:
foreach ($b as $key => $value){
if(!array_key_exists($key, $a)){
$a[$key]=array($value[0], 0);
}
}
But the returned result is far from what I need:
Array
(
[0] => Array
(
[0] => 2013-12-22
[1] => 12
)
[1] => Array
(
[0] => 2013-08-25
[1] => 5
)
[2] => Array
(
[0] => 2013-08-27
[1] => 10
)
[3] => Array
(
[0] => 2013-08-27
[1] => 0
)
[4] => Array
(
[0] => 2013-08-29
[1] => 0
)
)
I understand they keys are no longer the dates, but how should I go about checking each array and making sure I don't get double entries?
$a = array(
array("2013-12-22","12"),
array("2013-08-25","5"),
array("2013-08-27","10"));
$b = array(
array("2013-08-22","1"),
array("2013-08-23","3"),
array("2013-08-25","5"),
array("2013-08-27","10"),
array("2013-08-29","5"));
$exists = array();
foreach ($a as $data) {
$exists[$data[0]] = 1;
}
foreach ($b as $data) {
if (array_key_exists($data[0], $exists)) {
continue;
}
$a[] = array($data[0], $data[1]);
}
$a now contains:
array(6) {
[0]=>
array(2) {
[0]=>
string(10) "2013-12-22"
[1]=>
string(2) "12"
}
[1]=>
array(2) {
[0]=>
string(10) "2013-08-25"
[1]=>
string(1) "5"
}
[2]=>
array(2) {
[0]=>
string(10) "2013-08-27"
[1]=>
string(2) "10"
}
[3]=>
array(2) {
[0]=>
string(10) "2013-08-22"
[1]=>
string(1) "1"
}
[4]=>
array(2) {
[0]=>
string(10) "2013-08-23"
[1]=>
string(1) "3"
}
[5]=>
array(2) {
[0]=>
string(10) "2013-08-29"
[1]=>
string(1) "5"
}
}

how to combine two specific arrays keys and values together, in PHP?

i have two interesting arrays that im trying to combine together. Simple put:
$firstArr
array(3) {
[0] => array(2) {
[0] => string(1) "1"
[1] => string(16) "test1"
}
[1] => array(2) {
[0] => string(1) "8"
[1] => string(26) "test2"
}
[2] => array(2) {
[0] => string(1) "9"
[1] => string(23) "test3"
}
}
$secondArr
array(3) {
[0] => string(1) "1"
[1] => string(1) "2"
[2] => string(1) "3"
}
what i would like to get is something like this (not arrays):
$x = 1, 8, 8, 9, 9, 9;
$y = test1, test2, test2, test3, test3, test3;
basically the second array values dictates how many times the first array values are duplicated.
any ideas?
$firstArr=array(array("1","test1"),array("8","test2"),array("9","test3"));
$secondArr=array("1","2","3");
$x=$y='';
foreach ($secondArr as $k=>$v){
$x.= str_repeat($firstArr[$k][0].',',$v);
$y.= str_repeat($firstArr[$k][1].',',$v);
}
echo rtrim($x,",").";\n";
echo rtrim($y,",").";";
OUTPUT:
1,8,8,9,9,9;
test1,test2,test2,test3,test3,test3;
working demo:
http://codepad.org/yFvWs0Rh

Categories