ExpertAlmost Posted August 3, 2010 Share Posted August 3, 2010 Good morning! I have a two dimensional array, basically a table (see code below). I want to get a value from the array using two methods: 1) Using the row's key: $NewValue = $MyArray[$UniqueKey]; 2) Using the row's index (row number, so to speak): $NewValue = $MyArray[$RowNumber]; The second print statement in the code below does not work. Both print statements should output the same value. Is there an easy way to do this? The table has hundreds of rows and I will not know the key value of row 879 nor can I generate it. So I cannot use array_keys(). And I DO NOT want to start at the first row and count up to the 879th row. Any clever ideas to share and enlighten? Thanks! <?php // Initialize the array keys and values $MyArray = array(); $MyArray['first']['col1'] = 'abc'; $MyArray['first']['col2'] = 'def'; $MyArray['first']['col3'] = 'ghi'; $MyArray['second']['col1'] = 'jkl'; $MyArray['second']['col2'] = 'mno'; $MyArray['second']['col3'] = 'pqr'; $MyArray['third']['col1'] = 'stu'; $MyArray['third']['col2'] = 'vwx'; $MyArray['third']['col3'] = 'yz'; $MyArray['fourth']['col1'] = 'a1a'; $MyArray['fourth']['col2'] = 'b2b'; $MyArray['fourth']['col3'] = 'c3c'; $MyArray['fifth']['col1'] = 'ddd'; $MyArray['fifth']['col2'] = 'eee'; $MyArray['fifth']['col3'] = 'fff'; // Two methods to get a value. Second one does nothing. print"{$MyArray['third']['col2']}</br>"; print"{$MyArray[2]['col2']}</br>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/209644-how-to-get-an-array-value-by-1-unique-key-and-2-numerical-index-row-number/ Share on other sites More sharing options...
JonnoTheDev Posted August 3, 2010 Share Posted August 3, 2010 I have a two dimensional array Multidimensional. There is no such array key in the example below! $MyArray[2]['col2'] You array is using text values for its keys i.e 'first','second' For the above to work your array would have to look like <?php $myArray = array(1 => array('first' => array('col1' => 'abc')), 2 => array('first' => array('col1' => 'def'))); ?> Use the following to view your array and you will see its structure <?php print "<pre>"; print_r($myArray); print "</pre>"; exit(); ?> Also, to find an array key from its value use the function array_search() http://uk3.php.net/array_search Hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/209644-how-to-get-an-array-value-by-1-unique-key-and-2-numerical-index-row-number/#findComment-1094529 Share on other sites More sharing options...
ExpertAlmost Posted August 3, 2010 Author Share Posted August 3, 2010 That does help! Thank you very much. I tried using array_search, but the problem is that in many cases, the value is repeated and array_search only returns the first value it finds. The PHP manual suggests using array_keys, but it does not seem to work in 2 dimensional arrays. Example below just returns an empty array when there are three different keys with those values: $FoundKeys = array_keys($MyArray,"ddd"); Any thoughts as to why? //init the array $MyArray['first']['col1'] = 'abc'; $MyArray['first']['col2'] = 'def'; $MyArray['first']['col3'] = 'ghi'; $MyArray['second']['col1'] = 'jkl'; $MyArray['second']['col2'] = 'mno'; $MyArray['second']['col3'] = 'pqr'; $MyArray['third']['col1'] = 'stu'; $MyArray['third']['col2'] = 'vwx'; $MyArray['third']['col3'] = 'yz'; $MyArray['fourth']['col1'] = 'ddd'; $MyArray['fourth']['col2'] = 'b2b'; $MyArray['fourth']['col3'] = 'c3c'; $MyArray['fifth']['col1'] = 'ddd'; $MyArray['fifth']['col2'] = 'eee'; $MyArray['fifth']['col3'] = 'fff'; $MyArray['sixth']['col1'] = 'ggg'; $MyArray['sixth']['col2'] = 'hhh'; $MyArray['sixth']['col3'] = 'iii'; $MyArray['seventh']['col1'] = 'ddd'; $MyArray['seventh']['col2'] = 'kkk'; $MyArray['seventh']['col3'] = 'lll'; //print out values based on key print"{$MyArray['third']['col2']}</br>"; $Keys = array_keys($MyArray); print "{$MyArray[$Keys[2]]['col2']}</br>"; //search for values and return the keys, should be three instances. $FoundKeys = array_keys($MyArray,"ddd"); print_r($FoundKeys); Quote Link to comment https://forums.phpfreaks.com/topic/209644-how-to-get-an-array-value-by-1-unique-key-and-2-numerical-index-row-number/#findComment-1094531 Share on other sites More sharing options...
JonnoTheDev Posted August 3, 2010 Share Posted August 3, 2010 You cannot use these functions on such an array. You require a loop to search the array. <?php $MyArray['first']['col1'] = 'abc'; $MyArray['first']['col2'] = 'def'; $MyArray['first']['col3'] = 'ghi'; $MyArray['second']['col1'] = 'jkl'; $MyArray['second']['col2'] = 'mno'; $MyArray['second']['col3'] = 'pqr'; $MyArray['third']['col1'] = 'stu'; $MyArray['third']['col2'] = 'vwx'; $MyArray['third']['col3'] = 'yz'; $MyArray['fourth']['col1'] = 'ddd'; $MyArray['fourth']['col2'] = 'b2b'; $MyArray['fourth']['col3'] = 'c3c'; $MyArray['fifth']['col1'] = 'ddd'; $MyArray['fifth']['col2'] = 'eee'; $MyArray['fifth']['col3'] = 'fff'; $MyArray['sixth']['col1'] = 'ggg'; $MyArray['sixth']['col2'] = 'hhh'; $MyArray['sixth']['col3'] = 'iii'; $MyArray['seventh']['col1'] = 'ddd'; $MyArray['seventh']['col2'] = 'kkk'; $MyArray['seventh']['col3'] = 'lll'; // print out values based on key print $MyArray['third']['col2']."<br />"; // search for value $search = "ddd"; $keys = array(); foreach($MyArray as $number => $col) { foreach($col as $colname => $value) { if($value == $search) { $keys[$number] = $colname; } } } print "The search for ".$search." is found in the following arrays<br />"; print "<pre>"; print_r($keys); print "</pre>"; exit(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/209644-how-to-get-an-array-value-by-1-unique-key-and-2-numerical-index-row-number/#findComment-1094535 Share on other sites More sharing options...
ExpertAlmost Posted August 3, 2010 Author Share Posted August 3, 2010 That's fantastic Neil! Works great. Thank you for sharing your expertise and time. And so early in the morning also Keep sharing the wealth. Quote Link to comment https://forums.phpfreaks.com/topic/209644-how-to-get-an-array-value-by-1-unique-key-and-2-numerical-index-row-number/#findComment-1094539 Share on other sites More sharing options...
JonnoTheDev Posted August 3, 2010 Share Posted August 3, 2010 No probs. I'm in the UK so it's early afternoon here. Quote Link to comment https://forums.phpfreaks.com/topic/209644-how-to-get-an-array-value-by-1-unique-key-and-2-numerical-index-row-number/#findComment-1094540 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.