1

I have an array, what Is like this:

 $itemx=
 [
 'Weapons'=>[
 'Sword'=> [
 'ID' => '1',
 'Name' => 'Lurker',
 'Value' => '12',
 'Made' => 'Acient'
 ],
 'Shield'=> [
 'ID' => '2',
 'Name' => 'Obi',
 'Value' => '22',
 'Made' => 'Acient'
 ],
 'Warhammer'=> [
 'ID' => '3',
 'Name' => 'Clotch',
 'Value' => '124',
 'Made' => 'Acient'
 ]
 ],
 'Drinks'=>[
 'Water'=> [
 'ID' => '4',
 'Name' => 'Clean-water',
 'Value' => '1',
 'Made' => 'Acient'
 ],
 'Wine'=> [
 'ID' => '5',
 'Name' => 'Soff',
 'Value' => '5',
 'Made' => 'Acient'
 ],
 'Vodka'=> [
 'ID' => '6',
 'Name' => 'Laudur',
 'Value' => '7',
 'Made' => 'Acient'
 ]
 ]
 ];

I want to find an array from it by Name or ID, so my output should be like this.

*Search by ID=4*
 'Water'=> [
 'ID' => '4',
 'Name' => 'Clean-water',
 'Value' => '1',
 'Made' => 'Acient'
 ]

I look at other topics and found that I should use array_search

But It didn't work. I tried like this:

$arra=$itemx["Drinks"];
$key = array_search(4, array_column($arra, 'ID'));
 var_dump($arra[$key]);

It also dident work when I tried with Name as a search key.

How can I get this working?

mickmackusa
49.1k13 gold badges97 silver badges163 bronze badges
asked Aug 29, 2017 at 21:09
1

3 Answers 3

2

You can do it like below:-

$search_id = 4;
$final_array = [];
 foreach($itemx as $key=>$val){
 foreach($val as $k=>$v){
 if($v['ID'] == $search_id){
 $final_array[$k] = $itemx[$key][$k];
 }
 }
 }
 print_r($final_array);

https://eval.in/852123

answered Aug 29, 2017 at 21:18
Sign up to request clarification or add additional context in comments.

Comments

2

This should probably get you what you want.

function rec($itemx,$search=4){
 foreach ($itemx as $key => $value) {
 if (is_array($value)) {
 foreach ($value as $k => $v) {
 if ($v['ID'] == $search) {
 return $value; 
 }
 }
 }
 }
}
print_r(rec($itemx,4));
answered Aug 29, 2017 at 21:24

Comments

0

Your coding attempt suggests that the item category is known in advance by the developer.

From PHP8.4, array_find_key() returns the key of a qualifying value and stops iterating for best performance. Demo

$cat = 'Drinks';
$id = 4;
$key = array_find_key(
 $itemx[$cat],
 fn($row) => $row['ID'] == $id
);
if ($keyword !== null) {
 var_export([$key => $itemx[$cat][$key]]);
}

Output:

array (
 'Water' => 
 array (
 'ID' => '4',
 'Name' => 'Clean-water',
 'Value' => '1',
 'Made' => 'Acient',
 ),
)

If you don't need the 2nd level key, use array_find(). If there is no match, null is returned. Demo

$cat = 'Drinks';
$id = 4;
var_export(
 array_find(
 $itemx[$cat],
 fn($row) => $row['ID'] == $id
 )
);

Output:

array (
 'ID' => '4',
 'Name' => 'Clean-water',
 'Value' => '1',
 'Made' => 'Acient',
)

Although it becomes uglier than nested foreach loops, it is possible to nest array_find_key() calls when you don't know either of the first two level keys. Demo

$sub = null;
$id = 4;
$cat = array_find_key(
 $itemx,
 function ($set) use ($id, &$sub) {
 return ($sub = array_find_key(
 $set,
 fn($row) => $row['ID'] == $id
 )) !== null;
 }
);
var_export([$sub => $itemx[$cat][$sub]]);

If you are going to use classic nested loops and only one qualifying data set is expected, break both loops as soon as a match is found. Demo

$id = 4;
foreach ($itemx as $cat => $set) {
 foreach ($set as $sub => $row ) {
 if ($row['ID'] == $id) {
 var_export([$sub => $row]);
 break 2;
 }
 }
}
answered Nov 21, 2024 at 20:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.