This question already has answers here:
Group array data on one column and sum data from another column
(5 answers)
Closed 5 months ago.
I have a php array like this:
[
['url_id' => 2191238, 'time_spent' => 41],
['url_id' => 2191606, 'time_spent' => 215],
['url_id' => 2191606, 'time_spent' => 25]
]
How to get the SUM of time_spent based on group by url_id (using array_count_values?)?
Let's pretend that $array contains our data. We will go through the array and continually add the time_spent to another array keyed by url_id.
$ts_by_url = array();
foreach($array as $data) {
if(!array_key_exists($data['url_id'], $ts_by_url))
$ts_by_url[ $data['url_id'] ] = 0;
$ts_by_url[ $data['url_id'] ] += $data['time_spent'];
}
$ts_by_url should now contain:
2191238 => 41
2191606 => 240 // == 215 + 25
The OP posted an interesting problem, but the suggestion of using array_count_values() was not applicable; the function does not sum array values but counts their frequency. The answer of #marcocassisa works but is insufficient because of the unfortunate emission of E_NOTICES, albeit unrelated to any math issue. (An undefined variable is unset and thus of NULL value, but in a math context it will temporarily be coerced into a value of zero.) Modern PHP discourages the use of undefined variables by intentionally raising notices when code utilizes them, with one exception starting with PHP 5.1: a statement consisting of only an undefined variable; see here.
To rectify the aforementioned solution, one might use two foreach loops, one to set the index keys and initialize the array elements while the other would perform the summing of values, as follows:
<?php
// set keys and initialize
foreach($arr as $data) {
$its_by_url[ $data["url_id"] ] = 0;
}
// now can sum values by url id number:
foreach($arr as $data) {
$its_by_url[ $data["url_id"] ] += $data["time_spent"];
}
see demo.
While satisfactory, this solution lacks elegance. Attempting to combine the two foreach loops into one, will negatively impact the second element, as it gets defined and then needlessly redefined. One could code per the answer of #Charles and create the url id numbered index if it doesn't already exist. Or, you could test to see if it is set, as follows:
<?php
foreach($arr as $data) {
if ( !isset( $its_by_url[ $data["url_id"] ] ) ) {
$its_by_url[ $data["url_id"] ] = 0;
}
$its_by_url[ $data["url_id"] ] += $data["time_spent"];
}
see demo
Note: if an array element were defined as null, then isset() would return false while array_key_exists() would return true. Per, Ilia Alshanetsky
... if your application does not need to distinguish between an
array key that does not exist and one whose value happens to be
NULL you should use isset() because it happens to be a little
faster.
Why not a simpler
$ts_by_url = array();
foreach($array as $data) {
$ts_by_url[ $data['url_id'] ] += $data['time_spent'];
}
Related
I am getting duplicate data in an array and that data is storing in my DB. I am using array_unique to refine the duplicate data but it is not working. Please tell me is there any other way to make data unique and store in DB this way.
if (preg_match($keywords, $links[$i]->href)) {
if (filter_var($links[$i]->href, FILTER_VALIDATE_URL) !== false) {
array_push($mainNews, $links[$i]->href);
}
}
return (array_unique($mainNews));
Error I am getting:
Undefined array key 1 at C:\xampp\htdocs\pacra-crawlers\modules\crawlers\services\MainNewsRepository.php:46
for ($i = 0; $i < count($mainNewsLinks); $i++) {
$mainNews = new MainNews();
$mainNews->newspaper_id = $this->newspaperId;
$mainNews->sector_id = $sectorId;
$mainNews->url = $mainNewsLinks[$i];
$mainNews->save();
}
return ['status' => true];
}
C:\xampp\htdocs\pacra-crawlers\modules\crawlers\services\MainNewsRepository.php:46
Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Undefined array key 1", "C:\xampp\htdocs\pacra-crawlers\modules\crawlers\services\MainNewsRepo
sitory.php")
array_unique is working however although it is removing duplicates it is maintaining the same keys i.e.
If you had the following items in an array with
position/key value
0 a
1 a
2 b
array_unique would return
position/key value
0 a
2 b
which is why you are getting the Undefined array key when looping through the array based on the incrementing index $i.
Based on your sample you could use a foreach loop since you are only interested in the value eg
foreach($mainNewsLinks as $mainNewsLink) {
$mainNews = new MainNews();
$mainNews->newspaper_id = $this->newspaperId;
$mainNews->sector_id = $sectorId;
$mainNews->url = $mainNewsLink;
$mainNews->save();
}
If you would like to continue indexing or iterating through each element based on an index, you could use array_values in your return eg
return array_values(array_unique($mainNews));
from your function to reset the array keys to incrementing indexes
I receive the following error message:
Undefined offset: 1
It points to this block of code:
$nbrProgrammingsRemoved = 0;
for($i = 0; $i<count($this->products); $i++){
if((($this->products[$i])->id)==$id){
array_splice($this->products, $i, 1);
for($j = 0; $j<count($this->programming); $j++){
/*ERROR LINE*/ if((($this->programming[$j]->out_prod_id)==$id) || (($this->programming[$j]->in_prod_id)==$id)){
$nbrProgrammingsRemoved++;
array_splice($this->programming, $j, 1);
}
}
return true;
}
}
return false;
Specifically, the error points to the innermost if-statement. (The one with "||" in it).
Now, important to note is that this error does not always occur. It only ever happens after the following code has been run:
foreach ($this->programming as $key => &$prog) {
if($prog->in_prod_id == $in_prod_id){
if($prog->in_index == $in_index){
unset($this->programming[$key]);
}
}
}
The purpose of this code is to iterate through my objects in my array and remove those associated with a certain ID. This does appear to work since the output on my website is as expected. It's only when I, after doing this, attempt to execute the first code-block that my error occurs.
I've tried troubleshooting this for a while now, but without success. Any ideas? Any more information that you need me to post?
Edit: For further clarification, if needed, the 1st code block iterates through an array to remove a single element of a specified ID. The 2nd code block iterates through another array and removes several elements.
As far as I understand, you have an array with indexes comming in a sequence:
$programming = array(
0 => ...,
1 => ...,
2 => ...,
);
At some point you unset one element, so you array looks like this:
$programming = array(
0 => ...,
2 => ...,
);
And then you're using a for loop to iterate over all numbers from 0 up to N-1 (0, 1, 2, 3, 4 ... to be precise) presuming that all indexes are filled.
I think the best solution is to use a foreach loop in this case as it will care about indexes automatically and bypass deleted items.
Question has been updated to clarify
For simple arrays, I find it convenient to use $arr[$key]++ to either populate a new element or increment an existing element. For example, counting the number of fruits, $arr['apple']++ will create the array element $arr('apple'=>1) the first time "apple" is encountered. Subsequent iterations will merely increment the value for "apple". There is no need to add code to check to see if the key "apple" already exists.
I am populating an array of arrays, and want to achieve a similar "one-liner" as in the example above in an element of the nested array.
$stats is the array. Each element in $stats is another array with 2 keys ("name" and "count")
I want to be able to push an array into $stats - if the key already exists, merely increment the "count" value. If it doesn't exist, create a new element array and set the count to 1. And doing this in one line, just like the example above for a simple array.
In code, this would look something like (but does not work):
$stats[$key] = array('name'=>$name,'count'=>++);
or
$stats[$key] = array('name'=>$name,++);
Looking for ideas on how to achieve this without the need to check if the element already exists.
Background:
I am cycling through an array of objects, looking at the "data" element in each one. Here is a snip from the array:
[1] => stdClass Object
(
[to] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[name] => foobar
[id] => 1234
)
)
)
I would like to count the occurrences of "id" and correlate it to "name". ("id" and "name" are unique combinations - ex. name="foobar" will always have an id=1234)
i.e.
id name count
1234 foobar 55
6789 raboof 99
I'm using an array of arrays at the moment, $stats, to capture the information (I am def. open to other implementations. I looked into array_unique but my original data is deep inside arrays & objects).
The first time I encounter "id" (ex. 1234), I'll create a new array in $stats, and set the count to 1. For subsequent hits (ex: id=1234), I just want to increment count.
For one dimensional arrays, $arr[$obj->id]++ works fine, but I can't figure out how to push/increment for array of arrays. How can I push/increment in one line for multi-dimensional arrays?
Thanks in advance.
$stats = array();
foreach ($dataArray as $element) {
$obj = $element->to->data[0];
// this next line does not meet my needs, it's just to demonstrate the structure of the array
$stats[$obj->id] = array('name'=>$obj->name,'count'=>1);
// this next line obviously does not work, it's what I need to get working
$stats[$obj->id] = array('name'=>$obj->name,'count'=>++);
}
Try checking to see if your array has that value populated, if it's populated then build on that value, otherwise set a default value.
$stats = array();
foreach ($dataArray as $element) {
$obj = $element->to->data[0];
if (!isset($stats[$obj->id])) { // conditionally create array
$stats[$obj->id] = array('name'=>$obj->name,'count'=> 0);
}
$stats[$obj->id]['count']++; // increment count
}
$obj = $element->to->data is again an array. If I understand your question correctly, you would want to loop through $element->to->data as well. So your code now becomes:
$stats = array();
foreach ($dataArray as $element) {
$toArray = $element->to->data[0];
foreach($toArray as $toElement) {
// check if the key was already created or not
if(isset($stats[$toElement->id])) {
$stats[$toElement->id]['count']++;
}
else {
$stats[$toElement->id] = array('name'=>$toArray->name,'count'=>1);
}
}
}
Update:
Considering performance benchmarks, isset() is lot more faster than array_key_exists (but it returns false even if the value is null! In that case consider using isset() || array_key exists() together.
Reference: http://php.net/manual/en/function.array-key-exists.php#107786
This question already has answers here:
Most efficient way to search for object in an array by a specific property's value
(11 answers)
Closed 4 months ago.
I Have an array holding multiple objects. Is it posible to check if a value exists in any one of the objects e.g. id->27 without looping? In a similar fashion to PHP's in_array() function. Thanks.
> array(10)[0]=>Object #673
["id"]=>25
["name"]=>spiderman
[1]=>Object #674
["id"]=>26
["name"]=>superman
[2]=>Object #675
["id"]=>27
["name"]=>superman
.......
.......
.........
No. If you often need quick direct lookup of values, you need to use array keys for them, which are lightning fast to lookup. For example:
// prepare once
$indexed = array();
foreach ($array as $object) {
$indexed[$object->id] = $object;
}
// lookup often
if (isset($indexed[42])) {
// object with id 42 exists...
}
If you need to lookup objects by different keys, so you can't really index them by one specific key, you need to look into different search strategies like binary searches.
$results = array_filter($array, function($item){
return ($item->id === 27);
});
if ($results)
{
.. You have matches
}
You will need to do looping one way or another - but you don't have to manually implement the loop yourself. Have a look at array_filter function. All you need to do is to provide a function that checks the objects, something like this:
function checkID($var)
{
return $var->id == 27;
}
if(count(array_filter($input_array, "checkID"))) {
// you have at least one matching element
}
Or you can even do this in one line:
if(count(array_filter($input_array, function($var) { return $var->id == 27; }))) {
// you have at least one matching element
}
You may want to combine two functions to get the desired results.
array_search($needle, array_column($array, 'key_field');
Created a small code to demonstrate its use.
<?php
$superheroes = [
[
"id" => 1,
"name" => "spiderman"
],
[
"id" => 2,
"name" => "superman"
],
[
"id" => 3,
"name" => "batman"
],
[
"id" => 4,
"name" => "robin"
],
];
$needle = 'spiderman';
$index = array_search($needle, array_column($superheroes, "name"));
echo "Is $needle a superhero?<br/>";
//Comparing it like this is important because if the element is found at index 0,
//array_search will return 0 which means false. Hence compare it with !== operator
if ( false !== $index ) {
echo "yes";
} else {
echo "no";
}
?>
You can do:
foreach ($array as $value)
{
if ($value == "what you are looking for")
break;
}
array_search — Searches the array for a given value and returns the
corresponding key if successful
$key = array_search('your search', $array);
This is the set of result from my database
print_r($plan);
Array
(
[0] => Array
(
[id] => 2
[subscr_unit] => D
[subscr_period] =>
[subscr_fee] =>
)
[1] => Array
(
[id] => 3
[subscr_unit] => M,Y
[subscr_period] => 1,1
[subscr_fee] => 90,1000
)
[2] => Array
(
[id] => 32
[subscr_unit] => M,Y
[subscr_period] => 1,1
[subscr_fee] => 150,1500
)
)
How can I change the $plan[0] to $plan[value_of_id]
Thank You.
This won't do it in-place, but:
$new_plan = array();
foreach ($plan as $item)
{
$new_plan[$item['id']] = $item;
}
This may be a bit late but I've been looking for a solution to the same problem. But since all of the other answers involve loops and are too complicated imho, I've been trying some stuff myself.
The outcome
$items = array_combine(array_column($items, 'id'), $items);
It's as simple as that.
You could also use array_reduce which is generally used for, well, reducing an array. That said it can be used to achieve an array format like you want by simple returning the same items as in the input array but with the required keys.
// Note: Uses anonymous function syntax only available as of PHP 5.3.0
// Could use create_function() or callback to a named function
$plan = array_reduce($plan, function($reduced, $current) {
$reduced[$current['id']] = $current;
return $reduced;
});
Note however, if the paragraph above did not make it clear, this approach is overkill for your individual requirements as outlined in the question. It might prove useful however to readers looking to do a little more with the array than simply changing the keys.
Seeing the code you used to assemble $plan would be helpful, but I'm going assume it was something like this
while ($line = $RES->fetch_assoc()) {
$plan[] = $line;
}
You can simply assign an explicit value while pulling the data from your database, like this:
while ($line = $RES->fetch_assoc()) {
$plan[$line['id']] = $line;
}
This is assuming $RES is the result set from your database query.
In my opinion, there is no simpler or more expressive technique than array_column() with a null second parameter. The null parameter informs the function to retain all elements in each subarray, the new 1st level keys are derived from the column nominated in the third parameter of array_column().
Code: (Demo)
$plan = array_column($plan, null, 'id');
Note: this technique is also commonly used to ensure that all subarrays contain a unique value within the parent array. This occurs because arrays may not contain duplicate keys on the same level. Consequently, if a duplicate value occurs while using array_column(), then previous subarrays will be overwritten by each subsequent occurrence of the same value to be used as the new key.
Demonstration of "data loss" due to new key collision.
$plans = array();
foreach($plan as $item)
{
$plans[$item['id']] = $item;
}
$plans contains the associative array.
This is just a simple solution.
$newplan = array();
foreach($plan as $value) {
$id = $value["id"];
unset($value["id"]);
$newplan[$id] = $value;
}