CodeLobster IDE

Free cross-platform PHP IDE (HTML, PHP, CSS, JavaScript code editor) with support Drupal, Joomla, Twig, JQuery, BackboneJS, LaravelJS, Node.js, Fhalcon, CodeIgniter, CakePHP, Laravel, Magento, MeteorJS, Phalcon, Symfony, VueJS, WordPress, Yii
It is currently Sun Dec 08, 2019 2:51 am

All times are UTC




Post new topic Reply to topic  [ 2 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:
$data = 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.


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: 3467
Hi.

Please, look at our several samples:

Code:
$data = 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($data,$value){
    foreach ($data as $key => $val) {
        if(in_array($value,$val['info'])){
            return $key;
        }
    }
    return -1;
}

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


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




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



Regards,
Codelobster Team.


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 9 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