i got an array like this with unsorted outer index.
$a = array(
(1) => array(1, 2, 3, 0, 5, 4),
(0) => array(2, 1, 5, 0, 3, 4)
);
echo "<br/>Before Sorting: ";
print_r($a);
foreach($a as $b)
array_multisort($b, SORT_ASC, SORT_NUMERIC);
echo "<br/>After Sorting: ";
print_r($a);
which gives me output as below
Before Sorting:
Array
(
[1] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 0
[4] => 5
[5] => 4
)
[0] => Array
(
[0] => 2
[1] => 1
[2] => 5
[3] => 0
[4] => 3
[5] => 4
)
)
After Sorting:
Array
(
[1] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 0
[4] => 5
[5] => 4
)
[0] => Array
(
[0] => 2
[1] => 1
[2] => 5
[3] => 0
[4] => 3
[5] => 4
)
)
AND WHAT I WANT IS
$a = array(
(0) => array(2, 1, 5, 0, 3, 4),
(1) => array(1, 2, 3, 0, 5, 4)
);
please tell me how to deal with.........
How about just using ksort (as you need to reorder your array by key)?
$a = array(
1 => array(1, 2, 3, 0, 5, 4),
0 => array(2, 1, 5, 0, 3, 4)
);
echo "<br/>Before Sorting: ";
print_r($a);
ksort($a);
echo "<br/>After Sorting: ";
print_r($a);
Since you're only wanting to sort by the top level indices, you don't need to use multisort. You don't even need a loop.
Try this:
$a = array(
(1) => array(1, 2, 3, 0, 5, 4),
(0) => array(2, 1, 5, 0, 3, 4)
);
ksort($a);
print_r($a);
Should give you what you want.
See http://www.php.net/manual/en/function.ksort.php for more info.
Related
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 problem with matrix operation in php.
I have two multidimensional array
array1=
Array([0] => Array([0] => 5000, [1] => 6, [2] => 325, [3] => 3, [4] => 3, [5] => 517000000)
[1] => Array([0] => 20000, [1] => 5, [2] => 217, [3] => 5, [4] => 3, [5] => 1692000000)
[2] => Array([0] => 12150, [1] => 3, [2] => 254, [3] => 4, [4] => 4, [5] => 1370520000)
[3] => Array([0] => 4200, [1] => 4, [2] => 351, [3] => 3, [4] => 2, [5] => 394800000)
[4] => Array([0] => 24700, [1] => 7, [2] => 237, [3] => 3, [4] => 4, [5] => 2089620000)
[5] => Array([0] => 18500, [1] => 5, [2] => 314, [3] => 5, [4] => 4, [5] => 1739000000)
[6] => Array([0] => 12150, [1] => 5, [2] => 247, [3] => 3, [4] => 3, [5] => 1142100000)
[7] => Array([0] => 15000, [1] => 5, [2] => 307, [3] => 4, [4] => 3, [5] => 1410000000)
[8] => Array([0] => 10000, [1] => 4, [2] => 231, [3] => 4, [4] => 4, [5] => 940000000)
[9] => Array([0] => 27500, [1] => 6, [2] => 347, [3] => 5, [4] => 4, [5] => 2147483647))
the table display is
5000 6 325 3 3 517000000
20000 5 217 5 3 1692000000
12150 3 254 4 4 1370520000
4200 4 351 3 2 394800000
24700 7 237 3 4 2089620000
18500 5 314 5 4 1739000000
12150 5 247 3 3 1142100000
15000 5 307 4 3 1410000000
10000 4 231 4 4 940000000
27500 6 347 5 4 2147483647
and the second array is
array2=
Array(
[0] => Array
(
[0] => 27500
[1] => 7
[2] => 351
[3] => 3
[4] => 4
[5] => 394800000
))
then, I want to do mathematical operation for each column.
for column 0,1,2,4 the operation is array1[][]/array2[][].
and for column 3 and 5 the operation is array2[][]/array1[][]
how do I do that ?
I have tried for this function :
function perkalian_matriks($array1, $array2) {
$result= array();
for ($i=0; $i<sizeof($array1); $i++) {
for ($j=0; $j<sizeof($array2[0]); $j++) {
$temp = 0;
$temp1 = 0;
$temp2 = 0;
for ($k=0; $k<sizeof($array2); $k++) {
if(isset($array1[$i][3]) || isset($array1[$i][5])){
$temp1 += ($array2[0][$j]/$array1[$i][$j])/2;
}else{
$temp2 += $array1[$i][$j]/$array2[0][$j];
}
$temp += $array1[$i][$j]/$array2[0][$j];
}
$result[$i][$j] = $temp;
$result1[$i][$j] = $temp1;
$result2[$i][$j] = $temp2;
}
}
return $result;
return $temp;
return $result1;
return $result2;
}
But the final result that I expected is
0.181818 0.857143 0.925926 1 0.75 0.763636
0.727273 0.714286 0.618234 0.6 0.75 0.233333
0.441818 0.428571 0.723647 0.75 1 0.288066
0.152727 0.571429 1 1 0.5 1
0.898182 1 0.675214 1 1 0.188934
0.672727 0.714286 0.894587 0.6 1 0.227027
0.441818 0.714286 0.703704 1 0.75 0.345679
0.545455 0.714286 0.874644 0.75 0.75 0.28
0.363636 0.571429 0.65812 0.75 1 0.42
1 0.857143 0.988604 0.6 1 0.183843
Thank you.
first of all doing this:
return $result;
return $temp;
return $result1;
return $result2;
will execute every time just return $result; so that really ve no meaning.
If called from within a function, the return statement immediately
ends execution of the current function, and returns its argument as
the value of the function call. ( from php doc )
The second array don't need to be a matrix.
anyway you can achieve what you need using 2 nested loop:
$array1=
array(array(5000,6, 325, 3, 3, 517000000),
array( 20000, 5, 217, 5, 3, 1692000000),
array( 12150, 3, 254, 4, 4, 1370520000),
array( 4200, 4, 351, 3, 2, 394800000),
array( 24700, 7, 237, 3, 4, 2089620000),
array( 18500, 5, 314, 5, 4, 1739000000),
array( 12150, 5, 247, 3, 3, 1142100000),
array( 15000, 5, 307, 4, 3, 1410000000),
array( 10000, 4, 231, 4, 4, 940000000),
array( 27500, 6, 347, 5, 4, 2147483647));
$array2=array(27500,
7,
351,
3,
4,
394800000);
$action1 = array(0,1,2,4);
$action2 = array(3,5);
$result = array();
foreach($array1 as $rowNum => $row){
$result[] = array();
foreach($row as $colNum => $col){
if(in_array($colNum,$action1))
$result[$rowNum][] = $col/$array2[$colNum];
else if(in_array($colNum,$action2)){
$result[$rowNum][] = $array2[$colNum]/$col;
}
}
}
echo var_dump($result);
Working Example
I want to ask about how to replace inner key in a multidimensional array.
I have an multidimensional array :
$array1=
array(array(5000, 6, 325, 3, 3, 517000000),
array( 20000, 5, 217, 5, 3, 1692000000)
);
The second array is
$array2=array(1,2,3,4,5,6);
I expected the new array is
Array(
[0] => Array
(
[1] => 5000
[2] => 6
[3] => 325
[4] => 3
[5] => 3
[6] => 517000000
)
[1] => Array
(
[1] => 20000
[2] => 5
[3] => 217
[4] => 5
[5] => 3
[6] => 1692000000
))
I have tried this code below by another post PHP Replace multidimensional array keys, but I can't assign the value of my array1
foreach($array2 as $array2 ){
for($k=0;$k<sizeof($array2);$k++){
for($l=0;$l<$count;$l++){
$last[$l][$array2] = $array1[$k][$l];
}
$i += $count;
}
}
Thank you
As #Rizier has suggested in his comment you can do this using array_map() and array_combine().
<?php
$array1=
array(array(5000, 6, 325, 3, 3, 517000000),
array( 20000, 5, 217, 5, 3, 1692000000)
);
$array2 = array(1, 2, 3, 4, 5, 6);
foreach($array1 as $arr1){
$array3[] = array_combine($array2, $arr1);
}
var_dump($array3);
output
array (size=2)
0 =>
array (size=6)
1 => int 5000
2 => int 6
3 => int 325
4 => int 3
5 => int 3
6 => int 517000000
1 =>
array (size=6)
1 => int 20000
2 => int 5
3 => int 217
4 => int 5
5 => int 3
6 => int 1692000000
try
<?php
$array1=
array(array(5000, 6, 325, 3, 3, 517000000),
array( 20000, 5, 217, 5, 3, 1692000000)
);
$newArray = array();
foreach($array1 as $arr){
array_unshift($arr,'');
unset($arr[0]);
$newArray[] = $arr;
}
print_r($newArray);
this will have same output you require.
hope it helps :)
[a, 1, 3, 9, 0, 13]
[b, 5, 6, 0, 0, 11]
[j, 0, 6, 2, 1, 9]
[c, 1, 0, 8, 5, 14]
[d, 0, 0, 0, 17, 17]
[e, 0, 5, 0, 0, 5]
[h, 0, 0, 3, 3, 6]
The array needs to be sorted on
Ascending order of number of zeroes.
Ascending order of last element value.
So the above array after sorting should look like,
[j, 0, 6, 2, 1, 9]
[a, 1, 3, 9, 0, 13]
[c, 1, 0, 8, 5, 14]
[h, 0, 0, 3, 3, 6]
[b, 5, 6, 0, 0, 11]
[d, 0, 5, 0, 0, 5]
[d, 0, 0, 0, 17, 17]
I am sorting the multidimensional array normally against the last value via this code
function multiarraysorter($arr, $index) {
$b = array();
$c = array();
foreach ($arr as $key => $value) {
$b[$key] = $value[$index];
}
asort($b);
foreach ($b as $key => $value) {
$c[] = $arr[$key];
}
return $c;
}
Any ideas how to accomplish the first sort that is based on number of zeroes of the values?
You can use usort for tasks like this:
$arr=[['a', 1, 3, 9, 0, 13],['b', 5, 6, 0, 0, 11],['j', 0, 6, 2, 1, 9],['c', 1, 0, 8, 5, 14],['d', 0, 0, 0, 17, 17],['e', 0, 5, 0, 0, 5],['h', 0, 0, 3, 3, 6]];
usort($arr,function($a,$b){
$infoa=array_count_values($a);
$infob=array_count_values($b);
if(empty($infoa[0])) $infoa[0]=0;
if(empty($infob[0])) $infob[0]=0;
if($infoa[0]==$infob[0])
{
return end($a)-end($b);
}
else
{
return $infoa[0]-$infob[0];
}
});
print_r($arr);
3v4l.org demo
The generated output is:
Array
(
[0] => Array
(
[0] => j
[1] => 0
[2] => 6
[3] => 2
[4] => 1
[5] => 9
)
[1] => Array
(
[0] => a
[1] => 1
[2] => 3
[3] => 9
[4] => 0
[5] => 13
)
[2] => Array
(
[0] => c
[1] => 1
[2] => 0
[3] => 8
[4] => 5
[5] => 14
)
[3] => Array
(
[0] => h
[1] => 0
[2] => 0
[3] => 3
[4] => 3
[5] => 6
)
[4] => Array
(
[0] => b
[1] => 5
[2] => 6
[3] => 0
[4] => 0
[5] => 11
)
[5] => Array
(
[0] => e
[1] => 0
[2] => 5
[3] => 0
[4] => 0
[5] => 5
)
[6] => Array
(
[0] => d
[1] => 0
[2] => 0
[3] => 0
[4] => 17
[5] => 17
)
)
Try This its working :
<?php
$res_ary = array();
$res_ary = sortArray($arr,'key_value');
?>
in sortArray function you pass two parameters first one is array you want to short and second one is key value on which you have to do sorting.
<?php
function sortArray($arrData, $p_sort_field, $p_sort_type = false )
{
if(!empty($arrData))
{
foreach($arrData as $data)
{
$newData [] = $data;
}
for($i=0; $i<count($newData); $i++)
{
$ar_sort_field[$i]=$newData[$i][$p_sort_field];
}
array_multisort($ar_sort_field, ($p_sort_type ? SORT_DESC : SORT_ASC), $newData);
return $newData;
}
}
?>
I have two arrays, I want to merge these two arrays into single array. Please view the detail below:
First Array:
Array
(
[0] => Array
(
[a] => 1
[b] => 2
[c] => 3
)
[1] => Array
(
[a] => 3
[b] => 2
[c] => 1
)
)
Second Array:
Array
(
[0] => Array
(
[d] => 4
[e] => 5
[f] => 6
)
[1] => Array
(
[d] => 6
[e] => 5
[f] => 4
)
)
I want this result. Does somebody know how to do this?
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 3
[1] => 2
[2] => 1
)
[2] => Array
(
[0] => 4
[1] => 5
[2] => 6
)
[3] => Array
(
[0] => 6
[1] => 5
[2] => 4
)
)
Hope you have understand the question.
Thank you in advance.
Try array_merge:
$result = array_merge($array1, $array2);
FIXED (again)
function array_merge_to_indexed () {
$result = array();
foreach (func_get_args() as $arg) {
foreach ($arg as $innerArr) {
$result[] = array_values($innerArr);
}
}
return $result;
}
Accepts an unlimited number of input arrays, merges all sub arrays into one container as indexed arrays, and returns the result.
EDIT 03/2014: Improved readability and efficiency
more simple and modern way is:
$merged = $array1 + ['apple' => 10, 'orange' => 20] + ['cherry' => 12, 'grape' => 32];
new array syntax from php 5.4
If you want to return the exact result you specify in your question then something like this will work
function array_merge_no_keys() {
$result = array();
$arrays = func_get_args();
foreach( $arrays as $array ) {
if( is_array( $array ) ) {
foreach( $array as $subArray ) {
$result[] = array_values( $subArray );
}
}
}
return $result;
}
As a purely native function solution, merge the arrays, then reindex each subarray.
Code: (Demo)
$a = [
['a' => 1, 'b' => 2, 'c' => 3],
['a' => 3, 'b' => 2, 'c' => 1],
];
$b = [
['d' => 4, 'e' => 5, 'f' => 6],
['d' => 6, 'e' => 5, 'f' => 4],
];
var_export(
array_map('array_values' array_merge($a, $b))
);
Output:
array (
0 =>
array (
0 => 1,
1 => 2,
2 => 3,
),
1 =>
array (
0 => 3,
1 => 2,
2 => 1,
),
2 =>
array (
0 => 4,
1 => 5,
2 => 6,
),
3 =>
array (
0 => 6,
1 => 5,
2 => 4,
),
)