2

How can I get the key and the value ? I tried using key() but it didn't work:

$array_newbie = array();
$array_newbie['121412'] = "Hello";
$array_newbie['121212'] = "Noob";
$array_newbie['155161'] = "nabbaa";
foreach($array_newbie as $k)
{
echo key($array_newbie) . "\n";
}

this outputs:

121212
121212
121212

how can I get the key value ? I want it to output

121412
121212
155161

i'm new to php and having trouble with this multi dimensional arrays, thanks

1 Answer 1

1

key() is getting the key of the current array pointer. In your case, it is always 0.

You can get it via the foreach.

foreach($array_newbie as $key => $k) {
    echo $key . "\n";
}

Also, that array is an associative array, not a multi-dimensional array.

The latter is an array of whom their members are also arrays...

$arr = array(
   array(
      'a', 'b', 'c'
   ),
   array(
      'd', 'e', 'f'
   )
);
Sign up to request clarification or add additional context in comments.

3 Comments

got it, thank you very much, another quick question: How can I remove everything before a word in a string ? example: I have this: colegas-que-manjam-de-harry-potter_t_1578519 and I want this: 1578519, removing everything before t and t
@AndréCardoso: That is really a new question in itself. Ask it and link me :)
Heh, thank you very much, I will link you in 13 minutes ( time till I can post another question, lol )

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.