split array based on value - php

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 )

Related

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

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.

Replacing inner keys from multidimensional array in PHP

Having the following array:
array(4) {
[0]=>
array(2) {
[0]=>
string(3) "233"
[1]=>
string(37) "some data"
}
[1]=>
array(2) {
[0]=>
string(3) "233"
[1]=>
string(68) "some other data"
}
[2]=>
array(2) {
[0]=>
string(3) "144"
[1]=>
string(38) "some other data"
}
[3]=>
array(2) {
[0]=>
string(3) "233"
[1]=>
string(42) "some other data"
}
}
I want to replace the values 233 and 144 (the key 0 from the inner array) by some random HEX color. The ones with the same keys (233) for example, has to have the same HEX color (FFF000 for example in the desired solution above).
This is the function I use to generate random HEX colors:
function randHEXcolor() {
return sprintf('%06X', mt_rand(0, 0xFFFFFF));
}
My desired output should be:
array(4) {
[0]=>
array(2) {
[0]=>
string(6) "FFF000"
[1]=>
string(37) "some data"
}
[1]=>
array(2) {
[0]=>
string(6) "FFF000"
[1]=>
string(68) "some other data"
}
[2]=>
array(2) {
[0]=>
string(6) "111333"
[1]=>
string(38) "some other data"
}
[3]=>
array(2) {
[0]=>
string(6) "FFF000"
[1]=>
string(42) "some other data"
}
}
How can I archieve this?
Thanks in advance.
foreach ($array as &$item) {
if (!isset($temp[$item[0]]) {
$temp[$item[0]] = randHEXcolor();
}
$item[0] = $temp[$item[0]];
}
If you want all values to be translated to the same random color, you'll have to save those colors:
$colors_translation = array();
foreach ($array as &$item) {
$color = $item[ 0 ];
$translate = $colors_translation[ $color ];
if (empty($translate)) {
$colors_translations[ $color ] = $translate = randHEXcolor();
}
$item[ 0 ] = $translate;
}
The solution using in_array and isset functions:
$keys = [];
foreach ($arr as &$v) { // $arr is your initial array
if (in_array($v[0], ['233', '144'])) {
if (!isset($keys[$v[0]])) $keys[$v[0]] = sprintf('%06X', mt_rand(0, 0xFFFFFF));
$v[0] = $keys[$v[0]];
}
}
print_r($arr);
The output:
Array
(
[0] => Array
(
[0] => 65A4BB
[1] => some data
)
[1] => Array
(
[0] => 65A4BB
[1] => some data
)
[2] => Array
(
[0] => DDB588
[1] => some data
)
[3] => Array
(
[0] => 65A4BB
[1] => some data
)
)
This code will create a color map as the array is traversed. Pre-populate $colorMap if you want pre-defined color translations.
<?php
$array = array(
0 => array(
0 => "233",
1 => "some data"
),
1 => array(
0 => "233",
1 => "some data"
),
2 => array(
0 => "144",
1 => "some data"
),
3 => array(
0 => "233",
1 => "some data"
),
);
$colorMap = array();
foreach ($array as &$inner) {
if (!array_key_exists($inner[0],$colorMap)) {
$newColor = randHEXcolor();
$colorMap[$inner[0]] = $newColor;
$inner[0] = $newColor;
} else {
$inner[0] = $colorMap[$inner[0]];
}
}
function randHEXcolor() {
return sprintf('%06X', mt_rand(0, 0xFFFFFF));
}
print_r($array);
print_r($colorMap);
Array
(
[0] => Array
(
[0] => F1519A
[1] => some data
)
[1] => Array
(
[0] => F1519A
[1] => some data
)
[2] => Array
(
[0] => 2F7D00
[1] => some data
)
[3] => Array
(
[0] => F1519A
[1] => some data
)
)
Array
(
[233] => F1519A
[144] => 2F7D00
)
Try:
<?php
$array = array(
0 => array(
0 => "233",
1 => "some data"
),
1 => array(
0 => "233",
1 => "some data"
),
2 => array(
0 => "144",
1 => "some data"
),
3 => array(
0 => "233",
1 => "some data"
),
);
function randHEXcolor() {
return sprintf('%06X', mt_rand(0, 0xFFFFFF));
}
$firstHex = randHEXcolor();
$secondHex = randHEXcolor();
foreach($array as $arrayIndex => &$arrayValue){
if($arrayValue[0] == "144"){
$arrayValue[0] = $firstHex;
}
if($arrayValue[0] == "233"){
$arrayValue[0] = $secondHex;
}
}
output:
array(4) {
[0]=>
array(2) {
[0]=>
string(6) "AB8248"
[1]=>
string(9) "some data"
}
[1]=>
array(2) {
[0]=>
string(6) "AB8248"
[1]=>
string(9) "some data"
}
[2]=>
array(2) {
[0]=>
string(6) "22AF8B"
[1]=>
string(9) "some data"
}
[3]=>
&array(2) {
[0]=>
string(6) "AB8248"
[1]=>
string(9) "some data"
}
}

Creating array using loop

I need to create array using loop, How to do it
Here is my array
$data = array(
value1 => 1,
value2 => 32,
value3 => 25
);
for (i=o,i<2,i++) {
}
If i value is 2 my array should be like
$arrays = (array(data,data));
If i value is 3my array should be like
$arrays=(array(data,data,data));
Help me to create array like this
If i i value is 2 means ouput should be like
result =(array(value1 => 1,value2 => 32,value3 => 25),(value1 => 1,value2 => 32,value3 => 25));
);
Do you mean this:
<?php
$data = array(
'value1' => 1,
'value2' => 32,
'value3' => 25
);
$finalArr = [];
for ($i=0;$i<2;$i++) {
$finalArr[] = $data;
}
print_r($finalArr);
Output:
Array
(
[0] => Array
(
[value1] => 1
[value2] => 32
[value3] => 25
)
[1] => Array
(
[value1] => 1
[value2] => 32
[value3] => 25
)
)
Your Eval sample
below code give output as
<?php
$data = array(
'value1' => 1,
'value2' => 32,
'value3' => 25
);
$finalArr = [];
for ($i=1;$i<=3;$i++) {
$finalArr['value'.$i] = $data['value'.$i];
}
print_r($finalArr);
?>
Array
(
[value1] => 1
[value2] => 32
[value3] => 25
)
if you dont need value1 in side array ie [value1]=>1
then remove 'value'.$i from $finalArr['value'.$i]
if you can change the value of $ according to the number of your array
So from what I understand, you want an array with a number of values...
$size = 5; # Size of the array
$array = array(); # The empty array to begin with
$value = array('value1' => '1', 'value2' => '32', 'value3' => '25');
// Create our array with a for loop
for($i=1; $i<=$size; $i++)
array_push($array, $value);
The var_dump of the array would be:
array(5) {
[0]=>
array(3) {
["value1"]=>
string(1) "1"
["value2"]=>
string(2) "32"
["value3"]=>
string(2) "25"
}
[1]=>
array(3) {
["value1"]=>
string(1) "1"
["value2"]=>
string(2) "32"
["value3"]=>
string(2) "25"
}
[2]=>
array(3) {
["value1"]=>
string(1) "1"
["value2"]=>
string(2) "32"
["value3"]=>
string(2) "25"
}
[3]=>
array(3) {
["value1"]=>
string(1) "1"
["value2"]=>
string(2) "32"
["value3"]=>
string(2) "25"
}
[4]=>
array(3) {
["value1"]=>
string(1) "1"
["value2"]=>
string(2) "32"
["value3"]=>
string(2) "25"
}
}

