array-key

Make a unique list of values from a particular key existing anywhere in a deep array

核能气质少年 提交于 2019-12-13 00:22:42
问题 I have an array that consists of an undetermined number of arrays, recursively (n levels deep). Each array might contain a name key. I want to create a unique list of those values. Example Suppose the array is: $bigArray = array( 'name'=>'one', 'something'=>array( 'name'=>'two', 'subthing'=>array('name'=>'three') ), 'anotherthing'=>array('name'=>'one') ); The expected result would be: $uniques = array('one', 'two', 'three') // All the 'name' keys values and without duplicates. Here's a fiddle

php array difference against keys of an array and an array of keys?

房东的猫 提交于 2019-12-12 17:23:06
问题 Suppose we have two arrays: $a=array('1'=>'Apple','2'=>'Microsoft', '3'=>'Microapple','4'=>'Applesoft','5'=>'Softapple'); $b=array(1,3); Where $b array represents the keys of array $a to be differentiated against. And we expect to receive another array $c with the following values: $c=array('2'=>'Microsoft','4'=>'Applesoft','5'=>'Softapple'); In php manual there are two functions: array_diff($array1,$array2); //difference of values array_diff_key($array1,$array2);//difference of keys But

compare string to sub key value in php array partial match

醉酒当歌 提交于 2019-12-11 22:42:06
问题 I need to figure out a way to partial match a string to a sub key in my PHP array. example: string = howdy-doody show as you can see there is a - dash and a space between the words. In my PHP array the sub key might be howdy doody show with no dashes or it might be howdy doody-show with the dash between a different word in the string. How can I find the sub key in the array with the string given? sample array $pages = array( 'Administrator' => array( 'network-administrator' => array('title' =

php explode and force array keys to start from 1 and not 0

好久不见. 提交于 2019-12-08 17:29:40
I have a string that will be exploded to get an array, and as we know, the output array key will start from 0 as the key to the first element, 1 for the 2nd and so on. Now how to force that array to start from 1 and not 0? It's very simple for a typed array as we can write it like this: array('1'=>'value', 'another value', 'and another one'); BUT for an array that is created on the fly using explode, how to do it? Thanks. $exploded = explode('.', 'a.string.to.explode'); $exploded = array_combine(range(1, count($exploded)), $exploded); var_dump($exploded); Done! Just use a separator to create a

php explode and force array keys to start from 1 and not 0

本秂侑毒 提交于 2019-12-08 08:52:44
问题 I have a string that will be exploded to get an array, and as we know, the output array key will start from 0 as the key to the first element, 1 for the 2nd and so on. Now how to force that array to start from 1 and not 0? It's very simple for a typed array as we can write it like this: array('1'=>'value', 'another value', 'and another one'); BUT for an array that is created on the fly using explode, how to do it? Thanks. 回答1: $exploded = explode('.', 'a.string.to.explode'); $exploded = array

How to wrap around in PHP array when index falls off the end?

こ雲淡風輕ζ 提交于 2019-12-06 03:04:31
I want to be able to retrieve the value of an array by using the numeric key. The catch is that if the key is beyond the array length, I need it to loop through the array again. $my_array = array('zero','one','two','three','four','five','six','seven'); function loopArrayValues($array,$key){ //this is what is needed to return return } echo "Key 2 is ".loopArrayValues($my_array,2)."<br />"; echo "Key 11 is ".loopArrayValues($my_array,11)."<br />"; echo "Key 150 is ".loopArrayValues($my_array,11)."<br />"; Expected output: Key 2 is two Key 11 is three Key 150 is three My research references:

replace array keys with given respective keys

末鹿安然 提交于 2019-11-30 09:05:24
I have an array like below $old = array( 'a' => 'blah', 'b' => 'key', 'c' => 'amazing', 'd' => array( 0 => 'want to replace', 1 => 'yes I want to' ) ); I have another array having keys to replace with key information. $keyReplaceInfoz = array('a' => 'newA', 'b' => 'newB', 'c' => 'newC', 'd' => 'newD'); I need to replace all keys of array $old with respective values in array $keyReplaceInfo . Output should be like this $old = array( 'newA' => 'blah', 'newB' => 'key', 'newC' => 'amazing', 'newD' => array( 0 => 'want to replace', 1 => 'yes I want to' ) ); I had to do it manually as below. I am

How to get a subset of $_POST array with keys starting with a prefix

China☆狼群 提交于 2019-11-29 13:03:30
问题 Let's say my $_POST variable looks like: <?php Array ( [user_ID] => 65 [action] => editpost [originalaction] => editpost [post_author] => 154 [empl_bd_dd] => 6 [empl_bd_mm] => 5 [empl_bd_yy] => 1987 [empl_gen] => 1 [empl_height] => 155 [empl_weight] => 61 [empl_arra] => 2 [save] => Update [post_it] => 2 [empl_pay] => J77 [empl_cust] => Married [empl_lang] => Array ( [0] => EN [1] => FR ) [empl_rent] => 1 [name] => Jimmy Nathan [empl_text] => Lorem ipsum dolor sit amet, consectetur adipiscing

PHP - Get key name of array value

痴心易碎 提交于 2019-11-27 02:46:43
I have an array as the following: function example() { /* some stuff here that pushes items with dynamically created key strings into an array */ return array( // now lets pretend it returns the created array 'firstStringName' => $whatEver, 'secondStringName' => $somethingElse ); } $arr = example(); // now I know that $arr contains $arr['firstStringName']; I need to find out the index of $arr['firstStringName'] so that I am able to loop through array_keys($arr) and return the key string 'firstStringName' by its index. How can I do that? zrvan If you have a value and want to find the key, use

array_key_exists is not working

给你一囗甜甜゛ 提交于 2019-11-26 23:01:41
array_key_exists is not working for large multidimensional array. For ex $arr = array( '1' => 10, '2' => array( '21' => 21, '22' => 22, '23' => array( 'test' => 100, '231' => 231 ), ), '3' => 30, '4' => 40 ); array_key_exists('test',$arr) returns 'false' but it works with some simple arrays. halfdan array_key_exists does NOT work recursive (as Matti Virkkunen already pointed out). Have a look at the PHP manual, there is the following piece of code you can use to perform a recursive search: <?php function array_key_exists_r($needle, $haystack) { $result = array_key_exists($needle, $haystack);