Page 1 of 1

Get an array value by: 1) unique key AND 2) numerical index

Posted: Tue Aug 03, 2010 3:39 am
by ExpertAlmost
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>";
?>

Re: Get an array value by: 1) unique key AND 2) numerical in

Posted: Tue Aug 03, 2010 4:04 am
by requinix
ExpertAlmost wrote: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().
How does that mean you can't use array_keys?

Code: Select all

print "{$MyArray['third']['col2']}</br>";
$keys = array_keys($MyArray);
print "{$MyArray[$keys[2]]['col2']}</br>";

Re: Get an array value by: 1) unique key AND 2) numerical in

Posted: Tue Aug 03, 2010 4:18 am
by ExpertAlmost
Thank you Tasairis!

I was thinking in terms of $Value = array_keys($MyArray, $UniqueKey); using the $search_value. But since I do not know what the unique key will be for the 879th row... But your solution is a certainly clear enough! :D

I am wondering about the processing time however in that solution however. A direct lookup into an array using indexes $MyArray[$Row][$Col] versus generating an entire list of keys, direct index into it and then again into the main array. The arrays could be thousands of rows and I do alot of indexing into them.

Any thoughts?

Thank you again for the clever idea.

Re: Get an array value by: 1) unique key AND 2) numerical in

Posted: Tue Aug 03, 2010 7:07 am
by requinix
If performance is a big issue then PHP isn't the language to use for this.

First thing to think about: If using array_keys made a little bit of a performance hit - not much, but noticeable, like a quarter-second - then what would you be prepared to do about it?
Second thing to think about: Why not make those changes now?

Re: Get an array value by: 1) unique key AND 2) numerical in

Posted: Tue Aug 03, 2010 7:20 am
by ExpertAlmost
Good points all around. Thank you again!

By the way, another user told me that what I wanted to do could not be done in any case. So I sent them your solution :)

I am in the process of reformatting my structures to use names as keys which is making many things simpler. But it has revealed some new unknowns for me. The most confusing of which is why the following returns an empty array when I changed my array to have 3 values of 'ddd':
$FoundKeys = array_keys($MyArray,'ddd'); (last line of the code below)

//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);

Learning is endless...

Re: Get an array value by: 1) unique key AND 2) numerical in

Posted: Tue Aug 03, 2010 8:01 am
by ExpertAlmost
Apparently, array_keys cannot be used on multi-dimensional arrays.

However, a user did provide me with the following solution which is simple and works great!

//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>";

Hope this helps others as much as it helped me. Thank you everyone :)