1

I have a 0 indexed array that I can't do much about, but inside this array there are values that I need to echo. example array is:

$x =  array(0 => array('store'=> 107));

I would like to have 2 variables that both echo texts store and 107

I could do this, using

$var1 = array_keys($x[0]); 
$var2 = array_values($x[0]); 

echo $var1[0]; // store 
echo $var2[0]; // 107

I would like to know if there is a more effective way of getting those values, or remving that first 0 index. as array_filter($x) or unset($x) obviously don't work as in other cases.

4
  • It's only the one element at [0] you will need to get at? Commented Dec 20, 2014 at 0:56
  • 1
    $x = $x[0] will give $x = array('store'=> 107) and you may keep it like that Commented Dec 20, 2014 at 0:56
  • 1
    You could use current($x) instead of $x[0]. I don't see the benefit, though. Commented Dec 20, 2014 at 0:58
  • thanks @Barmar I didn't know current could be used for this. Although there Nordenheim's answer is neat also. I suppose you don't think there is any different to from memory footprint side, even though it is too small to scale. I just got curious to know. Commented Dec 20, 2014 at 1:09

2 Answers 2

2

As an alternative, you could also use combinations of key() and reset() if you're curious.

$x =  array(0 => array('store'=> 107));

$y = reset($x); // point to first element
$key = key($y); // get the current key, store
$val = reset($y); // get the value
echo $key; // store
echo $val; // 107
Sign up to request clarification or add additional context in comments.

Comments

1

this should work for you.

$x =  array(0 => array('store'=> 107));

foreach($x as $y){
    foreach ($y as $key => $value){
        echo $key;
        echo $value;
    }
}

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.