Addition of two matrix using loop [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
These are my two matrices in four arrays:
Array
(
[0] => Array
(
[0] => 1
[1] => 2
)
[1] => Array
(
[0] => 4
[1] => 5
)
)
Array
(
[0] => Array
(
[0] => 1
[1] => 2
)
[1] => Array
(
[0] => 4
[1] => 5
)
)
How can I add up these matrices using loop?

Try this:-
<?php
$a1 = Array('0' => Array('0' => 1,'1' => 2),'1' => Array('0' => 4,'1' => 5));
$a2 = Array('0' => Array('0' => 1,'1' => 2),'1' => Array('0' => 4,'1' => 5));
$sumArray = array();
$result = array();
for($i=0; $i<=1; $i++) {
for($j=0; $j<=1; $j++) {
$result[$i][$j] = $a1[$i][$j] + $a2[$i][$j];
}
}
echo "<pre/>";print_r($result);
?>
Output:- http://prntscr.com/75hoqi

try this:
$result = array();
for($i=0; $i<=2; $i++) {
for($j=0; $j<=2; $j++) {
$result[$i][$j] = $matrix1[$i][$j] + $matrix2[$i][$j];
}
}

Go with array_merge() thats will merge the array into one

Related

Change keys to multidimensional array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Here is my array
Array
(
[0] => Array
(
[0] => 710715609
[1] => 3
[2] => 2020-02-28 00:01:01/2020-02-25 00:01:01/2020-02-21 00:01:01
[3] => 2
[4] => sports/Mtunes/Astro/D50
[5] => sports-1/Mtunes-1/Astro-1/D50-2
)
[1] => Array
(
[0] => 119774100
[1] => 2
[2] => 2020-02-22 00:01:01/2020-02-22 00:01:01
[3] => 1
[4] => sports/D50
[5] => sports-1/D50-1
)
)
I want to change these 5 keys in array to (msisdn, logCount, logins, transc, actVas) like this and I have tried by using array_fill_keys
Is there a way to do this ?
You can map and combine:
$array = array_map(function($v) {
return array_combine(["msisdn", "logCount", "logins",
"transc", "actVas", "vasCount"], $v);
}, $array);
Or walk and combine:
array_walk($array, function(&$v) {
$v = array_combine(["msisdn", "logCount", "logins",
"transc", "actVas", "vasCount"], $v);
});
You can do either one with a defined array and use() like:
$k = ["msisdn", "logCount", "logins", "transc", "actVas", "vasCount"];
$array = array_map(function($v) use($k) { return array_combine($k, $v); }, $array);
//or
array_walk($array, function(&$v) use($k) { $v = array_combine($k, $v); });
Assuming you may have a very large array, this will just replace the existing arrays keys rather than making another copy of the array.
$pushed = [
[ 710715609, 3, '2020-02-28 00:01:01/2020-02-25 00:01:01/2020-02-21 00:01:01', 2, 'sports/Mtunes/Astro/D50', 'sports-1/Mtunes-1/Astro-1/D50-2'],
[ 119774100, 2, '2020-02-22 00:01:01/2020-02-22 00:01:01', 1, 'sports/D50', 'sports-1/D50-1']
];
$names = ["msisdn", "logCount", "logins", "transc", "actVas", "vasCount"];
foreach ($pushed as $x => $push){
foreach ($push as $i => $v) {
unset ($pushed[$x][$i]);
$pushed[$x][$names[$i]] = $v;
}
}
print_r($pushed);
RESULT
Array
(
[0] => Array
(
[msisdn] => 710715609
[logCount] => 3
[logins] => 2020-02-28 00:01:01/2020-02-25 00:01:01/2020-02-21 00:01:01
[transc] => 2
[actVas] => sports/Mtunes/Astro/D50
[vasCount] => sports-1/Mtunes-1/Astro-1/D50-2
)
[1] => Array
(
[msisdn] => 119774100
[logCount] => 2
[logins] => 2020-02-22 00:01:01/2020-02-22 00:01:01
[transc] => 1
[actVas] => sports/D50
[vasCount] => sports-1/D50-1
)
)

PHP Array Readable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have an Array format.
Array
(
[0] => Array
(
[order_id] => 1
)
[1] => Array
(
[order_id] => 11
)
[2] => Array
(
[order_id] => 12
)
)
It should to be changed the array format to be like this format:
[1,11,12];
Please help. Thank you in advanced.
For (PHP 5 >= 5.5.0, PHP 7) you can use array_column and lower than that you can use array_map function (for PHP < 5.3) you need to define saperate function for array_map instead of anonymous function.
$array = array
(
'0' => array
(
'order_id' => 1
),
'1' => array
(
'order_id' => 11
),
'2' => array
(
'order_id' => 12
)
);
$new_array = array_column($array, 'order_id');
print_r($new_array);
$new_array = array_map(function($element) {
return $element['order_id'];
}, $array);
print_r($new_array);
output
Array
(
[0] => 1
[1] => 11
[2] => 12
)
How about this?
$arr = Array
(
0 => Array
(
'order_id' => 1
),
1 => Array
(
'order_id'=> 11
),
2 => Array
(
'order_id' => 12
),
);
$newArr = array();
foreach($arr as $x){
foreach($x as $y){
$newArr[] = $y;
}
}
You can try this:
$ar = array(array('order_id' => 1), array('order_id' => 11), array('order_id' => 12));
$temp = array();
foreach ($ar as $val) {
$temp[] = $val['order_id'];
}
print_r($temp);
Basically, you are looking to flatten the array. This SO answer has some tips on how to do that. Also, there's a golf version in this Github gist.
For the specific case in your question, this should do
function specificFlatten($arr) {
if (!is_array($arr)) return false;
$result = [];
foreach ($arr as $arr_1) {
result[] = $arr_1['order_id'];
}
return $result;
}

i want to remove index from array with minimum length 3 characters [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
this is my array and i want to remove index from array which is minimum length is 3 characters
Array
(
[0] => #FCBayern
[1] => won
[2] => a
[3] => Championsleague
[4] => match
[5] => with
[6] => 2
[7] => goals
[8] => difference
)
i want to out put like this
Array
(
[0] => #FCBayern
[1] => Championsleague
[2] => match
[3] => with
[4] => goals
[5] => difference
)
foreach($array as $key => $val) {
if(strlen($val) <= 3)
unset($array[$key]);
}
The below will do what you need.
Here is a brief explanation.
Iterate over the loop with a foreach loop.
Use conditional statement to check if the length is less than or equal to 3.
If the conditional evaluates to true, remove the index from the array.
Try this, It will produce hte same out put as you are looking
$array=Array ( 0 => '#FCBayern', 1 => 'won',
2 => 'a', 3 => 'Championsleague', 4 => 'match',
5 => 'with', 6 => '2', 7 => 'goals', 8 => 'difference' );
foreach($array as $newarray)
{
if(strlen($newarray)>2)
{
$myarray[]=$newarray;
}
}
echo '<pre>';
print_r($myarray);
'<pre>';
There is a function designed for this type of task which is slightly faster than using a scripted loop as it is handled at the assembly level.
The function is called : array_filer()
As solution for your problem using this method is below. You can name the function whatever you want and it simply returns a boolean -- true to keep the item, or false to remove it.
$array1 = Array (
'#FCBayern',
'won',
'a',
'Championsleague',
'match',
'with',
'2',
'goals',
'difference'
);
$array1 = array_filter($array1, "myfilter");
var_export ( $array1 );
function myfilter($item) {
return (strlen($item) > 3 ? true : false);
}
<?php
$sample = Array(0 => '#FCBayern',1 => 'won',2 => 'a',3 => 'Championsleague',4 => 'match',5 => 'with',6 => 2,7 => 'goals',8 => 'difference');
array_walk_recursive($sample,'makeProper');
$sample1 = array_filter($sample);
echo "<pre>";
print_r($sample1);
function makeProper(&$item, $key){
if(strlen($item) > 3){
$item = $item;
} else {
$item = '';
}
}
?>

How to merge array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
i have two arrays like
Array1
(
[0] => 1
[1] => 2
[2] => 3
)
Array2
(
[0] => a
[1] => b
[2] => c
)
i want make
Array3 Like
(
[0] => ([0]=>1 [1]=>a)
[1] => ([0]=>2 [1]=>b)
[2] => ([0]=>3 [1]=>c)
)
This is definitely not the prettiest way to do it, but since you haven't supplied any attempted code, I doubt anybody wants to bother with this question so here:
NOTE: As stated in the comments, you're going to have to make sure the two arrays are the same length and sort that out yourself.
$one = array(
'1',
'2',
'3'
);
$two = array(
'a',
'b',
'c'
);
$derp = array();
foreach($one as $key => $val) {
$derp[] = array(
$val,
$two[$key]
);
}
?>
Which returns
Array
(
[0] => Array
(
[0] => 1
[1] => a
)
[1] => Array
(
[0] => 2
[1] => b
)
[2] => Array
(
[0] => 3
[1] => c
)
)
Using SPL's MultipleIterator
$arr1 = [1, 2, 3];
$arr2 = ['a', 'b', 'c'];
$mi = new MultipleIterator();
$mi->attachIterator(new ArrayIterator($arr1));
$mi->attachIterator(new ArrayIterator($arr2));
$result = array();
foreach($mi as $details) {
$result[] = $details;
}
var_dump($result);

PHP array map to anther array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to map
Array
(
[0] => Array
(
[cid] => 1
[value] => red
)
[1] => Array
(
[cid] => 2
[value] => green
)
[2] => Array
(
[cid] => 3
[value] => pink
)
[3] => Array
(
[cid] => 4
[value] => yellow
)
)
To:
Array
(
[0] => 2
[1] => 3
[2] => 1
)
I need to map the second arrays value [0] => 2 to the cid in the first array. In other words I need 2 to map to value green.
Any help? THANK YOU.
I would first change the first array to something that is easier to check.
<?php
$temp = array();
foreach ($array1 as $val) {
$temp[$val['cid']] = $val['value'];
}
?>
Now you have an array:
$temp[1] = "red";
$temp[2] = "green";
$temp[3] = "pink";
$temp[4] = "yellow";
Then you can easily use that in the second array
<?php
$new= array();
foreach ($array2 as $key=>$val) {
$new[$key] = $temp[$val];
}
?>
Codepad example
PHP >= 5.5.0
$colors = array_column($first, 'value', 'cid');
foreach($second as $value) {
if(isset($colors[$value])) {
echo $colors[$value];
}
}
Where $first is your first array and $second obviously the second.

Categories