sen5241b Posted April 22, 2021 Share Posted April 22, 2021 I pull the key names out of multi-dimensional associative array all the time. But how do you get the key name for one specific element? Quote Link to comment https://forums.phpfreaks.com/topic/312536-need-age-in-myarrayiage/ Share on other sites More sharing options...
Barand Posted April 22, 2021 Share Posted April 22, 2021 You need to provide context details and a better explanation than that if you want any meaningful replies. Quote Link to comment https://forums.phpfreaks.com/topic/312536-need-age-in-myarrayiage/#findComment-1586072 Share on other sites More sharing options...
phppup Posted April 22, 2021 Share Posted April 22, 2021 Do you want this?? print_r($array_name); Quote Link to comment https://forums.phpfreaks.com/topic/312536-need-age-in-myarrayiage/#findComment-1586077 Share on other sites More sharing options...
sen5241b Posted April 22, 2021 Author Share Posted April 22, 2021 (edited) More detail and context as requested: I use the following lines of code a lot because I use deal with a lot of multi-dimensional associative arrays: $headerNames = implode('‖', array_keys(current($AnyArray))); $ColumnNameLookup = explode('‖', $headerNames); $ColumnNameLookup is a lil array to lookup column names in an associative array. I call them 'column names' but in proper PHP terminology they're called "key names". As you can see its fairly easy to extract the key names and put them in a little array. Those two lines of code has made it easy to write some data manipulation functions that can perform all sorts of operations on a specific column name in any generic array I pass to it. E.g. such as concatenate a string to a string already in the array or highlight a string within a string: HighlightColumnsWithBadData($AnyArray, $ColumnName) My current problem: I have a func that validates a long list of data elements in a multi-dimensional associative array. If a particular element, like 'Age' in $MyArray[$i]['Age'] has bad data, then I need to pass the key name 'Age' to a func that reports the data problem. That is why I need the 'Age' in $MyArray[$i]['Age'] or the 'Name' in $MyArray[$i]['Name']. Why not: if ($MyArray[$i]['Age'] > 700) then pass 'Age' to reporting func? Key names for the numerous arrays I deal with are not hard coded anywhere in the generic data manipulation funcs and that makes these funcs immune to key renames and flexible enough to process any array. Edited April 22, 2021 by sen5241b Quote Link to comment https://forums.phpfreaks.com/topic/312536-need-age-in-myarrayiage/#findComment-1586083 Share on other sites More sharing options...
Barand Posted April 22, 2021 Share Posted April 22, 2021 The usual method for processing an array is with "foreach" which gets you the key and value on each iteration foreach ($myArray[$i] as $key => $value) { if isInvalid($key, $value) { report($key); } } Quote Link to comment https://forums.phpfreaks.com/topic/312536-need-age-in-myarrayiage/#findComment-1586090 Share on other sites More sharing options...
sen5241b Posted April 22, 2021 Author Share Posted April 22, 2021 Yep. I use foreaches all the time. I enhanced your code a bit. Foreach prints the key value and but not the key name. <?php $myArray = array(); $myArray[0] = 'fred'; $myArray[1] = 'Joe'; foreach ($myArray as $key => $value): echo '<br> ' . $myArray[$key]; endforeach; $myArray = array(); $myArray[0]['Name'] = 'fred'; $myArray[0]['Age'] = 55; $myArray[1]['Name'] = 'Joe'; $myArray[1]['Age']= 765; print_r($myArray); foreach ($myArray as $key => $value): echo '<br> '; echo '<br> ' . $myArray[1]['Age']; endforeach; ?> Quote Link to comment https://forums.phpfreaks.com/topic/312536-need-age-in-myarrayiage/#findComment-1586095 Share on other sites More sharing options...
Barand Posted April 22, 2021 Share Posted April 22, 2021 The key names there - you just have to use it. $myArray = array(); $myArray[0]['Name'] = 'fred'; $myArray[0]['Age'] = 55; $myArray[1]['Name'] = 'Joe'; $myArray[1]['Age']= 765; echo '<pre>', print_r($myArray, 1), '</pre>'; foreach ($myArray as $key => $value): echo "<br><b>Person $key:</b><br>"; foreach ($value as $k => $v) { echo " • $k : $v<br>"; } endforeach; Outputs Array ( [0] => Array ( [Name] => fred [Age] => 55 ) [1] => Array ( [Name] => Joe [Age] => 765 ) ) Person 0: • Name : fred • Age : 55 Person 1: • Name : Joe • Age : 765 Quote Link to comment https://forums.phpfreaks.com/topic/312536-need-age-in-myarrayiage/#findComment-1586108 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.