Getting the array index in a foreach loop [duplicate] - php

This question already has answers here:
How to find the foreach index?
(14 answers)
Closed 8 years ago.
So I may just be completely overcomplicating this for myself but I am trying to get the index of an array in a multidimensional array.
Hopefully showing it makes more sense.
Array
(
[1234] => Array
(
[Name] => Test
)
[5435] => Array
(
[Name] => Test
)
)
I have a large array with different numbers as the index and I need to do a foreach through them, but I need that index number. (1234,5435)
Is there any easy way of doing this?

Yes there is.
foreach ($arr as $key=>$val)
{
//do stuff
}
The $key is the key you need

Related

How to retrieve data from index of an array in php? [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 3 years ago.
It is my array:
[main] => Array (
[data] => Array
( [777] => Array (... ),
[888] => Array (....),
[999] => Array (....) ));
I usually use such syntax:
array['main']['data'][0]...
however here I don't know what exactly values are (777, 888, 999).
How could I receive this 777, 888, 999 and also theirs included data?
You can use foreach loop and you can access the values in each iteration like below :
<?php
foreach($array['main']['data'] as $item) {
print_r($item['name']);
// i assure there is some index as name in the subarray. Change the index value as per your data.
}
?>
Refer the document ion for further information PHP Foreach loop
Use foreach loop... Foreach loop. Will solve your problem

PHP Pull data out of an array [duplicate]

This question already has answers here:
How to get single value from this multi-dimensional PHP array [duplicate]
(6 answers)
Closed 5 years ago.
I know this should be a relatively simple thing but I have not been able to do it yet. I have look hi and low and every example I try it fails I am sure it is fairly simple
Here is my array. I need to get the value of name last and filenames
Any help would be most appreciated.
Thanks!!
Array
(
[formData] => Array
(
[name] => TEST
[last] => TEST1
[filenames] => Array
(
[0] => /ocdata/uploads/export-1511887767.csv
)
)
)
Really simple method to see your content:
foreach($array as $k => $v)
{
echo $k . $v . PHP_EOL; // see content of your array
}
Or use directly the values:
$array['formData']['name'];
$array['formData']['last'];
$array['formData']['filenames'];

PHP - Find key in multidimensional array [duplicate]

This question already has answers here:
PHP multidimensional array search by value
(23 answers)
Closed 3 years ago.
Is there any predefined PHP function to find a key in a multi dimensional array?
In the below example - there is a variable name 'rose' and I need to get the key of the array by using the variable name.
The result of the key is "flower".
$array = array (
'fruits' => array (
'mango',
'cherry'
),
'flowers' => array (
'rose'
)
);
How do I achieve this?
Loop it up using a foreach
$keyword='mango';
foreach($array as $k=>$arr)
{
if(in_array($keyword,$arr))
{
echo $k;break;// "prints" fruits
}
}
Working Demo

How to get the value of an array that's inside another array [duplicate]

This question already has answers here:
Get the first element of an array
(39 answers)
multidimensional array
(3 answers)
Closed 9 years ago.
I have this:
Array
(
[28] => Array
(
[name] => HTC Touch HD
)
)
There's only one array inside the main array and I only the value of name. Problem is that I don't know the index (28).
You could use array_values just in general to get rid of any weird keys:
$normal = array_values($arr);
$normal[0]['name']
Or in this particular case, end, which is only a little bit hacky:
end($normal)['name']
http://codepad.viper-7.com/cApBjK
(Yep, reset and first and such work too.)
You could also just use
$array = array_pop($array);
And then to get the name element:
$array['name']
You can try something like this:
reset($outerArray);
$innerArray = current($outerArray);
Now you should have access to the value you want.
Pretty self-explanatory :)
<?php
$array = array(
28 => array(
'name' => 'HTC Touch HD'
)
);
$key = current(array_keys($array));
echo '<pre>';
print_r($array[$key]);
echo '</pre>';
?>
If you don't know the structure of an array, you can use foreach construct.

Get the name of an index in an array [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get first key in a [possibly] associative array?
I have this array:
Array
(
['foobar'] => Array
(
[key1] => value1
[key2] => value2
)
)
I want to get to name of the first index (foobar). How can I do it?
Other way is,
$keys = array_keys($array);
echo $keys[0];
Assuming that you haven't used each(), next(), prev() or in any other manner have you advanced the array pointer:
key($array);

Categories