Simple way to identify repeated strings in an array? (PHP) - php

I have an array with values $somearray = array('car','bike','legs,'car'). I'd like to find out which of these values in $somearray are repeated and pick out the index. In this example the answer would be 'car' and the index of the array would be 0 and 3.
I'm wondering if this can be done simply in a few lines, perhaps using some PHP function that I don't know, or do I need to explicitly make comparison in nested loops?
TIA!

The solution is pretty simple and I bet you can write it yourself. All you need is just 2 functions: array_count_values() and array_keys() with specified second argument (thanks to #prodigitalson)

There's a good answer on this similar question - How to detect duplicate values in PHP array?
To get the array indexes, I would then filter any array value > 1 and get the indexes

Related

add a key=>value to all arrays in a 2D array in PHP

I would like to know if there is a quicker way to add the same key=>value pair to every inner array (2nd level) of a 2 dimensional array, other than using a for loop to cycle through every inner array?
Background
The array in question is a data set created with PDO, so am unsure how to inject this at the time of creation as it is not in the database.
(the first part of this should be comments - but for various reasons pre-pended to answer)
First off, PHP does not have multidimesional arrays - it has nested arrays which can look like multidimensional arrays.
Secondly, what are your criteria for "quicker"? Something which executes faster? Something that takes less time to implement? Something else?
While there are functions which operate on arrays, such as array_map(), and therefore require marginally less code than implementing a loop, they execute no faster than a PHP loop (indeed in some cases slower).
it is not in the database
Why do you think that has got anything to do the problem? You can inject the value in the DML statement. Assuming its an SQL database and using MySQL syntax:
SELECT mytable.*, 'value' AS `key`
FROM mytable
WHERE $somecondition

PHP Can the order of array elements (as initialized) be counted on as a behavior?

I'm using numeric keys that are part of my data, if I can count on the order as initialized my solution is easier, friendlier to read, and cleaner code!
Probably obvious but: Between array initialization and the foreach() outputting the data no other array functions will be touching the array.
PHP arrays are implemented as hashes. Even for numeric keys, the keys actually exist and values are associated with them, unlike lists or sets in other languages. You can count on the order to never change on its own, because that would mean actually changing the values associated with the (numeric) keys.
You can count on it. PHP only changes the order after a sort() or similar function call.
You could have found out by var_dump()ing the array yourself, by the way.
If you are asking if:
array("a","b","c")
will always put a into key 1, b into key 2, and c into key 3, then yes, it can be counted on (hence the name array).

What is php's performance on directly accessing array's row with key specified

I have a question regarding array's performance.... how does php handle array keys? I mean if I do something like $my_city = $cities[15]; .... does php directly access the exact row item in $cities array or does php iterate trough array until it finds the matched row?
and if it access the row directly... is there a difference in performance between an array with 100 rows and array with 100,000 rows?
like in this example
$my_city = $cities[15];
PHP's arrays are implemented as hash tables, so the elements are accessed as directly as possible, without iterating through everything. Read more about the algorithm here: http://en.wikipedia.org/wiki/Hash_table
It access it directly. Behind the scene everything is memory pointers arithmetic. I can hardly believe that array concept or implementation in PHP somehow differs from other languages like C, Java or C#.
It depends on what is inside the array too.
// $my_city is a reference to the variable
$cities[15] = new stdobj();
$my_city = $cities[15];
// $my_city is a copy of the array row
$cities[15] = 'foobar';
$my_city = $cities[15];
May not be a direct answer to you question, but a reference to an object usually uses less memory than a variable copy.
there is always an obvious difference in performance between an array with 100 rows and array with 100,000 rows

What is the best way to delete an array of values from an array?

I want to be able to easily and quickly delete an array of needles from a haystack array. In actual fact I have a comma separated list of numbers (though this is a string i know) that need to be deleted from a second comma separated list of number that I pull from a field in my sql database.
So I need -
$orig_needle_list = "3456,5678";
The haystack list is in a field in a mysql database and is "3456, 4567, 5678, 6789" - so I basically want to update this field to "4567,6789"
Q1. Should I retrieve the haystack list, convert both to arrays and iterate over them with a nested foreach cycle and array_splice any matched values?
Q2 Can I use in_array to be faster than nested foreach method?
Q3 is there a way to cut out the middle man and update the field by performing this in an sql query?
thanks
you don't need to iterate over things, there's a function called array_diff:
http://www.php.net/manual/en/function.array-diff.php
So create 2 arrays of the comma separated list and use array_diff, the resulting array is the difference of these two.
Storing comma separated lists in a database isn't a good idea because it breaks normalization.
I think you are looking for array_intersect() http://php.net/manual/en/function.array-intersect.php
try this:
implode(",",array_diff(explode(",",$haystack),explode(",",$orig_needle_list)));

php array push - associative arrays and keep associative keys

I have several associative arrays, each starting with a string key. I also have a master array that i want to use to combine each of these sub arrays. When using array_push though, each array is then given an additional numeric key in the master array.
How can i avoid this and push the sub arrays into the master array keeping the keys intact?
$master_array = array_merge($master_array, $sub_array_1, $sub_array_2, ...) ;
Beware of what happens when the sub arrays have the same keys - if they are numeric, you will get both values, but if not, later values will over-write earlier ones.
As you have not posted any example, it is difficult for me to visualize you code... however, I think you need to use "array_merge" function http://www.php.net/manual/en/function.array-merge.php
Hope that helped.

Categories