Jump to content

Recommended Posts

Hi...

 

I have an array '$names' with the following...

 

Array

(

    [un] => a

    [pa] => v

)

 

However this array gets used in a for loop and I need to pull the value out using a number i.e.

$myname = $names[1] // should print out v

 

When I try this it fails, any tips would be helpful. Or if there is a function for converting array keys into numbers?

Link to comment
https://forums.phpfreaks.com/topic/207505-array-help/
Share on other sites

Hi,

 

you cant index your array with a numeric becouse that is an accociative array.

create an enumerative array from your ass.arr example like this (with foreach or use @ProjectFear suggestion [array_values()] which is more beautiful i think):

 

$new_array();

foreach ($names as $name)
{
  $new_array[] = $name;
}

 

and after that you can index with a number your enumerative $new_array that holds your $names array content.

 

edit:

yeah, i was a bit mindful with the code, sry:

 

$new_array();

foreach ($names as $k => $name)
{
  $new_array[] = $name;
}

Link to comment
https://forums.phpfreaks.com/topic/207505-array-help/#findComment-1084873
Share on other sites

That coding is wrong, if you want to array, do like this!

 

<?
$string = array ("A", "V");
echo $string[0]
echo $string[1]
?>

 

And the result from that will be

 

A

V

 

No, the result from that (assuming short tags are enabled), will be a parse error. While what you have provided is close enough to what can also be done, it depends on how the OP is retrieving the said array.

Link to comment
https://forums.phpfreaks.com/topic/207505-array-help/#findComment-1084877
Share on other sites

Example shows three ways to loop through an array, assuming the need for a numeric key is not essential.  If you need to pick out a specific key/value pair, you can get a list of keys using array_keys().

$names = array('un' => 'a', 'pa' => 'v');

// Loop using foreach.
foreach($names as $k => $v)
{
    var_dump($v);
}

//Loop using while and list.
while(list($key, $val) = each($names))
{
    var_dump($val);
}

// Loop using while.
while(current($names) !== FALSE)
{
    var_dump(current($names));
    next($names);
}

Link to comment
https://forums.phpfreaks.com/topic/207505-array-help/#findComment-1084879
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.