I have original array as given below
$orginal_arra = Array(
[0] => 53_0.1,
[1] => 53_0.3,
[2] => 54_0.2,
[3] => 54_0.1,
[4] => 54_0.1,
[5] => 54_0.19,
[6] => 56_0.19,
[7] => 56_0.50
);
The above array can explode by "_" separating
foreach($orginal_arra as $part) {
if($part){
$arr_parts = explode("_",$part);
$arr1[] = $arr_parts[0];
$arr2[] = $arr_parts[1];
}
}
The $arr1 result after sperating
Array (
[0] => 53,
[1] => 53,
[2] => 54,
[3] => 54,
[4] => 54,
[5] => 54,
[6] => 56,
[7] => 56
)
The $arr2 result after sperating
Array (
[0] => 0.1,
[1] => 0.3,
[2] => 0.2,
[3] => 0.1,
[4] => 0.1,
[5] => 0.19,
[6] => 0.19,
[7] => 0.50
)
I want to return the array by combining $arr1 and $arr2 as like
$array = Array(
[0] => 53_0.3,
[1] => 54_0.19,
[2] => 56_0.50
);
Is there any way to get the result like above using PHP Arrays.
You can use array_values on grouping the array into an associative array. Use array_values to convert the associative array into a simple array.
$orginal_arra = array(
'53_0.1',
'53_0.3',
'54_0.2',
'54_0.1',
'54_0.1',
'54_0.19',
'56_0.19',
'56_0.50'
);
$result = array_values(array_reduce($orginal_arra, function($c,$v){
$key = explode('_', $v); //explode the value
$c[$key[0]] = $v; //Use the first element of the explode value as a key and just overide the value
return $c;
}, array()));
echo "<pre>";
print_r( $result );
echo "</pre>";
This will result to:
Array
(
[0] => 53_0.3
[1] => 54_0.19
[2] => 56_0.50
)
strstr() is the perfect temporary key generator. Each recurring key will overwrite the previous value. When the loop finishes, just reindex the result array with array_values(). Clean and simple.
Code: (Demo)
$original_array = Array(
'53_0.1',
'53_0.3',
'54_0.2',
'54_0.1',
'54_0.1',
'54_0.19',
'56_0.19',
'56_0.50'
);
foreach ($original_array as $v) {
$result[strstr($v, "_", true)] = $v;
}
var_export(array_values($result));
Output:
array (
0 => '53_0.3',
1 => '54_0.19',
2 => '56_0.50',
)
You can do this if you want to combine $array1 and $array2:
<?php
$array1= array(
"0" => 53,
"1" => 53,
"2" => 54,
"3" => 54,
"4" => 54,
"5" => 54,
"6" => 56,
"7" => 56
);
$array2 = array (
"0" => 0.1,
"1" => 0.3,
"2" => 0.2,
"3" => 0.1,
"4" => 0.1,
"5" => 0.19,
"6" => 0.19,
"7" => 0.50
);
$array1 = array_reverse($array1, true);
$array1 = array_unique($array1);
foreach($array1 as $key=>$val){
$array1[$key] = $val."_".number_format((float)$array2[$key], 2, '.', '');
}
print_r(array_reverse($array1));
Output:
Array
(
[0] => 53_0.30,
[1] => 54_0.19,
[2] => 56_0.50
)
Related
I have multiple arrays structured like these:
$array1 = ["aaa" => 1, "bbb" => 1];
$array2 = ["aaa" => 12, "bbb" => 12];
$array3 = ["bbb" => 15, "ccc" => 15];
meaning:
every array has the same value for each key (eg: array1 has value "1" for every item in the array) but there are no arrays sharing the same value (eg: if array1 has value 1, then none of the other arrays has value = 1)
the arrays may or may not share the same keys
I need to combine these arrays in a way that the final result is something like this:
$result = [
"aaa" => [1,12],
"bbb" => [1,12,15],
"ccc" => [15],
];
meaning:
the final array must contain all the keys from the previous arrays
the value of the key is an array composed of all the values of the previous arrays that shared the same key
I know it's a bit messy, but I hope it is clear enough. I'm struggling to build the $result array. I tried merge, combine, intersect, but none of them seems to work. Is there a way to build the $result array without using a loop?
Thanks
Does it match your goal ?
<?php
$array1 = ["aaa" => 1, "bbb" => 1];
$array2 = ["aaa" => 12, "bbb" => 12];
$array3 = ["bbb" => 15, "ccc" => 15];
$array = array_merge_recursive($array1, $array2, $array3);
print_r($array);
?>
outputs
Array
(
[aaa] => Array
(
[0] => 1
[1] => 12
)
[bbb] => Array
(
[0] => 1
[1] => 12
[2] => 15
)
[ccc] => 15
)
Merge all of array into an mergedArray. Then use 2 foreach to set it.
<?php
$array1 = ["aaa" => 1, "bbb" => 1];
$array2 = ["aaa" => 12, "bbb" => 12];
$array3 = ["bbb" => 15, "ccc" => 15];
$mergedArray = [$array1, $array2, $array3];
$result = [];
foreach ($mergedArray as $array) {
foreach ($array as $key => $item) {
$result[$key][] = $item;
}
}
echo '<pre>';
print_r($result);
echo '</pre>';
exit;
?>
The result:
Array
(
[aaa] => Array
(
[0] => 1
[1] => 12
)
[bbb] => Array
(
[0] => 1
[1] => 12
[2] => 15
)
[ccc] => Array
(
[0] => 15
)
)
I need help merging two PHP arrays:
Array 1:
Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 6
)
Array 2:
Array
(
[0] => Array
(
[id_sabor] => 2
[chocolate] => N
)
[1] => Array
(
[id_sabor] => 3
[chocolate] => S
)
[2] => Array
(
[id_sabor] => 4
[chocolate] => N
)
[3] => Array
(
[id_sabor] => 5
[chocolate] => S
)
[4] => Array
(
[id_sabor] => 6
[chocolate] => N
)
)
The values on array 1 are the active objects. I need to keep on Array 2 or on a new array only the ones with an [id_sabor] that matches in the array 1 (in this case: 2, 3, 4 and 6). Also, on those that [chocolate]=S add a new value: [costo_extra]=25.
One way to do that could be to use array_reduce and use in_array to check if the first array contains the value of id_sabor.
$array1 = [2, 3, 4, 6];
$array2 = [
["id_sabor" => 1, "chocolate" => "N"],
["id_sabor" => 2, "chocolate" => "N"],
["id_sabor" => 3, "chocolate" => "S"],
["id_sabor" => 4, "chocolate" => "N"],
["id_sabor" => 5, "chocolate" => "S"],
["id_sabor" => 6, "chocolate" => "N"]
];
$array2 = array_reduce($array2, function($carry, $item) use ($array1){
if (in_array($item["id_sabor"], $array1)) {
if ($item["chocolate"] === "S") {
$item["costo_extra"] = 25;
}
$carry[] = $item;
}
return $carry;
});
Demo
I have data below :
Array(
[A] => Array
(
[AA] => 10
)
[B] => Array
(
[BA] => 5
[BB] => 1
[BC] => -2
)
[C] => Array
(
[CA] => 3
[CB] => 0
)
)
I want to sum the value of second element my array (BA,BB,BC, etc) like this :
Array(
[A] => 10
[B] => 4
[C] => 3
)
I've tried to do with foreach (I'm using php as my platform) but the result is wrong, can someone give me explanation and the logic to solve this? thanks
You can loop thru your array and use array_sum
$arr = array(
"A" => array
(
"AA" => 10,
),
"B" => array
(
"BA" => 5,
"BB" => 1,
"BC" => -2
),
"C" => array
(
"CA" => 3,
"CB" => 0
)
);
$result = array();
foreach( $arr as $key => $val ){
$result[$key] = array_sum ( $val );
}
echo "<pre>";
print_r( $result );
echo "</pre>";
This will result to:
Array
(
[A] => 10
[B] => 4
[C] => 3
)
Doc: http://php.net/manual/en/function.array-sum.php
This should work for arrays like the one of your example:
$arr = array(
"A" => array
(
"AA" => 10,
),
"B" => array
(
"BA" => 5,
"BB" => 1,
"BC" => -2
),
"C" => array
(
"CA" => 3,
"CB" => 0
)
);
$res = array();
foreach($arr as $key => $value) {
foreach($value as $number) {
(!isset($res[$key])) ?
$res[$key] = $number :
$res[$key] += $number;
}
}
echo "<pre>";
print_r( $res );
echo "</pre>";
This is working without using an inbuilt function.
if you want sum column
<?php
$array = array
(
"A"=>array
(
"AA" => 10,
),
"B"=>array
(
"BA" => 5,
"BB" => 1,
"BC" => -2
),
"C"=>array
(
"CA" => 3,
"CB" => 0
)
);
foreach ($array as $key=>$value)
{
$mehrdad[]=$key;
}
foreach ($mehrdad as $key1=>$value1)
{
$arrays=$array[$value1];
foreach ($arrays as $key2=>$value2)
{
$mehrdadi[]=$key2;
}
}
$mehrdadend=array_unique($mehrdadi);
$mehrdadis = array();
foreach ($mehrdadend as $key3=>$value3)
{
$sum=array_sum(array_column($array, $value3));
$mehrdadis[$value3] = $sum;
}
print_r($mehrdadis);
?>
Result
Array
(
[AA] => 10
[BA] => 5
[BB] => 1
[BC] => -2
[CA] => 3
[CB] => 0
)
Consider the following array returned from an API:
$arr = Array
(
[A] =>
[C] =>
[D] =>
[EE] => Array
(
[0] => Array
(
[B] =>
[C] => 2.06
[O] =>
[P] => 0
[T] => 1
)
[1] => Array
(
[B] =>
[C] => 2.56
[O] =>
[P] => 0
[T] => 2
)
[2] => Array
(
[B] =>
[C] => 4.94
[O] =>
[P] => 0
[T] => 3
)
[3] => Array
(
[B] =>
[C] => 1.42
[O] =>
[P] => 1
[T] => 9
)
[4] => Array
(
[B] =>
[C] => 2.83
[O] =>
[P] => 1
[T] => 10
)
[5] => Array
(
[B] =>
[C] => 2.13
[O] =>
[P] => 1.5
[T] => 9
)
[6] => Array
(
[B] =>
[C] => 1.7
[O] =>
[P] => 1.5
[T] => 10
)
)
)
I want to get the C value from the sub array where P is 1.5 and T is 9. Obviously if I knew this would always be in sub array with index 5 I could just do this:
$arr['EE'][5]['C'];
But 5 will not always be the index of that particular array. So I'm looking for something more along the lines of:
$arr['EE'][...P is 1.5 and T is 9...]['C'];
This is within a loop that processes thousands of these arrays, so performance is definitely a consideration. In other words, I don't want to do a bunch of nested loops to find that value - I'm looking for a built-in PHP function for this (or a combination thereof) if it exists.
You can also use array_reduce
$arr = array
(
"A" => "",
"C" => "",
"D" => "",
"EE" => array
(
"0" => array
(
"B" => "",
"C" => 2.06,
"O" => "",
"P" => 0,
"T" => 1,
),
"1" => array
(
"B" => "",
"C" => 2.56,
"O" => "",
"P" => 0,
"T" => 2,
),
"2" => array
(
"B" => "",
"C" => 4.94,
"O" => "",
"P" => 0,
"T" => 3,
),
"3" => array
(
"B" => "",
"C" => 1.42,
"O" => "",
"P" => 1,
"T" => 9,
),
"4" => array
(
"B" => "",
"C" => 2.83,
"O" => "",
"P" => 1,
"T" => 10,
),
"5" => array
(
"B" => "",
"C" => 2.13,
"O" => "",
"P" => 1.5,
"T" => 9,
),
"6" => array
(
"B" => "",
"C" => 1.7,
"O" => "",
"P" => 1.5,
"T" => 10,
),
)
);
$p = 1.5;
$t = 9;
$result = array_reduce( $arr["EE"], function( $c, $v ) use ($p,$t) {
if ( $v["P"] == $p && $v["T"] == $t ) $c = $v["C"];
return $c;
}, "" );
echo $result;
This will result to:
2.13
Other option: You can use array_filter
$p = 1.5;
$t = 9;
$result = array_filter( $arr["EE"], function( $v ) use ($p,$t) {
return $v["P"] == $p && $v["T"] == $t;
} );
$result = array_values( $result );
echo "<pre>";
print_r( $result );
echo "</pre>";
This will result to
Array
(
[0] => Array
(
[B] =>
[C] => 2.13
[O] =>
[P] => 1.5
[T] => 9
)
)
So accessing C is $result[0]["C"]
Doc: array_filter
Or for performance, you can use foreach loop
$p = 1.5;
$t = 9;
$result = "";
foreach( $arr["EE"] as $v ) {
if ( $v["P"] == $p && $v["T"] == $t ) $result = $v["C"];
}
echo $result;
I have two arrays
First Array
(
[0] => Array
(
[352] => 1
[128] =>
[64] =>
[70] => 2
)
)
Second array is like this :
Array
(
[0] => Array
(
[128] => 1.5
)
)
I want to make final array like this.(i want to store the matching into the main array in this example it is 128 -> 1.5) how can i do it.?
Array
(
[0] => Array
(
[352] => 1
[128] => 1.5
[64] =>
[70] => 2
)
)
here is my array variables:
print_r($listskilssresult);
print_r($listskilssresultmatching);
You need to use array_replace_recursive
$listskilssresult = [
[
352 => 1,
128 => '',
64 => '',
70 => 2
]
];
$listskilssresultmatching = [
[
128 => 1.5
]
];
print_r(array_replace_recursive($listskilssresult, $listskilssresultmatching));
Prints :
Array
(
[0] => Array
(
[352] => 1
[128] => 1.5
[64] =>
[70] => 2
)
)
Know the difference between array_replace_recursive and array_merge_recursive here
This is specific to your question. If you want to make something more automated, you can create a function. But this will do what you want:
<?php
$array1 = [[352 => 1, 128 => null, 64 => null, 70 => 2]];
$array2 = [[128 => 1.5]];
$keys1 = array_keys($array1[0]);
$keys2 = array_keys($array2[0]);
foreach ($keys1 as $key => $value) {
if (in_array($value, $keys2)) {
$array1[0][$value] = $array2[0][$value];
unset($array2[0][$value]);
}
}
if (!empty($array2[0])) {
foreach ($array2[0] as $key => $value) {
$array1[0][$key] = $value;
unset($array2[0][$key]);
}
}
print_r($array1[0]);
?>
The last if statement will add key + value from the 2nd array to the first if no match was found for them (from the foreach statement). You can just delete that condition if you just want to add only matching keys.
For this solution, you have to use array_merge() built-in php function.
Syntax:
$finalArray = array_merge($array1, $array2);
print_r($finalArray)
Example:
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
Output:
Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)
Reference : http://php.net/manual/en/function.array-merge.php
array_replace_recursive is the best solution!
$old_array = Array('my_index1' => Array('1' => 'A', '2' => 'B'), 'my_index2' => Array('1' => 'C', '2' => 'D'));
$new_array = Array('my_index2' => Array('2' => 'Z'));
$result = array_replace_recursive($old_array, $new_array);
//Result : Array('my_index1' => Array('1' => 'A', '2' => 'B'), 'my_index2' => Array('1' => 'C', '2' => 'Z'));
array_merge would do the job for you:
array_merge($listskilssresult,$listskilssresultmatching)