PHP - Find key in multidimensional array [duplicate] - php

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

Related

Add Key and Value to a multidimesnsion array PHP [duplicate]

This question already has answers here:
Add a static value to each row in a 2d array
(3 answers)
php - Add value with key to all the elements [duplicate]
(1 answer)
How can I push single element in existing array of JSON? [duplicate]
(5 answers)
Closed 11 days ago.
I have the following multidimenson array
array (
'count' => 386,
'report' =>
array (
'uuid' => '183a3956-9425-43da-845c-2839c30a951b',
'name' => 'OnlyScrumFND',
'Have' =>
array (
0 =>
array (
'uuid' => '00ad6013-4109-4940-a711-4f8fb5389e8c',
),
1 =>
array (
'uuid' => 'd651a86d-beac-498a-85a0-75ce62f28f4e',
),
),
),
)
I would like to add some info to the Array in a sublevel
foreach ($OUTPUT AS &$have['report']['Have']) {
$have['name'] = "something";
}
But it is not woking, Any hint? thanks rob
You are very close. you can try with following solution.
foreach ($OUTPUT['report']['Have'] as &$item) {
$item['name'] = "something";
}
I am using & operator here to modify the original array element in foreach loop instead of copied one.
Find the implementation below .
you can check here

How to get first key that is dynamic in multidimentional array in php? [duplicate]

This question already has answers here:
Get first key in a (possibly) associative array?
(25 answers)
Closed 2 years ago.
I have a multidimensional array and I want the first key that is
dynamic. How do I get that in PHP?
I want to display the customer name in frontend. But as you can see in the first
array there is the number [12345] => account number (the dynamic part). So if i
want to display customer name how can I get this with this dynamic
account number.
Array
(
[12345] => Array(
['customername'] => ABC
['customerid'] => 456
)
)
Try with Foreach
foreach($array as $value){
echo $value['customername'].'<br>';
echo $value['customerid'].'<br>';
}

Making array into multidimensional array with same key => value [duplicate]

This question already has answers here:
Create an assoc array with equal keys and values from a regular array
(3 answers)
Closed 4 years ago.
I have an array like this
$arr = ['Hello', 'World'];
What I am trying to achieve is
$arr [
'Hello' => 'Hello',
'World' => 'World'
]
Is there a array method to achieve this or should I run a foreach loop and do it manually? I am just thinking if there is a more elegant way
You could use array_combine for this
$new = array_combine($arr, $arr);
print_r($new);

How to convert a multidimensional array into a single line array in PHP? [duplicate]

This question already has answers here:
Convert multidimensional array into single array [duplicate]
(24 answers)
Restructure multidimensional array of column data into multidimensional array of row data [duplicate]
(3 answers)
Closed 5 years ago.
How can I convert a multidimensional array like the one below
Array(
[0] => Array(
[0]=001
[1]=002
[2]=003
)
[1] => Array(
[0]=America
[1]=Japan
[2]=South Korea
)
[2] => Array(
[0]=Washington DC
[1]=Tokyo
[2]=Seoul
)
)
into a single line array like the one below?
Array(
[0]=001,America,Washington DC
[1]=002,Japan,Tokyo
[2]=003,South Korea,Seoul
)
Here is simple code to work around,
foreach ($text as $key => $value) {
foreach ($value as $key1 => $value1) {
$result[$key1][] = $value1;
}
}
array_walk($result, function(&$item){
$item = implode(',', $item);
});
Here is the working link
array_walk — Apply a user supplied function to every member of an array
The variadiac php5.6+ version: (Offers the added benefits of not breaking on missing values and inserting null where values are missing.)
Code: (Demo)
var_export(array_map(function(){return implode(',',func_get_args());},...$text));
The non-variadic version:
Code: (Demo)
foreach($text as $i=>$v){
$result[]=implode(',',array_column($text,$i));
}
var_export($result);
Input:
$text = [
['001','002','003'],
['America','Japan','South Korea'],
['Washington DC','Tokyo','Seoul']
];
Output from either method:
array (
0 => '001,America,Washington DC',
1 => '002,Japan,Tokyo',
2 => '003,South Korea,Seoul',
)
Nearly exact duplicate page: Combining array inside multidimensional array with same key

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