Get nth key of associative php array [duplicate] - php

This question already has answers here:
Accessing an associative array by integer index in PHP
(6 answers)
Closed 7 years ago.
I want to get the value of the KEY of an associative PHP array at a specific entry. Specifically, I know the KEY I need is the key to the second entry in the array.
Example:
$array = array('customer' => 'Joe', 'phone' => '555-555-5555');
What I'm building is super-dynamic, so I do NOT know the second entry will be 'phone'. Is there an easy way to grab it?
In short, (I know it doesn't work, but...) I'm looking for something functionally equivalent to: key($array[1]);

array_keys produces a numerical array of an array's keys.
$keys = array_keys($array);
$key = $keys[1];
If you're using PHP 5.4 or above, you can use a short-hand notation:
$key = array_keys($array)[1];

Related

How to create a function finding the last key of an array using value PHP in different ways [duplicate]

This question already has answers here:
PHP - Get key name of array value
(9 answers)
Closed 4 years ago.
How to get the Cat by using the value Nap in array
["Dog" => "Bite", "Cat" => "Nap"]
and i want to get Cat using value Nap
$key = array_search('Nap', $array);
What is not clear in your question is if you already know the entry is the last entry, or if you're searching for a value that is somewhere in your array.
If you have PHP 7.3, and you know it's the last entry, you can use array_key_last() (see http://php.net/manual/en/function.array-key-last.php )
$key = array_search('Nap', $array); (as mentionned by #Sanu0786 )

Imploding specific associative array key values [duplicate]

This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
I've got associative array with results from database containing data like in following structure:
$arr[0] = Array("id"=>4, "otherdata"=>"something");
$arr[1] = Array("id"=>6, "otherdata"=>"something else");
$arr[2] = Array("id"=>15, "otherdata"=>"something totally different");
I would like to implode data that is only in id key for each $arr entry, so that final imploded string is 4,6,15 (gluded with ,).
Right now I've got some solutions:
Doing it in pure PHP within Smarty.
Creating function that will implode result from array_map which creates new table with id's only.
Assigning variable within Smarty template and creating implode-like result string with foreach.
but neither of them I am happy of.
Is there any other simple way to achieve desired result?
The 4th solution:
echo implode(',', array_column($arr, 'id'));

Set Array Equal to Another Array without Specified Key/Values [duplicate]

This question already has answers here:
php array difference against keys of an array and an array of keys?
(3 answers)
Unset elements using array keys
(2 answers)
Closed 5 years ago.
I am attempting to mirror the behavior of newArray = oldArray, with the caveat of excluding some key/values of the oldArray, so something like newArray = oldArray - undesiredOldKeyValue. I realize this is fully doable with a foreach on the oldArray and using an if to see if the encountered key is desired or not, but I am interested in a simpler or more concise approach if possible.
A couple of things to keep in mind, I need to exclude key/value pairs based on key, not value. I do not want to modify the oldArray in the process of doing this.
You may try to use array_filer. Something like:
$new_array = array_filter($old_array, function ($value, $key) {
// return false if you don't want a value, true if you want it.
// Example 1: `return $value != 'do not keep this one';`
// Example 2: `return !in_array($key, ['unwanted-key1', 'unwanted-key2', 'etc']);`
}, ARRAY_FILTER_USE_BOTH);
It will filters elements of an array using a callback function.

How do PHP arrays actually work? [duplicate]

This question already has answers here:
Are PHP Associative Arrays ordered?
(4 answers)
Closed 8 years ago.
I'm learning PHP and I've got a question that's bothering me. PHP arrays seem to be hashmaps internally. If you give an array a key and value, it almost certainly has to put the key through some sort of hashing function before placing it in an actual array, right? Why then, if I give an array a series of keys and values and then dump these to screen, does PHP maintain the order in which I entered the values?
for instance:
$arr = array();
$arr[1] = 'one';
$arr[3] = 'three';
$arr[2] = 'two';
foreach($arr as $key => $val)
echo "$key => $val<br>"
would render "1 => one, 2 => two, 3 => three" in a typical hashmap, but instead I get "1 => one, 3 => three, 2 => two." Which to me means that there have to be both and order and a key being maintained in whatever datatype this actually is.
Thanks in advance for any explanation.
You are correct about the array being stored as a hash table or ordered map. Basically, everything in PHP is a hash table.
See here: Understanding PHP's internal array implementation

PHP using a string with multiple child keys as a lookup key for an associated array [duplicate]

This question already has answers here:
String to multidimensional array path
(1 answer)
Nested numbering to array keys
(1 answer)
Closed 8 years ago.
I would like a multiple child 'key' as a 'lookup' to get a object out of various associative arrays. For example:
$lookup_key = "['objects'][0]['object2']";
// this will be stored so when I need to get an object's value from various different arrays I can use these string to form a lookup key to get certain objects
$object_array= array();
$object_array= ["objects"=>[["object1"=>"boot", "object2"=>"shoe"],"object"], "flowers"];
// using that key get the value
$object_value= $object_array->lookup($lookup_key);
Now does php have a method that already does this type of lookup or I suppose its an multidimensional key?
Any help would be great! Thanks in advance. This is part of an object lookup table.
I approached this in a slightly different way by storing a string of the objects then exploding that into an array from that travelling across that array to get the value:
$object_array= array();
$object_array= ["objects"=>[["object1"=>"boot", "object2"=>"shoe"],"object"], "flowers"];
$lookup=array();
$lookup_key="objects,0,object2";
$lookup=explode(',',$lookup_key);
$temp_object=array();
$new_object_array=array();
$new_object_array=$object_array;
$count=count($lookup);
for($i=0; $i<$count; $i++) {
$temp_object=$new_object_array[$lookup[$i]];
$new_object_array=$temp_object;
}
echo "\n value: $new_object_array";
As I was search for a quick implementation this seemed to be the best way within my time constraints and php lose variable casting.

Categories