Jump to content

How to get an array value by: 1) unique key AND 2) numerical index (row number)


Recommended Posts

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

?>

 

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

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

 

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();
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.