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);
Related
This question already has answers here:
How can I add all of my array values together in PHP?
(4 answers)
Closed 3 years ago.
I have an array and I want to sum up the values of the array such as [0][1][2] and assign the result of the sum to a variable
My array
Array ( [0] => 1 [1] => 2 [2] => 0 )
Can anyone provide me a solution that would be really helpful?
Use php array_sum() function.
$val = array_sum($yourArray);
It will sum up all of your array elements into a variable.
This question already has answers here:
Reverse an associative array with preserving keys in PHP
(4 answers)
Closed 5 years ago.
I am developing a new website, and I have a quetion.
Input array:
Array ( [1319] => ####,[1316] => ###)
I have an array and I want to revese him, after the reverse the array would be like this:
Expected output:
Array ( [1316] => ###,[1319] => ####)
but when i'm using array_reverse function, it doesnt work for me, I got this array:
Array ( [0] => ###,[1] => ####)
Why it is happen?
For preserving keys you just pass second parameter to true in array_reverse.
Try this code snippet here
$array=Array ( 1319 => "####",1316 => "###");
print_r(array_reverse($array,true));
you can try this:
$a = []; //your array
$keys = array_keys($arr);
$values = array_values($arr);
$rv = array_reverse($values);
$newArray = array_combine($keys, $rv);
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
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
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.