CodeLobster IDE

Free cross-platform PHP IDE (HTML, PHP, CSS, JavaScript, Python code editor) with support Drupal, Joomla, Twig, JQuery, BackboneJS, LaravelJS, Node.js, CodeIgniter, CakePHP, Laravel, Magento, MeteorJS, Phalcon, Symfony, VueJS, WordPress, Yii
It is currently Fri Feb 23, 2024 7:42 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Search in a multidimensional array and get the key?
PostPosted: Thu May 23, 2019 6:38 am 
Offline

Joined: Mon Dec 03, 2018 2:01 am
Posts: 3
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.


Last edited by PerterP on Mon Sep 06, 2021 11:59 am, edited 2 times in total.

Top
 Profile  
 
 Post subject: Re: Search in a multidimensional array and get the key?
PostPosted: Mon Jun 03, 2019 9:47 am 
Offline
Site Admin

Joined: Wed Sep 12, 2007 2:18 pm
Posts: 3931
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.


Top
 Profile  
 
 Post subject: Re: Search in a multidimensional array and get the key?
PostPosted: Sat Dec 09, 2023 6:06 pm 
Offline

Joined: Sat Dec 09, 2023 4:57 pm
Posts: 1
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.


Top
 Profile  
 
 Post subject: Re: Search in a multidimensional array and get the key?
PostPosted: Fri Jan 19, 2024 10:51 am 
Offline

Joined: Tue Jan 16, 2024 10:46 am
Posts: 2
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 17 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2016 phpBB Group