CodeLobster IDE
http://codelobster.com/forum/

Search in a multidimensional array and get the key?
http://codelobster.com/forum/viewtopic.php?f=3&t=24605
Page 1 of 1

Author:  PerterP [ Thu May 23, 2019 6:38 am ]
Post subject:  Search in a multidimensional array and get the key?

Hello everyone, I was wondering if someone could explain to me how I could search in a multidimensional array and get the key?

Here is the example array:
Code:
Quote:
= array(
0 => array(
'name' => 'name one',
'lastname' => 'last name one',
info => array(
'tag' => 'some tag',
'itemId' => 'item id one'
)

),
1 => array(
'name' => 'name two',
'lastname' => 'last name two',
info => array(
'tag' => 'some tag',
'itemId' => 'item id two'
)

)
);


I want to search for "item id two" and get the array key of 1, or if I searched for "item id one" then I get the key 0.
Sorry for my bad English, I hope someone will help me.
Ringtone free: https://ringtonesfree.info/

Author:  Admin [ Mon Jun 03, 2019 9:47 am ]
Post subject:  Re: Search in a multidimensional array and get the key?

Hi.

Please, look at our several samples:

Code:
= array(
0 => array(
'name' => 'name one',
'lastname' => 'last name one',
info => array(
'tag' => 'some tag',
'itemId' => 'item id one'
)

),
1 => array(
'name' => 'name two',
'lastname' => 'last name two',
info => array(
'tag' => 'some tag',
'itemId' => 'item id two'
)

)
);


/*
Example 1
   If all elements of the array have the same structure
*/
function search(,){
    foreach ( as  => ) {
        if(in_array(,['info'])){
            return ;
        }
    }
    return -1;
}

= search(,'item id one');
if( !== -1){
    echo "Array key is ".;
}else{
    echo "Key not found";
}


/*
Example 2
If all the elements of the array have a different structure
*/
function search2(,,=-1){
    foreach ( as  => ) {
        if(is_array()){
             = search2(,,);
        }
        if( !== -1){
            break;
        }else if( == ){
            = ;
            break;
        }
       
    }
    return ;
}




= search(,'item id one');
if( !== -1){
    echo "Array key is ".;
}else{
    echo "Key not found";
}



Regards,
Codelobster Team.

Author:  alkorm [ Sat Dec 09, 2023 6:06 pm ]
Post subject:  Re: Search in a multidimensional array and get the key?

Searching for a specific value within a multidimensional array and retrieving its key involves iterating through the array and checking each element for the desired value. Depending on the programming language, the approach might differ slightly. Here's a general example in JavaScript:

javascript

function findKey(array, value) {
for (var key in array) {
if (array.hasOwnProperty(key)) {
if (Array.isArray(array[key])) {
var innerArray = array[key];
var innerKey = findKey(innerArray, value);
if (innerKey !== null) {
return key + ',' + innerKey;
}
} else {
if (array[key] === value) {
return key;
}
}
}
}
return null;
}

var myArray = {
'key1': 'value1',
'key2': ['value2', 'value3'],
'key3': {
'subkey1': 'value4',
'subkey2': 'value5'
}
};

var searchValue = 'value5';
var result = findKey(myArray, searchValue);
console.log('Key:', result);
This code defines a function findKey that recursively searches through a JavaScript object (which can be considered a multidimensional array) to find a specific value. This function can work for arrays with multiple levels of nesting.

Keep in mind that depending on your programming language or the specific structure of your array, the implementation might vary.

Author:  gulshan212 [ Fri Jan 19, 2024 10:51 am ]
Post subject:  Re: Search in a multidimensional array and get the key?

Well, you can try the correct, complete code below.


Code:
<?php
$yourArray = array(
    0 => array(
        'name' => 'name one',
        'lastname' => 'last name one',
        'info' => array(
            'tag' => 'some tag',
            'itemId' => 'item id one'
        )
    ),
    1 => array(
        'name' => 'name two',
        'lastname' => 'last name two',
        'info' => array(
            'tag' => 'some tag',
            'itemId' => 'item id two'
        )
    )
);

$searchItemId = 'item id two';  // Change this to the itemId you're searching for

foreach ($yourArray as $key => $value) {
    if ($value['info']['itemId'] === $searchItemId) {
        echo "Key: $key";
        break;  // If you found a match, stop the loop
    }
}

?>


Thanks

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/