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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.