It is my understanding that using isset to prevent duplicate values from being inserted into an array is the best method with regard to memory consumption, resource usage and ease of code processing. I am currently using the array_count_values, like this:
$XMLproducts = simplexml_load_file("products.xml");
foreach($XMLproducts->product as $Product) {
if (condition exists) {
$storeArray[] = (string)$Product->store; //potentially several more arrays will have values stored in them
}}
$storeUniques = array_count_values($storeArray)
foreach ($storeUniques as $stores => $amts) {
?>
<?php echo $stores; ?> <?php echo "(" . ($amts) . ")" . "<br>";
}
How would prevent duplicate values from being inserted into an array (similar to the above) using ISSET? And is there a big performance difference between the 2 if the XML file being parsed is very large (5-6MB)?
As you are using the count in your output, you cannot use array_unique() because you would loose that information.
What you could do, is build the array you need in your loop, using the string as your key and counting the values as you go:
$storeArray = array();
foreach($XMLproducts->product as $Product) {
if (condition exists) {
$store = (string)$Product->store;
if (array_key_exists($store, $storeArray))
{
$storeArray[$store]++;
}
else
{
$storeArray[$store] = 1;
}
}
}
Note that this is just to illustrate, you can probably wrap it up in one line.
This way you will not have multiple duplicate strings in your array (assuming that that is your concern) and you don't increase your memory consumption by generating a second (potentially big...) array.
I think array_unique and company are considered unfriendly because they check the database each time an entry is made. The code you're trying to write is doing essentially the same thing, so I don't see a problem with using array_unique.
Very simple, no checking required:
foreach($XMLproducts->product as $Product)
$helperArray[$product->store] = "";
associative arrays have unique keys by definition. if a key already exists, it is simply overwritten.
Now swap key and value:
$storeArray = array_keys($helperArray);
EDIT: to also count the number of occurences of each <store>, I suggest:
foreach($XMLproducts->product as $Product)
$helperArray[] = (string)$product->store;
And then:
$storeArray = array_count_values($helperArray);
Result: key = unique store, value = count.
Related
This question has been asked a thousand times, but each question I find talks about associative arrays where one can delete (unset) an item by using they key as an identifier. But how do you do this if you have a simple array, and no key-value pairs?
Input code
$bananas = array('big_banana', 'small_banana', 'ripe_banana', 'yellow_banana', 'green_banana', 'brown_banana', 'peeled_banana');
foreach ($bananas as $banana) {
// do stuff
// remove current item
}
In Perl I would work with for and indices instead, but I am not sure that's the (safest?) way to go - even though from what I hear PHP is less strict in these things.
Note that after foreach has run, I expected var_dump($bananas) to return an empty array (or null, but preferably an empty array).
1st method (delete by value comparison):
$bananas = array('big_banana', 'small_banana', 'ripe_banana', 'yellow_banana', 'green_banana', 'brown_banana', 'peeled_banana');
foreach ($bananas as $key=>$banana) {
if($banana=='big_banana')
unset($bananas[$key]);
}
2nd method (delete by key):
$bananas = array('big_banana', 'small_banana', 'ripe_banana', 'yellow_banana', 'green_banana', 'brown_banana', 'peeled_banana');
unset($bananas[0]); //removes the first value
unset($bananas[count($bananas)-1]); //removes the last value
//unset($bananas[n-1]); removes the nth value
Finally if you want to reset the keys after deletion process:
$bananas = array_map('array_values', $bananas);
If you want to empty the array completely:
unset($bananas);
$bananas= array();
it still has the indexes
foreach ($bananas as $key => $banana) {
// do stuff
unset($bananas[$key]);
}
for($i=0; $i<count($bananas); $i++)
{
//doStuff
unset($bananas[$i]);
}
This will delete every element after its use so you will eventually end up with an empty array.
If for some reason you need to reindex after deleting you can use array_values
How about a while loop with array_shift?
while (($item = array_shift($bananas)) !== null)
{
//
}
Your Note: Note that after foreach has run, I expected var_dump($bananas) to return an empty array (or null, but preferably
an empty array).
Simply use unset.
foreach ($bananas as $banana) {
// do stuff
// remove current item
unset($bananas[$key]);
}
print_r($bananas);
Result
Array
(
)
This question is old but I will post my idea using array_slice for new visitors.
while(!empty($bananas)) {
// ... do something with $bananas[0] like
echo $bananas[0].'<br>';
$bananas = array_slice($bananas, 1);
}
I need to determine the keys of values, that have duplicates from an array.
What I came up with is:
$duplicates_keys = array();
$unique = array_unique($in);
$duplicates = array_diff_assoc($in, $unique);
foreach ($in as $key => $val){
if (in_array($val,$duplicates)){
$duplicates_keys[]=$key;
}
}
Which works, but that's pretty resource intensive, is there a faster way to do this?
As per my comment, i doubt this is a bottleneck. However you can reduce your iterations to once through the array, as follows:
$temp=[];
$dup=[];
foreach ($in as $key=>$val) {
if(isset($temp[$val])){
$dup[]=$key;
}else{
$temp[$val]=0;
}
}
Note that the value is set as an array key in temp, so you can use O(1) isset rather than in_array, which must search the full array until the value is found.
This is theoretically faster than your example, but you would need to profile it to be sure (as you should have already done to ascertain your current code is slow).
Probably you can do something else that has a far greater impact, like caching or a better database query
Use array_intersect() for this.
$duplicates_keys = array_intersect($in, $duplicates);
array_intersect()
Which of these two methods (array_count_values or array_key_exists) is more 'friendly' with regard to memory consumption, resource usage and ease of code processing. And is the difference big enough to where a significant performance bump or loss could take place?
If more details on how these methods may be used are needed, I will be parsing a very large XML file and using simpleXML to retrieve certain XML values into an array (to store URL's), like this:
$XMLproducts = simplexml_load_file("products.xml");
foreach($XMLproducts->product as $Product) {
if (condition exists) {
$storeArray[] = (string)$Product->store; //potentially several more arrays will have values stored in them
}}
No duplicate values will be inserted into the array, and I am debating on either using the array_key_exists or array_count_values method to prevent duplicate values. I understand array_key_exists is more , but my question is how much more resource friendly is it? Enough to slow down performance significantly?
I would much rather use the array_count_values method primarily because along with preventing duplicate value in the array, you can also easily display how many duplicate values are in each value. IE if in the storeArray "Ace Hardware" had 3 instances, you could easily display "3" next to the URL, like this:
// The foreach loop builds the arrays with values from the XML file, like this:
$storeArray2[] = (string)$Product->store;
// Then we later display the filter links like this:
$storeUniques = array_count_values($storeArray)
foreach ($storeUniques as $stores => $amts) {
?>
<?php echo $stores; ?> <?php echo "(" . ($amts) . ")" . "<br>";
}
If there is a big performance gap between the 2 methods, I will go with the array_key_exists method. But the code below only prevents duplicate values from being displayed. Is there a way to also display the number of duplicate values (for each value) that would have occurred?
// The foreach loop builds the arrays with values from the XML file, like this:
$Store = (string)$Product->store;
if (!array_key_exists($Store, $storeArray)) {
$storeArray[$Store] = "<a href='webpage.php?Keyword=".$keyword."&Features=".$features."&store=".$Product->store"'>" . $Store . "</a>";
}
// Then we later display the filter links like this:
foreach ($storeArray as $storeLinks) {
echo $storeLinks . "<br>";
}
Use isset() !
It's 2.5x faster than array_key_exists
Source :
Here
If you have any array $p that you populated in a loop like so:
$p[] = array( "id"=>$id, "Name"=>$name);
What's the fastest way to search for John in the Name key, and if found, return the $p index? Is there a way other than looping through $p?
I have up to 5000 names to find in $p, and $p can also potentially contain 5000 rows. Currently I loop through $p looking for each name, and if found, parse it (and add it to another array), splice the row out of $p, and break 1, ready to start searching for the next of the 5000 names.
I was wondering if there if a faster way to get the index rather than looping through $p eg an isset type way?
Thanks for taking a look guys.
Okay so as I see this problem, you have unique ids, but the names may not be unique.
You could initialize the array as:
array($id=>$name);
And your searches can be like:
array_search($name,$arr);
This will work very well as native method of finding a needle in a haystack will have a better implementation than your own implementation.
e.g.
$id = 2;
$name= 'Sunny';
$arr = array($id=>$name);
echo array_search($name,$arr);
Echoes 2
The major advantage in this method would be code readability.
If you know that you are going to need to perform many of these types of search within the same request then you can create an index array from them. This will loop through the array once per index you need to create.
$piName = array();
foreach ($p as $k=>$v)
{
$piName[$v['Name']] = $k;
}
If you only need to perform one or two searches per page then consider moving the array into an external database, and creating the index there.
$index = 0;
$search_for = 'John';
$result = array_reduce($p, function($r, $v) use (&$index, $search_for) {
if($v['Name'] == $search_for) {
$r[] = $index;
}
++$index;
return $r;
});
$result will contain all the indices of elements in $p where the element with key Name had the value John. (This of course only works for an array that is indexed numerically beginning with 0 and has no “holes” in the index.)
Edit: Possibly even easier to just use array_filter, but that will not return the indices only, but all array element where Name equals John – but indices will be preserved:
$result2 = array_filter($p, function($elem) {
return $elem["Name"] == "John" ? true : false;
});
var_dump($result2);
What suits your needs better, resp. which one is maybe faster, is for you to figure out.
I have a large array.
In this array I have got (among many other things) a list of products:
$data['product_name_0'] = '';
$data['product_desc_0'] = '';
$data['product_name_1'] = '';
$data['product_desc_1'] = '';
This array is provided by a third party (so I have no control over this).
It is not known how many products there will be in the array.
What would be a clean way to loop though all the products?
I don't want to use a foreach loop since it will also go through all the other items in the (large) array.
I cannot use a for loop cause I don't know (yet) how many products the array contains.
I can do a while loop:
$i = 0;
while(true) { // doing this feels wrong, although it WILL end at some time (if there are no other products)
if (!array_key_exists('product_name_'.$i, $data)) {
break;
}
// do stuff with the current product
$i++;
}
Is there a cleaner way of doing the above?
Doing a while(true) looks stupid to me or is there nothing wrong with this approach.
Or perhaps there is another approach?
Your method works, as long as the numeric portions are guaranteed to be sequential. If there's gaps, it'll miss anything that comes after the first gap.
You could use something like:
$names = preg_grep('/^product_name_\d+$/', array_keys($data));
which'll return all of the 'name' keys from your array. You'd extract the digit portion from the key name, and then can use that to refer to the 'desc' section as well.
foreach($names as $name_field) {
$id = substr($names, 12);
$name_val = $data["product_name_{$id}"];
$desc_val = $data["product_desc_{$id}"];
}
How about this
$i = 0;
while(array_key_exists('product_name_'.$i, $data)) {
// loop body
$i++;
}
I think you're close. Just put the test in the while condition.
$i = 0;
while(array_key_exists('product_name_'.$i, $data)) {
// do stuff with the current product
$i++;
}
You might also consider:
$i = 0;
while(isset($data['product_name_'.$i])) {
// do stuff with the current product
$i++;
}
isset is slightly faster than array_key_exists but does behave a little different, so may or may not work for you:
What's quicker and better to determine if an array key exists in PHP?
Difference between isset and array_key_exists