What is the order of foreach loop in PHP? - php

I have an array, for example
$arr=array(
"foo" => "fooval",
"boo" => "booval",
"roo" => "rooval",
);
and then I want to print all elements in pattern "key is value". This code should do the job:
foreach($arr as $key => $val)
echo $key." is ".$val;
Will I get this?
foo is fooval
boo is booval
roo is rooval
I mean the order. Is it guaranteed that it will execute in same order as the array was given, or the arrays are sorted somehow?
Thanks for any answers.

It will loop through the array in sequential order. So in your specific question you would see the results you expect.

arrays are ordered list of values and thier orders do not need to be sorted. It follows a sequence
FROM PHP DOC
An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.

Related

Different datatype values in an array

Why php array allows different datatype to reside in an array as array should contains data of same datatype but that is not the case with php?
For example: - We can add ints, strings, floats, all of these in a single array.
An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.

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

Are PHP Associative Arrays ordered?

I come from python background and the python datatype which is similar (a dictionary) is an unordered set of key value pairs.
I am wondering if PHP associative arrays are unordered? They appear to be ordered.
$test = array(
'test' => 'test',
'bar' => 'bar',
);
var_dump($test);
var_dump(array_slice($test, 0, 1));
Test always comes before bar and I can slice this array as you see. So is this always guaranteed to be ordered across php versions? Is the order just the order that I have declared the array with? So something is internally pointing 'test' to place [0] in the array? I have read http://php.net/manual/en/language.types.array.php but it doesn't shed too much light on this issue. I appreciate your responses. Ty
PHP associative arrays (as well as numeric arrays) are ordered, and PHP supplies various functions to deal with the array key ordering like ksort(), uksort(), and krsort()
Further, PHP allows you to declare arrays with numeric keys out of order:
$a = array(3 => 'three', 1 => 'one', 2 => 'two');
print_r($a);
Array
(
[3] => three
[1] => one
[2] => two
)
// Sort into numeric order
ksort($a);
print_r($a);
Array
(
[1] => one
[2] => two
[3] => three
)
From the documentation:
An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.
The documentation states:
An array in PHP is actually an ordered map.
So yes, they are always ordered. Arrays are implemented as a hash table.
From the php manual:
Arrays are ordered. The order can be changed using various sorting functions. See the array functions section for more information.
I have relied on the fact that they are ordered and it has worked consistently in every project I've had.
The array is ordered but that does not mean the keys are sorted, it means that they are in a given order. Where the precise order is not specified, but it appears to be the order in which you introduced the key-value pairs in it.
To understand it, think what would it mean to not be ordered?
Well think to a relation in a relational database.
A relation is not intrinsically ordered: when you access it with a query the database, unless you provide an order clause, can return the same data in any order.
Even if the data was not modified the same data can be returned in different order.

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.

fastest way to get parent array key in multidimensional arrays with php

what is the best way to get parent array key with multidimensional arrays?
for example I have this array:
array(
[0] => array(0=> sample, 1=>picture, 2=>frame, 3=>google)
[1] => array(0=> iphone, 1=>orange, 2=>love, 3=>msn)
[2] => array(0=> joe, 1=>geee, 2=>panda, 3=>yahoo)
)
now I need to search for example google and get the parent array key..
which it should be 0...any ideas? I used for loop for this but I think it will be slow if I have arrays with 700000 rows..
If you have an array with 700,000 rows you are almost certainly doing something wrong... I would first recomend thinking about utilizing a different data store: flat file or some type of DB.
foreach($array as $key => $value) {
if(in_array('google', $value)) return $key
}
Arrays with 700,000 rows? How many arrays? 9/10 times problem is that you've got your data set up wrongly.
I'm going to go ahead and assume you're doing a search of some sort. As you can't index an array (in the search meaning of index) then you're probably best putting the data into a database and making the most of column indexing to search fast.
Depending on context, you may alternatively want to think about storing your data in files, one per array, and using file searches to find which file contains your value.

Categories