this is my array i want to process.
Array ( [0] => Array ( [checklist_id] => 3 [order_id] => [id] => 1 )
[1] => Array ( [checklist_id] => 4 [order_id] => [id] => 2 ) [2] =>
Array ( [checklist_id] => 7 [order_id] => 8,9,10,11,12 [id] => 4 ) );
after trying array_push
$alreadyassingorder=array();
foreach ($collection as $checklistorder) {
if($checklistorder['order_id'])
{
$order=explode(',', $checklistorder['order_id']);
array_push($alreadyassingorder,$order);
}
}
the output is
Array ( [0] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5
[5] => 6 [6] => 7 ) [1] => Array ( [0] => 8 [1] => 9 [2] => 10 [3] =>
11 [4] => 12 ) )
the output i want
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5
[5] => 6 [6] => 7 ) [7] => 8 [8] => 9 [9] => 10 [10] =>
11 [11] => 12 ) )
Try like this:
$alreadyassingorder=array();
foreach ($collection as $checklistorder) {
if($checklistorder['order_id'])
{
$order=explode(',', $checklistorder['order_id']);
foreach($order as $index=>$key):
array_push($alreadyassingorder,$key);
endforeach;
}
}
Related
I am working with multi dimensional array is like below with php,
$return_array= Array
(
[0] => Array
(
[0] => 3_Mar_2017
[1] => 0
[2] => 19
[3] => 7
[4] => 13
[5] => 3
[6] => 0
[7] => 42
)
[1] => Array
(
[0] => Yet to closed
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 1
[6] => 0
[7] => 1
)
[2] => Array
(
[0] => 3_Mar_2017
[1] => 0
[2] => 7
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 7
)
[3] => Array
(
[0] => 4_Apr_2017
[1] => 0
[2] => 8
[3] => 4
[4] => 0
[5] => 0
[6] => 0
[7] => 12
)
)
On 0th and 2nd indexs -> from sub array of that indexes -> 0th index are common "3_Mar_2017" ,I want to sum that two indexes and want result as shown below,
$final_return = Array
(
[0] => Array
(
[0] => 3_Mar_2017
[1] => 0
[2] => 26
[3] => 7
[4] => 13
[5] => 3
[6] => 0
[7] => 49
)
[1] => Array
(
[0] => Yet to closed
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 1
[6] => 0
[7] => 1
)
[2] => Array
(
[0] => 4_Apr_2017
[1] => 0
[2] => 8
[3] => 4
[4] => 0
[5] => 0
[6] => 0
[7] => 12
)
)
My tried code with loop as below,
$tem_array = array();
$final_return = array();
foreach ($return_array as $unique) {
if (!in_array($unique[0], $tem_array)) {
array_push($tem_array, $unique[0]);
$final_return[] = $unique;
} else {
$index = array_search($unique[0], $tem_array);
for ($i = 1; $i < count($unique); $i++) {
$final_return[$index][$i] = $final_return[$index][$i] + $unique[$i];
}
}
}
but if array size will large then ,may be it will take time is there any simple solution.
can any person help me to get this required result with minimum code ?
I will appreciate best answer.
hope this is what you are looking for
$temp1 = array(); $result = array();
foreach ($myArray as $temp) {
if (!in_array($temp[0], $temp1)) {
array_push($temp1, $temp[0]); $result[] = $temp;
} else {
$id = array_search($temp[0], $temp1); for ($i = 1; $i <= count($temp); $i++) {
$result[$id][$i] = $result[$id][$i] + $temp[$i];
}
}
}
your first array would look like
Array
(
[0] => Array
(
[0] => 13
[1] => 1
[2] => 2
[3] => 3
)
[1] => Array
(
[0] => 14
[1] => 5
[2] => 6
[3] => 7
)
[2] => Array
(
[0] => 13
[1] => 1
[2] => 2
[3] => 3
)
)
and the result be like
Array
(
[0] => Array
(
[0] => 13
[1] => 2
[2] => 4
[3] => 6
)
[1] => Array
(
[0] => 14
[1] => 5
[2] => 6
[3] => 7
)
)
I have an multidimensional array which contain some numbers, and i want to multiplying value of array which contain key 0 with each other key inside one area array and erase key 0.
Array
Array
(
[0] => Array
(
[0] => 3
[1] => 5
[2] => 5
[3] => 6
[4] => 7
)
[1] => Array
(
[0] => 2
[1] => 7
[2] => 4
[3] => 2
[4] => 8
)
[2] => Array
(
[0] => 4
[1] => 2
[2] => 3
[3] => 2
[4] => 5
)
)
Here's the result i want
Array
(
[0] => Array
(
[0] => 15
[1] => 15
[2] => 18
[3] => 21
)
[1] => Array
(
[0] => 14
[1] => 8
[2] => 4
[3] => 16
)
[2] => Array
(
[0] => 8
[1] => 12
[2] => 8
[3] => 20
)
)
I was already combine it using foreach and for, but it still not working for me, any idea how to do this?
The solution using array_map and array_slice functions:
// $arr is your initial array
foreach ($arr as &$v) {
$multiplier = $v[0];
$v = array_map(function($val) use($multiplier){
return $multiplier * $val;
}, array_slice($v, 1));
}
print_r($arr);
The output:
Array
(
[0] => Array
(
[0] => 15
[1] => 15
[2] => 18
[3] => 21
)
[1] => Array
(
[0] => 14
[1] => 8
[2] => 4
[3] => 16
)
[2] => Array
(
[0] => 8
[1] => 12
[2] => 8
[3] => 20
)
)
Hello sir below is my multidimensional array that contain some missing values on specific index
$array1 = Array
(
[2] => Array
(
[1] => 2
[2] => 4
[3] => 9
)
[3] => Array
(
[1] => 4
[2] => 6
[3] =>
)
[4] => Array
(
[1] => 4
[2] => 6
[3] => 7
)
[5] => Array
(
[1] => 2
[2] => 4
[3] =>
)
)
and below is my second array
I want to put the array2 value of index 3 and 4 in to the $array1 index 3 and 4 .but i dnt want to replace whole array value.
I just want to replace the those value that are null in the $array1
$array2 = Array
(
[3] => Array
(
[1] => 2
[2] => 4
[3] => 9
)
[5] => Array
(
[1] => 4
[2] => 6
[3] => 7
)
)
Required output below where i show the replace value in single qoutes )
$array1 = Array
(
[2] => Array
(
[1] => 2
[2] => 4
[3] => 9
)
[3] => Array
(
[1] => 4
[2] => 6
[3] => '9'
)
[4] => Array
(
[1] => 4
[2] => 6
[3] => 7
)
[5] => Array
(
[1] => 2
[2] => 4
[3] => '7'
)
This is it:
$array1 = array("2"=> array("1"=>"2", "2"=>"4", "3"=>"9"),
"3"=> array("1"=>"4", "2"=>"6", "3"=>""),
"4"=> array("1"=>"4", "2"=>"6", "3"=>"7"),
"5"=> array("1"=>"2", "2"=>"4", "3"=>"")
);
$array2 = array("3"=> array("1"=>"2", "2"=>"4", "3"=>"9"),
"5"=> array("1"=>"4", "2"=>"6", "3"=>"7")
);
foreach ($array1 as $key => $value) {
foreach ($value as $key2 => $value2) {
if($value2 == ""){
$array1[$key][$key2] = $array2[$key][$key2];
}
}
}
echo '<pre>';
print_r($array1);
echo '</pre>';
Output:
Array
(
[2] => Array
(
[1] => 2
[2] => 4
[3] => 9
)
[3] => Array
(
[1] => 4
[2] => 6
[3] => 9
)
[4] => Array
(
[1] => 4
[2] => 6
[3] => 7
)
[5] => Array
(
[1] => 2
[2] => 4
[3] => 7
)
)
So long story short, I have the array $total which looks like this:
Array (
[0] => Array
(
[0] => 1
[1] => 3
)
[1] => Array
(
[0] => 6
[1] => 7
[2] => 8
)
[2] => Array
(
[0] => 9
[1] => 10
)
)
However I need to pass it to a function that looks like this:
$count = cartesian(
Array("GH20"),
Array(1,3),
Array(6,7,8),
Array(9,10)
);
I have asked a similar question but I suspect the answer they gave was correct, I do not think asked the question in the right way. The cartesain function does not seem to accept:
$count = cartesian(
Array($total[0]),
Array($total[1]),
Array($total[2])
);
After some digging around I believe this is down to the fact that the arrays have keys which is not causing an error however instead of any values being returned, it just returns "array" for everything.
My question, in two parts then, is how do I remove the keys from the arrays so it is formatted as required. If possible, could the be done dynamically?
I have tried what everybody suggestes on google which is use the array_values() function, but this did not help
Any help is greatly appreciated, thanks, Nick
As requested, casertain function:
function cartesian() {
$_ = func_get_args();
if(count($_) == 0)
return array(array());
$a = array_shift($_);
$c = call_user_func_array(__FUNCTION__, $_);
$r = array();
foreach($a as $v)
foreach($c as $p)
$r[] = array_merge(array($v), $p);
return $r;
}
This is my desired output (Every permutation of ($total):
Array (
[0] => Array (
[0] => GH20
[1] => 1
[2] => 6
[3] => 9
)
[1] => Array (
[0] => GH20
[1] => 1
[2] => 6
[3] => 10
)
[2] => Array (
[0] => GH20
[1] => 1
[2] => 7
[3] => 9
)
[3] => Array (
[0] => GH20
[1] => 1
[2] => 7
[3] => 10
)
[4] => Array (
[0] => GH20
[1] => 1
[2] => 8
[3] => 9
)
[5] => Array (
[0] => GH20
[1] => 1
[2] => 8
[3] => 10
)
[6] => Array (
[0] => GH20
[1] => 3
[2] => 6
[3] => 9
)
[7] => Array (
[0] => GH20
[1] => 3
[2] => 6
[3] => 10
)
[8] => Array (
[0] => GH20
[1] => 3
[2] => 7
[3] => 9
)
[9] => Array (
[0] => GH20
[1] => 3
[2] => 7
[3] => 10
)
[10] => Array (
[0] => GH20
[1] => 3
[2] => 8
[3] => 9
)
[11] => Array (
[0] => GH20
[1] => 3
[2] => 8
[3] => 10
)
)
I have a two column array (array1), for each row of that array I need to compare the value in the 2nd column with a column value in each row of another array(array1) , when they equal I want to append another column value (from array2) to the first array.
in english:
if array1[x][1] = array2[y][0]
then array1[x][2] = array2[y][2]
screen dumps of both arrays
array1 (
[1] => Array (
[0] => 1 [1] => 2
)
[2] => Array (
[0] => 2 [1] => 3
)
[3] => Array (
[0] => 3 [1] =>
) [7] => Array (
[0] => 7 [1] => 1
)
[8] => Array (
[0] => 8 [1] => 1
)
[9] => Array (
[0] => 9 [1] => 10
)
[10] => Array (
[0] => 10 [1] => 2
)
)
array2 (
[0] => Array (
[0] => 1
[1] => 2
[2] => 2
[3] => Jane
[4] => Smith
[5] => jsmith#internet.com
[6] => jsmith
[7] => 12345
[8] => 1
[9] => no
)
[1] => Array (
[0] => 2
[1] => 2
[2] => 3
[3] => James
[4] => Beard
[5] => jasb#bellsouth.net
[6] => jbeard03
[7] => keeper
[8] => 1
[9] => no
)
[2] => Array (
[0] => 3
[1] => 2
[2] =>
[3] => Peter
[4] => Allen
[5] => pallen#rfgg.com
[6] => pallen
[7] => pallen
[8] => 1
[9] => no
)
[3] => Array (
[0] => 7
[1] => 2
[2] => 1
[3] => Joe
[4] => Blow
[5] => jasb#bellsouth.net
[6] => jblow
[7] => blow123
[8] => 5
[9] => yes
)
[4] => Array (
[0] => 8
[1] => 2
[2] => 1
[3] => John
[4] => Smith
[5] => logtest#bellsouth.net
[6] => jnsmith
[7] => jsmith123
[8] => 4
[9] => yes
)
[5] => Array (
[0] => 9
[1] => 2
[2] => 10
[3] => Frank
[4] => Smith
[5] => pallen#test.com
[6] => fsmith
[7] => fsmith123
[8] => 4
[9] => yes
)
[6] => Array (
[0] => 10
[1] => 2
[2] => 2
[3] => Loretta
[4] => Beard
[5] => lbeard#me.net
[6] => lbeard
[7] => lbeard123
[8] => 1
[9] => no
)
)
Does this work? I'm not sure I'm entirely clear on what you're looking for.
foreach($array1 as $x => $xarray) {
foreach($array2 as $y => $yarray) {
if($xarray[1] == $yarray[0]) {
$array1[$x][2] = $array[$y][2];
}
}
}
foreach ($array1 as &$a1) {
foreach ($array2 as $a2) {
if ($a1[1] == $a2[0]) {
$a1[] = $a2[2];
continue 2;
}
}
}
BTW, you should try to use explicit keys that actually mean something, e.g. $a1['id'] instead of $a1[1]. You'll thank yourself when you look at the code again two months down the road.
And because I threatened to do so:
btwyoushouldtrytouseexplicitkeysthatactuallymeansomethingeg$a1['id']insteadof$a1[1]youllthankyourselfwhenyoulookatthecodeagaintwomonthsdowntheroad ;-P