I want to merge some multidimension arrays and calculate the values of their items. For example :
Array
(
[0] => Array
(
[0] => Array
(
[nr_colete] => 6
)
)
[1] => Array
(
[0] => Array
(
[nr_colete] => 22
)
)
)
I want to get a solution to combine them and get a result such as
Array
(
[0] => Array
(
[nr_colete] => 6 + 22
)
)
Is there a native php function to help me get this result ? I try to found one.
I can't think of one single php native function to do this, but you can do it very simply using a foreach loop.
$sum = 0;
foreach($array AS $k => $value) {
$sum += $value[0]['nr_colete'];
}
Here is the code in action
No native function which will do that directly.But you can use array_column() and array_sum() two native function to get your desired result.
Check below code:-
$final_array[0]['nr_colete'] = array_sum(array_column(array_column($array,0),'nr_colete'));
print_r($final_array);
Output:- https://eval.in/873338
Reference:-
array_column()
array_sum()
Related
How do you count items in an array that's part of an array? I'm using Advance Custom Fields plugin in WordPress and my array print_r(get_field('irl_today_entry')) output looks like this:
Array
(
[0] => Array
(
[acf_fc_layout] => irl_today_website_entry
[irl_today_website] => Array
(
[0] => Array
( data removed)
[1] => Array
( data removed )
)
)
[1] => Array
(
[acf_fc_layout] => irl_today_social_entry
[irl_today_social] => Array
(
[0] => Array
( data remove )
[1] => Array
( data remove)
)
)
)
How do you only count items in [irl_today_social]? I've tried a lot of options that do not work.
If you have multiple entries with irl_today_social you might also use array_map and array_column
$res = array_map(function($x) {
return count($x);
}, array_column($arrays, "irl_today_social"));
print_r($res);
Output
Array
(
[0] => 2
)
See a php demo
You can use array_reduce,
array_reduce(get_field('irl_today_entry'),function($a,$b){
return count($a["irl_today_website"]) + count($b["irl_today_website"]);
});
Loop through your array, get the count of your desired index, and add that to a counter.
$count = 0;
foreach(get_field('irl_today_entry') as $entry){
$count = $count + count(entry['irl_today_social']);
}
Simply you can use "Count" Function
Here is the Solution:
Step 1: First get your array items of ['irl_today_entry'].
Step 2 Then Count your ['irl_today_social'] items.
Example:
foreach(get_field('irl_today_entry') as $entry){
$socialData = count(entry['irl_today_social']);
}
Thanks
according to my understaing just simply try this
echo count($your_array[1]['irl_today_social']);
I am trying to count elements in an array, but it doens't work as intended:
I have a while loop, which loops through my user table:
while($refsData=$refs->fetch()){
$new_array = array($refsData['id']);
print_r($new_array);
$outcome = $rentedrefs->_paying($new_array);
}
The print_r($new_array); gives me:
Array
(
[0] => 90427
)
Array
(
[0] => 90428
)
Array
(
[0] => 90429
)
Array
(
[0] => 90430
)
Array
(
[0] => 90431
)
Array
(
[0] => 90432
)
Array
(
[0] => 90433
)
Array
(
[0] => 90434
)
Array
(
[0] => 90435
)
Array
(
[0] => 90436
)
Inside the _paying function, I count the number of values from the array:
function _paying($referrals_array){
echo count($referrals_array);
}
The problem is, that the above count($referrals_array); just gives me: 1, when it should be 10
What am I doing wrong?
You create a new array at each step of the loop. Instead it should be written like this:
$new_array = array();
while($refsData=$refs->fetch()){
$new_array[] = $refsData['id'];
// print_r($new_array);
}
$outcome = $rentedrefs->_paying($new_array);
Note that I moved the _paying call outside the loop, as it seems to be the aggregating function. If not, you'd most probably make it process $refsData['id'] instead - not the whole array.
As a sidenote, I'd strongly recommend using fetchAll() method (instead of fetch when you need to fill a collection with results of a query. It'll be trivial to count the number of the resulting array.
It works properly, in each circulation of loop you have one array, so in first you have:
Array
(
[0] => 90427
)
in 2nd:
Array
(
[0] => 90428
)
and so on.
It should work properly:
var $count = 0;
while($refsData=$refs->fetch()){
$new_array = array($refsData['id']);
$count += count($new_array);
$outcome = $rentedrefs->_paying($new_array);
}
You are creating $new_array as a new array with the single element $refsData['id']. The count of 1 is therefore correct.
To get the number of results, either use a COUNT(*) select to ask your sql server, or add a counter to your loop, like this:
$entries = 0;
while($refsData=$refs->fetch()){
$new_array = array($refsData['id']);
print_r($new_array);
$entries++;
$outcome = $rentedrefs->_paying($new_array);
}
echo $entries;
You are not adding elements to an array, but creating a new array each iteration. To add elements, just do:
$new_array[] = $refsData['id'];
My Array shown as follows:
Array
(
[0] => Array
(
[amount_id] => 1
[enquiry_id] => 1
[project_id] => 1
)
[1] => Array
(
[amount_id] => 4
[enquiry_id] => 4
[project_id] => 4
)
[2] => Array
(
[amount_id] => 5
[enquiry_id] => 5
[project_id] => 5
)
)
This Array can be increase. How can i get value of each 'amount_id' from this array? What function should i use? Can for each function will work?
Just try with:
$input = array( /* your data */ );
$output = array();
foreach ($input as $data) {
$output[] = $data['amount_id'];
}
You can use a one-liner array_walk() to print those..
array_walk($arr,function($v){ echo $v['amount_id']."<br>";});
Working Demo
Do like this in array_map or Use array_column for The version PHP 5.5 or greater
$outputarr= array_map(function($item){ return $item['amount_id'];},$yourarr);
print_r($outputarr);
Try the following, this is accessing the specific array - easier to debug later on and better speed:
$num=count($your_array);
for($i="0"; $i<$num; $i++)
{
echo $your_array[$i]['amount_id'];
}
you could loop until $i < count($your_array) but it means that the count will run in every loop, for high performance sites I wouldn't do it.
you could access a specific element in a 2D array by $your_array[$the_index]['amount_id'] or other index.
I've got an multi-dimensional array at the moment and want to remove the second-level of arrays and have the value of that second level as the new index value on the parent array. My current array is:
Array ( [0] => Array ( [connectee] => 1 ) [1] => Array ( [connectee] => 6 ) )
And want from that:
Array ( [0] => 1, [1] => 6 )
I was poking around the usort function but couldn't get it to work (where $current_connections is my array as above:
function cmp($a, $b) {
return strcmp($a["connectee"], $b["connectee"]);
}
$current_connections = usort($current_connections, "cmp");
The key doesn't need to be maintained (should be destroyed in the process).
foreach ($array as &$value) {
$value = $value['connectee'];
}
Note: Please note that the question statement is very confusing and contradicting, but this answer is based upon your statement for expected output
Array ( [0] => 1, [1] => 6 )
You could do
<?php
$values=array();
$values[0]=array("connectee"=>1);
$values[1]=array("connectee"=>6);
foreach($values as $index=>$value)
{
$values[$index]=$value["connectee"];
}
print_r($values);
?>
I have two arrays, one is generated by using explode() on a comma separated string and the other is generated from result_array() in Codeigniter.
The results when doing print_r are:
From explode():
Array
(
[0] => keyword
[1] => test
)
From database:
Array
(
[0] => Array
(
[name] => keyword
)
[1] => Array
(
[name] => test
)
)
I need them to match up so I can use array_diff(), what's the best way to get them to match? Is there something other than result_array() in CI to get a compatible array?
You could create a new array like this:
foreach($fromDatabase as $x)
{
$arr[] = $x['name'];
}
Now, you will have two one dim arrays and you can run array_dif.
$new_array = array();
foreach ($array1 as $line) {
$new_array[] = array('name' => $line);
}
print_r($new_array);
That should work for you.