I don't really know how to explain what I would like to do, so here is an example. I have an 2D array, like this one :
Array
(
[0] => Array
(
[1] => value 1
[2] => value 2
)
[1] => Array
(
[1] => value 1
[2] => value 2
)
[2] => Array
(
[1] => value 1
[2] => value 2
)
)
And I would like to have this :
Array
(
[0] => Array
(
[1] => value 1
[2] => value 2
[3] => value 3
)
[1] => Array
(
[1] => value 1
[2] => value 2
[3] => value 3
)
[2] => Array
(
[1] => value 1
[2] => value 2
[3] => value 3
)
)
Can someone help me ? Thanks very much.
Just loop it and make sure to reference & the $val to update the original array. Then just append the new item:
foreach($array as &$val) {
$val[] = 'value 3';
}
The other way:
foreach($array as $key => $val) {
$array[$key][] = 'value 3';
}
Related
I would like to sum up several arrays. I have a script that dynamically creates arrays without name. Below is an example. In my script i have a 90+ arrays. I want to sum up all it. All keys in that arrays be user id, so i only want sum values of keys. How to do it? Regards
Array
(
[1] => 1
[2] => 1
[3] => 1
)
Array
(
[1] => 1
[2] => 1
)
Array
(
[1] => 1
)
I want to get only one array result like:
Array
(
[1] => 3
[2] => 2
[3] => 1
)
Add all your arrays into one array.
$all_arrays[] =Array
(
[1] => 1 ,
[2] => 1,
[3] => 1
);
$all_arrays[] = Array
(
[1] => 1,
[2] => 1
) ;
$all_arrays[] = Array
(
[1] => 1
);
$results = [];
foreach($all_arrays as $arr){
foreach($arr as $user_id=>$value){
if(in_array($user_id,$results)){
$results[$user_id] = $results[$user_id] + $value;
}else{
$results[$user_id] = $value;
}
}
}
You would need to specify all arrays in the array_merge:
$result = array_count_values(array_keys(array_merge($array1, $array2, $array3)));
If you can get the arrays dynamically added to another $main_array like:
Array
(
[0] => Array
(
[1] => 1
[2] => 1
[3] => 1
)
[1] => Array
(
[1] => 1
[2] => 1
)
[2] => Array
(
[1] => 1
)
)
Then it would be much easier:
$result = array_count_values(array_keys(array_merge(...$main_array)));
This is my array
Array
(
[2] => Array
(
[0] => Array
(
[id] => 2
[res_id] => 1
[grand_total] => 303.42
[time] => 2016-07-28 11:04:38 AM
[status] => 0
)
[1] => Array
(
[id] => 2
[res_id] => 1
[grand_total] => 303.42
[time] => 2016-07-28 11:04:38 AM
[status] => 0
)
)
[1] => Array
(
[0] => Array
(
[id] => 1
[res_id] => 1
[grand_total] => 303.42
[time] => 2016-07-28 11:04:17 AM
[status] => 0
)
)
)
From this I need sub array count i.e., the array having two indexes such as 2 & 1 from this 2 & 1 there are some nested arrays found such as 0 & 1 for each
Here,I need array count as follows
Array
(
[2] => Array
(
[count] = 2
)
[1] => Array
(
[count] = 1
)
)
How should I get this..
Someone help me out of this...
Thank you..
It is very easy foreach your array and use count or sizeof function.
$desiredArray = array();
foreach ($myarray as $key => $value) {
$desiredArray [$key] ['count'] = sizeof ($value);
}
print_r ($desiredArray);
The output will be as your desired output
Array
(
[2] => Array
(
[count] = 2
)
[1] => Array
(
[count] = 1
)
)
It's simple, and is better to create new array where you can save count of elements of main array items:
$counts = array();
foreach ($array as $k => $values) {
$counts[$k] = count($values);
}
print($counts); // gives desired result
Also you don't need to have extra array for the $counts array, what you get is:
array (
2 => 2,
1 => 1
)
I have toe different multidimensional array following :
Array
(
[1] => Array
(
[0] => 1
[1] => 2
)
[2] => Array
(
[0] => 1
)
)
Array
(
[1] => Array
(
[0] => 1
[1] => 2
)
[2] => Array
(
[0] => 1
[1] => 2
)
[3] => Array
(
[0] => 1
)
)
I want to check small multidimensional array exists in bigger array. Any suggestion please.
I am using
$diff = Hash::diff(samllarray, $bigger array);
of cakephp and its result is
Array
(
[2] => Array
(
[0] => 1
)
[3] => Array
(
[0] => 1
)
)
but in result I want only 3rd key but its also given me 2rd key see above
You can use is_array() to see if a variable is an array.
$arrs = array(
0 => "big array",
1 => "big array",
3 => array(
0 => "nested array",
1 => "nested array"
)
);
foreach ($arrs as $key=>$value) {
if (is_array($value)) {
echo "we've got an array at index {$key}";
}
}
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]);
}
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;
}
}