php array push - associative arrays and keep associative keys - php

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.

Related

Merge PHP multidimensional-array on key, parent keys and depth. In other words an array to a binary search tree.

I have an array that I am trying to merge. The merging is dependent upon the key, the parent keys and the depth of the array. I can manipulate the array in a number of ways to achieve unique key values. The outcome is technically a binary search tree. I have attached two files json_in.txt is what I currently have and json_out.txt is what I want. You will notice that the json_in file has some unique key names in order to merge and the json_out file just has "name" and "children" keys. My plan was to use preg_replace_all to remove the digits from the json string after the array was merged, but if you know a better way then great!
Again, I am trying to make the json array in json_in.txt into json_out.txt and I can manipulate the key of json_in to whatever is needed in order to accomplish this.
json_in.txt
json_out.txt

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).

Simple way to identify repeated strings in an array? (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

How do I sort while still keeping the same array indices?

The index is automatic, the indexes are 0, 1, 2, etc. nothing special.
But the key to the database is auto-incremented. meaning the index directly correlates to the key of the database. So after the sort, I can do a query and retrieve the info on that row with that key.
But I think that once you sort(), it changes the index, therefore I would lose that correlation between the index and the key of the database therefore making the array quite useless...so my question is how do I keep that correlation while still sorting so that the highest value goes to the top?
And then after that's successful, how do I sort through an array like that?
Thanks so much for your help and patience,
Binny
I think what you want is asort() or arsort(). According to the documentation for asort(),
This function sorts an array such that
array indices maintain their
correlation with the array elements
they are associated with. This is used
mainly when sorting associative arrays
where the actual element order is
significant.
arsort() sorts in the reverse direction.
Use asort: http://www.php.net/manual/en/function.asort.php

Can you say that associative arrays in PHP are like 2D arrays?

Can you say that associative arrays in PHP are like 2D arrays?
No, they are still one-dimensional just like regular 0-based arrays. The difference is that you aren't limited to integers for the keys; you can use any arbitrary string.
And strictly speaking there isn't a technical distinction between associative and non-associative arrays. They use the same syntax, it's just your choice whether you use integers or strings or both for the keys.
A 2D array is more like a matrix, a plane, a coordinate system. An associative array on the other hand could be called a dictionary or hash.
$var[$x] = 1-dimensional
$var[$y][$y2] = 2-dimensional
$var[$z][$z2][$z3] = 3-dimensional
It doesn't matter if $x, $y or $z are numeric or strings, actually.
From wikipedia
Associative array
An associative array (also associative
container, map, mapping, dictionary,
finite map, and in query-processing an
index or index file) is an abstract
data type composed of a collection of
unique keys and a collection of
values, where each key is associated
with one value (or set of values).
So an associative array is actually an ADT, implemented in another way.
Instead, a 2d array "really" has two dimensions and is, usually, a primitive type.

Categories