1

I'm sure this has been asked before, but I can't seem to find the answer.

To that end, I have an array that looks like this:

Array
(
    [0] => Array
        (
            [status] => active
            [sid] => 1
        )

    [1] => Array
        (
            [status] => expired
            [sid] => 2
        )

)

What I'd like to be able to do is type $arrayName["active"] and it return the SID code. I will be using this like a dictionary object of sorts. It's like I need to reindex the array so that it is the key/value pair that I need. I was just wondering if there was an easier way to do it.

1
  • 1
    What if there's 3 subarrays and 2 are 'active'? an array key can only point to one "value" (even if that value is another array). Commented Jan 22, 2012 at 7:01

6 Answers 6

3

You should convert your nested arrays into a single associative array. Something like this should take your example and turn it into an associative array:

$assoc_array = array();
foreach( $example_array as $values ) {
  $assoc_array[$values["status"]] = $values["sid"];
}

You can then access the sid for a given status by using $assoc_array["expired"] (returns 2)

Sign up to request clarification or add additional context in comments.

1 Comment

This is what I ended up doing - now I can access the array like I want. Thanks!
2

After seeing the others' solutions, I realize this might be bit of an overkill, but I'm still just gonna throw it out there:

$foo = array(
  array('status' => 'active', 'sid' => 1),
  array('status' => 'expired', 'sid' => 2),
);

// Get all the 'status' elements of each subarray
$keys = array_map(function($element) {
  return $element['status'];   
}, $foo);

// Get all the 'sid' elements of each subarray
$values = array_map(function($element) {
  return $element['sid'];
}, $foo);

// Combine them into a single array, with keys from one and values from another
$bar = array_combine($keys, $values);

print_r($bar);

Which prints:

Array
(
    [active] => 1
    [expired] => 2
)

Manual pages:

Comments

1

You can use this function:

  function findActive($my_array){

    foreach($my_array as $array){
      foreach($array as $val){
        if($val['status']==='active'){
          return $val['sid'];
        }
      }
    }

    return false;

  }

Comments

0

access it via a loop or directly.

if($arrayName[0]['status'] == "active") {
   echo $arrayName[0]['sid'];
}

If you want to check all the SIDs

foreach($arrayName as $item) {
  if($item['status'] == "active") {
     echo $item['sid'];
  }
}

A more direct approach is just putting the loop in a function and return an array of all active session IDs

Comments

0

$sidArr = array();
foreach($yourArr as $val) {
  if("active" == $val["status"]) {
    array_push($sidArr, $val["sid"]);
  }
}

Comments

0

reindex would be the best

$arrayName = array()
foreach ($data_array as $data)
   $arrayName[$data['status']]=$data['sid'];

Or use a function

function get_sid($status)
{
    global $data_array;
    foreach ($data_array as $data) {
        if ($data['status']==$status)
             return $data['sid'];
    }
    return false;    
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.