Jump to content

identify index of multidimensional array


Giddy Rob

Recommended Posts

Hey,

 

I have done a function to find a value in a multidemensional array but was wondering how I find out what the main index for the record containing the value?

 

i.e.

 

array

item[0] > [0]=34, [1]=curtains, [2]=£2.99

item[1] > [0]=58, [1]=tent, [2]=£8.99

item[2] > [0]=69, [1]=ruler, [2]=£1.99

 

I can find the value say 58 by using this code to pass the session array and the id number which is stored somewhere in the array:

function findrecord($session, $id){
     foreach($session as $key => $file){
         if($file[0] == $id){
            return $file[0];

         }
     }
}

 

the problem is I need to be able to tell that it is coming from item[1] for my system to work as I want to update it and need to know which one 58 is in the array.

 

Hope that makes sense.

 

Cheers

 

Rob

try

<?php
function findrecord($session, $id){
     foreach($session as $key => $file){
         if($file[0] == $id){
            return $key;
         }
     }
}
$item = array(
array(34, 'curtain', '£2.99'),
array(58, 'tent', '£8.99'),
array(69, 'ruler', '£1.99')
);
$a = findrecord($item, 58);
echo $a;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.