Get key from array with multiple values
So i need to return the number (6, 8 or 10) with the country value.
So in the example, with 'sweden' its supposed to return 8 but the key of the array is apparently just Array(). Is the wrong in the structure of my array or the usage of array_keys
?
$list= array (
'list' =>
array (
6 =>
array (
'default',
'finland'
),
8 =>
array (
'sweden',
'norway'
),
10 =>
array (
'germany',
'belgia'
),
),
);
print_r(array_keys($list, "sweden"));
return: Array()
php arrays key
add a comment |
So i need to return the number (6, 8 or 10) with the country value.
So in the example, with 'sweden' its supposed to return 8 but the key of the array is apparently just Array(). Is the wrong in the structure of my array or the usage of array_keys
?
$list= array (
'list' =>
array (
6 =>
array (
'default',
'finland'
),
8 =>
array (
'sweden',
'norway'
),
10 =>
array (
'germany',
'belgia'
),
),
);
print_r(array_keys($list, "sweden"));
return: Array()
php arrays key
Possible duplicate of PHP Multidimensional Array Searching (Find key by specific value)
– René Höhle
Jan 1 at 1:26
doesnt look to be
– justaguytrynabuildawebsite
Jan 1 at 1:28
What? use the accepted Answer that is working ;)arrray_keys
isn't working with multidimensional arrays very well.
– René Höhle
Jan 1 at 1:29
in that question its an associative array and is looking for 'slug' with slugs value, this is different
– justaguytrynabuildawebsite
Jan 1 at 1:32
add a comment |
So i need to return the number (6, 8 or 10) with the country value.
So in the example, with 'sweden' its supposed to return 8 but the key of the array is apparently just Array(). Is the wrong in the structure of my array or the usage of array_keys
?
$list= array (
'list' =>
array (
6 =>
array (
'default',
'finland'
),
8 =>
array (
'sweden',
'norway'
),
10 =>
array (
'germany',
'belgia'
),
),
);
print_r(array_keys($list, "sweden"));
return: Array()
php arrays key
So i need to return the number (6, 8 or 10) with the country value.
So in the example, with 'sweden' its supposed to return 8 but the key of the array is apparently just Array(). Is the wrong in the structure of my array or the usage of array_keys
?
$list= array (
'list' =>
array (
6 =>
array (
'default',
'finland'
),
8 =>
array (
'sweden',
'norway'
),
10 =>
array (
'germany',
'belgia'
),
),
);
print_r(array_keys($list, "sweden"));
return: Array()
php arrays key
php arrays key
edited Jan 1 at 1:26
justaguytrynabuildawebsite
asked Jan 1 at 1:12
justaguytrynabuildawebsitejustaguytrynabuildawebsite
106
106
Possible duplicate of PHP Multidimensional Array Searching (Find key by specific value)
– René Höhle
Jan 1 at 1:26
doesnt look to be
– justaguytrynabuildawebsite
Jan 1 at 1:28
What? use the accepted Answer that is working ;)arrray_keys
isn't working with multidimensional arrays very well.
– René Höhle
Jan 1 at 1:29
in that question its an associative array and is looking for 'slug' with slugs value, this is different
– justaguytrynabuildawebsite
Jan 1 at 1:32
add a comment |
Possible duplicate of PHP Multidimensional Array Searching (Find key by specific value)
– René Höhle
Jan 1 at 1:26
doesnt look to be
– justaguytrynabuildawebsite
Jan 1 at 1:28
What? use the accepted Answer that is working ;)arrray_keys
isn't working with multidimensional arrays very well.
– René Höhle
Jan 1 at 1:29
in that question its an associative array and is looking for 'slug' with slugs value, this is different
– justaguytrynabuildawebsite
Jan 1 at 1:32
Possible duplicate of PHP Multidimensional Array Searching (Find key by specific value)
– René Höhle
Jan 1 at 1:26
Possible duplicate of PHP Multidimensional Array Searching (Find key by specific value)
– René Höhle
Jan 1 at 1:26
doesnt look to be
– justaguytrynabuildawebsite
Jan 1 at 1:28
doesnt look to be
– justaguytrynabuildawebsite
Jan 1 at 1:28
What? use the accepted Answer that is working ;)
arrray_keys
isn't working with multidimensional arrays very well.– René Höhle
Jan 1 at 1:29
What? use the accepted Answer that is working ;)
arrray_keys
isn't working with multidimensional arrays very well.– René Höhle
Jan 1 at 1:29
in that question its an associative array and is looking for 'slug' with slugs value, this is different
– justaguytrynabuildawebsite
Jan 1 at 1:32
in that question its an associative array and is looking for 'slug' with slugs value, this is different
– justaguytrynabuildawebsite
Jan 1 at 1:32
add a comment |
3 Answers
3
active
oldest
votes
You have two problems.
First, the array you want to search is $list['list']
, not $list
itself.
Second, the second argument to array_keys()
is only useful for 1-dimensional arrays. You have a 2-dimensional array, but array_keys()
will not automatically search inside the nested arrays. So you need to write your own loop or use array_filter()
.
$results = array();
foreach ($list['list'] as $key => $value) {
if (array_search('sweden', $value) !== false) {
$results = $key;
}
}
print_r($results);
im getting a Parse error: syntax error, unexpected 'as' , do i need to edit something?
– justaguytrynabuildawebsite
Jan 1 at 1:38
1
Should beforeach
, notfor
– Nick
Jan 1 at 1:39
add a comment |
use foreach for it.
foreach ($list as $key => $value){
}
add a comment |
I think this is what you want
foreach($list as $key => $value ){
$arr = array_keys($value);//this has your (6, 8 or 10)
foreach($arr as $val){
print_r($value[$val]);//showing array data of 6,8,10 indexes
}
}
output:
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53992446%2fget-key-from-array-with-multiple-values%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have two problems.
First, the array you want to search is $list['list']
, not $list
itself.
Second, the second argument to array_keys()
is only useful for 1-dimensional arrays. You have a 2-dimensional array, but array_keys()
will not automatically search inside the nested arrays. So you need to write your own loop or use array_filter()
.
$results = array();
foreach ($list['list'] as $key => $value) {
if (array_search('sweden', $value) !== false) {
$results = $key;
}
}
print_r($results);
im getting a Parse error: syntax error, unexpected 'as' , do i need to edit something?
– justaguytrynabuildawebsite
Jan 1 at 1:38
1
Should beforeach
, notfor
– Nick
Jan 1 at 1:39
add a comment |
You have two problems.
First, the array you want to search is $list['list']
, not $list
itself.
Second, the second argument to array_keys()
is only useful for 1-dimensional arrays. You have a 2-dimensional array, but array_keys()
will not automatically search inside the nested arrays. So you need to write your own loop or use array_filter()
.
$results = array();
foreach ($list['list'] as $key => $value) {
if (array_search('sweden', $value) !== false) {
$results = $key;
}
}
print_r($results);
im getting a Parse error: syntax error, unexpected 'as' , do i need to edit something?
– justaguytrynabuildawebsite
Jan 1 at 1:38
1
Should beforeach
, notfor
– Nick
Jan 1 at 1:39
add a comment |
You have two problems.
First, the array you want to search is $list['list']
, not $list
itself.
Second, the second argument to array_keys()
is only useful for 1-dimensional arrays. You have a 2-dimensional array, but array_keys()
will not automatically search inside the nested arrays. So you need to write your own loop or use array_filter()
.
$results = array();
foreach ($list['list'] as $key => $value) {
if (array_search('sweden', $value) !== false) {
$results = $key;
}
}
print_r($results);
You have two problems.
First, the array you want to search is $list['list']
, not $list
itself.
Second, the second argument to array_keys()
is only useful for 1-dimensional arrays. You have a 2-dimensional array, but array_keys()
will not automatically search inside the nested arrays. So you need to write your own loop or use array_filter()
.
$results = array();
foreach ($list['list'] as $key => $value) {
if (array_search('sweden', $value) !== false) {
$results = $key;
}
}
print_r($results);
edited Jan 1 at 1:41
answered Jan 1 at 1:34
BarmarBarmar
429k36253353
429k36253353
im getting a Parse error: syntax error, unexpected 'as' , do i need to edit something?
– justaguytrynabuildawebsite
Jan 1 at 1:38
1
Should beforeach
, notfor
– Nick
Jan 1 at 1:39
add a comment |
im getting a Parse error: syntax error, unexpected 'as' , do i need to edit something?
– justaguytrynabuildawebsite
Jan 1 at 1:38
1
Should beforeach
, notfor
– Nick
Jan 1 at 1:39
im getting a Parse error: syntax error, unexpected 'as' , do i need to edit something?
– justaguytrynabuildawebsite
Jan 1 at 1:38
im getting a Parse error: syntax error, unexpected 'as' , do i need to edit something?
– justaguytrynabuildawebsite
Jan 1 at 1:38
1
1
Should be
foreach
, not for
– Nick
Jan 1 at 1:39
Should be
foreach
, not for
– Nick
Jan 1 at 1:39
add a comment |
use foreach for it.
foreach ($list as $key => $value){
}
add a comment |
use foreach for it.
foreach ($list as $key => $value){
}
add a comment |
use foreach for it.
foreach ($list as $key => $value){
}
use foreach for it.
foreach ($list as $key => $value){
}
answered Jan 1 at 5:20
PHP GeekPHP Geek
1,3861718
1,3861718
add a comment |
add a comment |
I think this is what you want
foreach($list as $key => $value ){
$arr = array_keys($value);//this has your (6, 8 or 10)
foreach($arr as $val){
print_r($value[$val]);//showing array data of 6,8,10 indexes
}
}
output:
add a comment |
I think this is what you want
foreach($list as $key => $value ){
$arr = array_keys($value);//this has your (6, 8 or 10)
foreach($arr as $val){
print_r($value[$val]);//showing array data of 6,8,10 indexes
}
}
output:
add a comment |
I think this is what you want
foreach($list as $key => $value ){
$arr = array_keys($value);//this has your (6, 8 or 10)
foreach($arr as $val){
print_r($value[$val]);//showing array data of 6,8,10 indexes
}
}
output:
I think this is what you want
foreach($list as $key => $value ){
$arr = array_keys($value);//this has your (6, 8 or 10)
foreach($arr as $val){
print_r($value[$val]);//showing array data of 6,8,10 indexes
}
}
output:
answered Jan 1 at 6:21
KhageshKhagesh
1138
1138
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53992446%2fget-key-from-array-with-multiple-values%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Possible duplicate of PHP Multidimensional Array Searching (Find key by specific value)
– René Höhle
Jan 1 at 1:26
doesnt look to be
– justaguytrynabuildawebsite
Jan 1 at 1:28
What? use the accepted Answer that is working ;)
arrray_keys
isn't working with multidimensional arrays very well.– René Höhle
Jan 1 at 1:29
in that question its an associative array and is looking for 'slug' with slugs value, this is different
– justaguytrynabuildawebsite
Jan 1 at 1:32