Changing values of all similar sub arrays in PHP - php

I've got the following 2D array, stored inside the array variable $my_array
Array
(
[0] => Array
(
[0] => 3
[1] => 6
[2] => 3
)
[1] => Array
(
[0] => 3
[1] => 6
[2] => 3
)
[2] => Array
(
[0] => 3
[1] => 6
[2] => 3
)
)
I wanted to decrement all the [1] sub array values by 3. Tried the following code, with no success.
$my_array[$i]['1']=($my_array[$i]['1'])-3;
print_r($my_array);
Ideas?

foreach ($my_array as &$val) {
$val[1] -= 3
}

Something like this is what you're after.
foreach($my_array as $k=>$v){
if (isset($my_array[$k][1]) && is_numeric($my_array[$k][1])){
$my_array[$k][1] -= 3;
}
}

Related

how to count the array values with key

I have array like this i need to count by array values
Array ( [Cop] => Array ( [0] => Dozen [1] => Dozen [2] => Akls [3] => Akls ) [MSN] => Array ( [0] => Dozen ) [NeK] => Array ( [0] => Suhan [1] => Ebao ) [NetSE] => Array ( [0] => SuZhan [1] => Guhang ) )
For example
Array ( [Cop] => Array ( [0] => Dozen [1] => Dozen [2] => Akls [3] => Akls ))
In the Cop key i have two different values for cop so i need cop should be 2
Cop - 2
MSn - 1
NeK - 2
NetSE - 2
I need the count like above how can i do this ?
Try simply using array_map,count,& array_unique like as
array_map(function($v) {
return count(array_unique($v));
}, $arr);
Use array_unique() and then count.
count(array_unique($array['Cop']));// output 2
If you want to print for every key do following:
$array = array('Cop'=>array('Dozen','Dozen','Akls','Akls'), 'MSN'=> array('Dozen'), 'NeK'=> array('Suhan','Ebao'));
foreach($array as $key => &$value) {
$value = count(array_unique($array[$key]));
}
print_r($array);
Output:
Cop = 2
MSN = 1
NeK = 2
You should use array_count_values() for that, here's an example:
$data = array('cop' => array(0 => 'test', 1 => 'test', 2 => 'test2'));
foreach($data as $item){
$result = array_count_values($item);
print_r($result);
}
Outputs:
Array
(
[test] => 2
[test2] => 1
)

running for loop on array without incremental index

I have this arbitrary multi dimensional array.
Array (
[0] => Array
(
[0] => 0
[1] => 1
[2] => 2
)
[5] => Array
(
[0] => 0
[1] => 1
[2] => 2
)
[10] => Array
(
[0] => 0
[1] => 1
[2] => 2
)
[15] => Array
(
[0] => 0
[1] => 1
[2] => 2
)
[1] => Array
(
[0] => 2
[1] => 1
[2] => 2
)
[2] => Array
(
[0] => 2
[1] => 1
[2] => 2
)
)
I wanna run a for loop to extract the data of each subarray.
But I cannot do a simple for loop because the index (0,5,10,15,1) is arbitrary.
Is there a way to run a for loop then skip the sub array if it is empty?
Thanks!
This will take $array and loop though it, echoing the keys.
You have an array in an array, you can place a foreach in a foreach:
// First we take the main array ($array) and loop though its values
foreach( $array as $main_key =>$sub_array){
echo $main_key.": <br />\n"; // echo the key, some extra html to format
// the values of the mainarray are arrays themselves, just loop again:
foreach($subarray as $sub_key =>$subvalue){
echo '- '.$subvalue."<br />\n";
}
}
There's a bit of a trap here if you foreach in a foreach:
foreach($array as $key =>$value){
foreach($value as $key=>$value){ /* ... */; }
}
This will create very weird results. The inner foreach uses the same parameter-names and will mess everything up.

PHP: put array entries in new order

