Reputation: 45
I have array of following form
$a = array(
[0] =>
[address_country] =>
[1] => 2011-11-29 14:49:10
[createdtime] => 2011-11-29 14:49:10
)
I am parsing this array using a for loop
for($i = 0; $i < count($a); $i++){
}
Now I want retrieve key of 2nd element as I parse it
For if i = 0
, then i need some way of knowing that it's corresponding column name is address_country
Is there any way for this?
Please note I have to stick with for loop only
Upvotes: 0
Views: 37
Reputation: 4099
try this. I think this is what you want
$c = array_values($a);
for($i = 0; $i < count($c); $i++){
if(count($c)-1 > ($i)) {
if(array_search($c[$i+1],$a) == "address_country")
echo "found";
}
}
Upvotes: 0
Reputation: 37365
For example, use array_keys()
:
$a = array(
0 => '',
'address_country' => '',
'1' => '2011-11-29 14:49:10',
'createdtime' => '2011-11-29 14:49:10'
);
$keys = array_keys($a);
$i = 1;
$m = count($keys);
foreach($a as $key=>$value)
{
echo(sprintf('My key is [%s] and my next neighbor key is [%s]'.PHP_EOL, $key, $i<$m?$keys[$i++]:null));
}
this will result in
My key is [0] and my next neighbor key is [address_country] My key is [address_country] and my next neighbor key is [1] My key is [1] and my next neighbor key is [createdtime] My key is [createdtime] and my next neighbor key is []
note, that for last element next key will be treated as null
Upvotes: 1