php switch array key with subelement key

I would like to switch the main keys(0,1,2) of an array with a subelement key(user_id).
For example, from this array:
array(3) {
[0]=>
array(3) {
["num_products_user_by_ref"]=>
string(1) "1"
["user_id"]=>
string(2) "77"
["reference"]=>
string(3) "E49"
}
[1]=>
array(3) {
["num_products_user_by_ref"]=>
string(1) "9"
["user_id"]=>
string(3) "526"
["reference"]=>
string(3) "E49"
}
[2]=>
array(3) {
["num_products_user_by_ref"]=>
string(2) "38"
["user_id"]=>
string(3) "346"
["reference"]=>
string(3) "E49"
}
}
I need :
array(952) {
[77]=>
array(2) {
["num_products_user_by_ref"]=>
string(1) "1"
["reference"]=>
string(3) "E49"
}
[526]=>
array(3) {
["num_products_user_by_ref"]=>
string(1) "9"
["reference"]=>
string(3) "E49"
}
[346]=>
array(3) {
["num_products_user_by_ref"]=>
string(2) "38"
["reference"]=>
string(3) "E49"
}
Every user_id could contains more than 1 pair num_products_user_by_ref/reference.
I remember that there is a function to achieve this (ksort?) associated to a custom function to implement.
$out = array();
foreach ($arr as $key => $value){
$out[$value['user_id']]["num_products_user_by_ref"] = $value["num_products_user_by_ref"];
$out[$value['user_id']]["reference"] = $value["reference"];
}
print_r($out);
Your question showed a structure that doesn't seem to fit with your comment "Every user_id could contains more than 1 pair num_products_user_by_ref/reference." So, here's another version that allows for that possibility:
$out = array();
foreach ($arr as $key => $value){
$entry = array("num_products_user_by_ref" => $value["num_products_user_by_ref"],
"reference" => $value["reference"]);
$out[$value['user_id']][] = $entry;
}
Output:
Array
(
[77] => Array
(
[0] => Array
(
[num_products_user_by_ref] => 1
[reference] => E49
)
[1] => Array
(
[num_products_user_by_ref] => 5
[reference] => E49
)
)
[526] => Array
(
[0] => Array
(
[num_products_user_by_ref] => 9
[reference] => E49
)
)
[346] => Array
(
[0] => Array
(
[num_products_user_by_ref] => 38
[reference] => E49
)
)
)
Here's another version for those who don't like traditional loops:
$out = array();
array_walk($arr, function($e, $k) use(&$out){
$entry = array("num_products_user_by_ref" => $e["num_products_user_by_ref"],
"reference" => $e["reference"]);
$out[$e['user_id']][] = $entry;
});

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

Categories