I try to take the following array:
Array
(
[0] => Array
(
[1] => 12
[2] => 21
[3] => 33
)
[1] => Array
(
[1] => 5
[2] => 5
[3] => 4
)
[2] => Array
(
[1] => 1
[2] => 10
[3] => 11
)
[3] => Array
(
[3] => 2
)
)
and create a new array with it, taking the first entries of the source array. So the first array from this one would look like this:
Array
(
[0] => 12
[1] => 5
[2] => 1
[3] => 2
)
And the second one obviously like this:
Array
(
[0] => 21
[1] => 5
[2] => 10
)
I tried around with 2 for loops, it somewhat works with
for ($i = 1; $i < count($month_array[$i]) + 1; $i++)
{
unset($temp_array);
for ($i2 = 0; $i2 < count($month_array); $i2++)
{
if (isset($month_array[$i2][$i]))
{
$temp_array[] = $month_array[$i2][$i];
}
}
}
But it leaves out some of the elements and if the source array is not complete, meaning only the 3rd array has a value for key 3, it also fails.
Any help ? Many thanks !
Try it like this:
$org_array = array(array(1 => 12,2 => 21,3 => 33),array(1 => 5,2 => 5,3 => 4),array(1 => 1,2 => 10,3 => 11),array(3 => 2),); // your original array
$new_array = array(); // the output array
foreach($org_array as $sub_org_value) { // for each sub array of your original array do this
foreach($sub_org_value as $key => $value) { // for each entry in a sub array do this
$new_array[$key][] = $value; // add the entry into the new_array in the right sub array
}
}
print_r($new_array[1]); // yields 12,5,1
print_r($new_array[2]); // yields 21,5,10
print_r($new_array[3]); // yields 33,4,11,2
Afterwards you have an array $new_array that contains three arrays. The first sub array has all 1 values, the second sub array has all 2 values and the third sub array has all 3 values.

Replacement of array to the number from two-dimensional array

I have two-dimensional array and I want replace second of two-dimensional array on random number of second array
Array
(
[1] => Array
(
[0] => 1
[1] => 3
[2] => 5
[3] => 500
[4] => 600
[5] => 700
)
[2] => Array
(
[0] => 2
[1] => 4
[2] => 6
)
)
I want get
Array (
[1] => 5 (<- random from first array)
[2] => 6 (<- random from second array)
)
I tried to do:
foreach($variables as $key => $val) {
$variables = str_replace($val, $val[array_rand($val)], $variables);
}
Why it doesnt work?
foreach($variables as $key => $val) {
$variables[$key] = $val[array_rand($val)];
}
foreach ($variables as &$var) {
$var = array_rand($variables[$var]);
}

How to process second dimension of an Array based on similar first dimension value?

I am trying to write some php code to process the second dimension's value of an array based on similar values of the first dimension values.
Following is the sample output.
[0] => Array (
[0] => 1
[1] => 0.091238491238491
)
[1] => Array (
[0] => 2
[1] => 0.2221793635487
)
[2] => Array (
[0] => 2
[1] => 0.10662717512033
)
[3] => Array (
[0] => 4
[1] => 0.44354338998346
)
[4] => Array (
[0] => 6
[1] => 0.2248243559719
)
[5] => Array (
[0] => 6
[1] => 0.31764705882353
)
[6] => Array (
[0] => 6
[1] => 0.15764625384879
)
[7] => Array (
[0] => 6
[1] => 0.19160083160083
)
[8] => Array (
[0] => 12
[1] => 0.31054875069499
)
[9] => Array (
[0] => 12
[1] => 0.10915034227918
)
[10] => Array (
[0] => 15
[1] => 0.32915461266474
)
//...........goes to 46000 elements
Now what I want to do is, if the index 0 values of each array is similar then I want to add the index 1's value.
So for example, if 0 index values for 4 arrays are same , I want to add index 1 values of all 4 arrays.
If there is a unique value on 0th index, dont add it with anything, simply store index 1's value and move on.
Thanks very much.
Ghanshyam
$added = array();
foreach ($array as $item) {
if (isset($added[$item[0]])) {
$added[$item[0]] += $item[1];
} else {
$added[$item[0]] = $item[1];
}
}
$p=0;
$temp = $final_prod_ex[0][1];
for($x=0; $x<count($final_prod)-1; $x++){
if($final_prod_ex[$x][0]==$final_prod_ex[$x+1][0]){
$temp = $temp + $final_prod_ex[$x+1][1];
}
else{
$ans[$p] = $temp." ".$final_prod_ex[$x][0];
$temp = $final_prod_ex[$x+1][1];
$p++;
}
}
Finally figured it out after a lot of thinking(I'm new to programming)...Array's name is $final_prod_ex. Comment on this if I can make it better. And sorry #deceze. I could not understand your solution. I know you were trying to give the value of one array as an index to another. But what the scenario is, that value isnt like 0,1,2,3,4.... Its like 1,3,5,6,7,10. We are missing numbers in between. Maybe I didnt understand your solution. Correct me if I am wrong.
Thanks for all the help.

Categories