Jump to content

2 dimensional array search


kjl-php

Recommended Posts

I think an array is the best choice for this because I don't want to use a db but I could be wrong.

 

I've only done basics in PHP so this is th first time doing anything with arrays.

 

Here's what I have defined:

 

<?php

$my_array = array(

array(1,'somevalueA','somevalueB'),

array(2,'anothervalueA','anothervalueB')

);

?>

 

I want to search through the array for say 1 or 2 or 3 or 4 etc.

When it finds the 1 for example I would like to store the value somevalueB in a variable.

If I'm looking for 2 I will store anothervalueB in a variable, in essence I will always be looking for the last value (the B value)

 

I've seen a couple of functions but I couldn't figure out what to do.

Please tell me the code to use? Thanks in advance.

 

Keith

Link to comment
https://forums.phpfreaks.com/topic/97743-2-dimensional-array-search/
Share on other sites

Instead of searching for the 1,2 etc, make them the keys

<?php

$my_array = array(
        1 => array('somevalueA','somevalueB'),
        2 => array('anothervalueA','anothervalueB')
);

    //
    //    now update b value for key 2
    //
$my_array[2][1] = "new_value";

?>

Thanks for the quick reply.

One more question if I can...

 

so in that small array all the values could be referenced this way:

 

$my_array[1][0] would equal somevalueA?

$my_array[1][1] would equal somevalueB?

$my_array[2][0] would equal anothervalueA?

$my_array[2][1] would equal anothervalueB?

 

Would that be how I would grab the values?

Thanks again...

 

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.