使用PHP中的查找函数中的数组_search返回单个键

使用PHP中的查找函数中的数组_search返回单个键,php,arrays,Php,Arrays,当我运行下面的代码时,我得到了输出“agriculture”,但我想从搜索数据代码时出现的条目中返回单个数组键的所有条目。例如: 任务、使用的工具、知识 以及第一个条目,在本例中为“拣选和包装” 我该怎么做?多谢各位 我在本文中找到了原始代码 将链接答案搜索到二维数组中。您的阵列中有3个深度级别 $data code不是有效的PHP变量(不能包含破折号-) 您可以循环使用$indus值来搜索所需的值 代码 输出: 位于:农业->拣选和包装 工作演示:什么是$data code?PHP

当我运行下面的代码时,我得到了输出“agriculture”,但我想从搜索数据代码时出现的条目中返回单个数组键的所有条目。例如: 任务、使用的工具、知识

以及第一个条目,在本例中为“拣选和包装”

我该怎么做?多谢各位

我在本文中找到了原始代码

  • 将链接答案搜索到二维数组中。您的阵列中有3个深度级别

  • $data code
    不是有效的PHP变量(不能包含破折号
    -

  • 您可以循环使用
    $indus
    值来搜索所需的值

代码

输出:

位于:农业->拣选和包装

工作演示:

什么是
$data code
?PHP变量不能包含减号(
-
)。需要一个数组作为第二个参数(这里,
$profcode
是一个字符串)。谢谢。我正在把它改成数据代码。它能解决你的问题吗?
<?php

$indus['agriculture'] = [
    "Picking and Packing" => [
        "Tasks" => [
            "Examine and inspect containers, materials, or products to ensure that product quality and packing specifications are met.", "Measure, weigh, and count products and materials", "Record product, packaging, and order information on specified forms and records.", "Seal containers or materials, using glues, fasteners, nails, and hand tools.", "Assemble, line, and pad cartons, crates, and containers, using hand tools.",
        ],
        "Tools_used" => [
            "Box sealing tape dispensers", "Form or fill or seal machinery", "Staple guns", "Strapping dispenser", "Wrapping machinery",
        ],
        "Knowledge" => [
            "Production and Processing", "Customer and Personal Service",
        ],
        "Skills" => [
            "Entry1", "Monitoring",
        ],
        "Abilities" => [
            "Near Vision", "Problem Sensitivity ", "Trunk Strength", "Manual Dexterity", "Multilimb Coordination",
        ],
        "data-code" => [
          "53-7064.00"
        ]
    ],
    "Farm Worker" => [
        "Tasks" => [
            "Operate tractors, tractor-drawn machinery, and self-propelled machinery to plow, harrow and fertilize soil, or to plant, cultivate, spray and harvest crops.", "Operate tractors, tractor-drawn machinery, and self-propelled machinery to plow, harrow and fertilize soil, or to plant, cultivate, spray and harvest crops.", "Set up and operate irrigation equipment.","Repair and maintain farm vehicles, implements, and mechanical equipment.","Harvest fruits and vegetables by hand."
        ],
        "Tools_used" => [
            "Broadcast spreaders", "Greenhouse ventilation equipment", "Saws","Secateurs or pruning shears","Utility knives",
        ],
        "Knowledge" => [
            "Entry1", "Entry2",
        ],
        "Skills" => [
            "Entry1", "Entry2",
        ],
        "Abilities" => [
            "Multilimb Coordination", "Static Strength","Manual Dexterity","Trunk Strength","Arm-Hand Steadiness",
        ],
        "data-code" => [
          "45-2092.00",
        ]
    ],
    "Agricultural Equipment Operator" => [
        "Tasks" => [
            "Load and unload crops or containers of materials, manually or using conveyors, handtrucks, forklifts, or transfer augers.", "Mix specified materials or chemicals, and dump solutions, powders, or seeds into planter or sprayer machinery.","Spray fertilizer or pesticide solutions to control insects, fungus and weed growth, and diseases, using hand sprayers.","Observe and listen to machinery operation to detect equipment malfunctions.","Manipulate controls to set, activate, and adjust mechanisms on machinery.",
        ],
        "Tools_used" => [
            "Container trailers", "Fertilizer spreaders or distributors","Harvesters","Haymaking machinery","Sprayers",
        ],
        "Knowledge" => [
            "Entry1", "Entry2",
        ],
        "Skills" => [
            "Operation and Control", "Operation Monitoring","Troubleshooting","Active Listening","Equipment Maintenance"
        ],
        "Abilities" => [
            "Control Precision", "Multilimb Coordination","Near Vision","Depth Perception","Far Vision",
        ],
        "data-code" => [
          "45-2091.00",
        ]    
    ],
];

$indus['construction'] = [
    "Construction Labourer" => [
        "Tasks" => [
            "Tend pumps, compressors, or generators to provide power for tools, machinery, or equipment or to heat or move materials, such as asphalt", "Lubricate, clean, or repair machinery, equipment, or tools.", "Signal equipment operators to facilitate alignment, movement, or adjustment of machinery, equipment, or materials.", "Read plans, instructions, or specifications to determine work activities.", "Measure, mark, or record openings or distances to layout areas where construction work will be performed.",
        ],
        "Tools_used" => [
            "Levels", "Manlift or personnel lift", "Power saws", "Vacuum cleaners", "Water trucks",
        ],
        "Knowledge" => [
            "Building and Construction", "Public Safety and Security","Mechanical",
        ],
        "Skills" => [
            "Speaking", "Active Listening","Coordination","Operation and Control","Operation Monitoring",
        ],
        "Abilities" => [
            "Entry1", "Entry2", "Entry3", "Entry4", "Entry5",
        ],
        "data-code" => [
          "47-2061.00",
        ]
    ],
    "Operating Engineers and Other Construction Equipment Operators" => [
        "Tasks" => [
            "Learn and follow safety regulations.", "Take actions to avoid potential hazards or obstructions, such as utility lines, other equipment, other workers, or falling objects.", "Start engines, move throttles, switches, or levers, or depress pedals to operate machines, such as bulldozers, trench excavators, road graders, or backhoes.","Coordinate machine actions with other activities, positioning or moving loads in response to hand or audio signals from crew members.","Align machines, cutterheads, or depth gauge makers with reference stakes and guidelines or ground or position equipment, following hand signals of other workers.",
        ],
        "Tools_used" => [
            "Dump trucks", "Hydraulic truck cranes", "Power saws","Scrubbing machines","Water trucks",
        ],
        "Knowledge" => [
            "Mechanical", "Public Safety and Security",
        ],
        "Skills" => [
            "Operation and Control", "Equipment Maintenance","Operation Monitoring","Monitoring","Active Listening",
        ],
        "Abilities" => [
            "Control Precision", "Depth Perception","Multilimb Coordination","Near Vision","Far Vision",
        ],
        "data-code" => [
          "47-2073.00"
        ]
    ],
    "Cement Masons and Concrete Finishers" => [
        "Tasks" => [
            "Check the forms that hold the concrete to see that they are properly constructed.", "Set the forms that hold concrete to the desired pitch and depth, and align them.","Spread, level, and smooth concrete, using rake, shovel, hand or power trowel, hand or power screed, and float.","Monitor how the wind, heat, or cold affect the curing of the concrete throughout the entire process.","Mold expansion joints and edges, using edging tools, jointers, and straightedge",
        ],
        "Tools_used" => [
            "Batching plants or feeders", "Concrete spreaders","Floats","Power saws","Trowels",
        ],
        "Knowledge" => [
            "Building and Construction ", "Mathematics","Public Safety and Security",
        ],
        "Skills" => [
            "Monitoring", "Coordination","Quality Control Analysis","Speaking","Time Management",
        ],
        "Abilities" => [
            "Manual Dexterity", "Trunk Strength","Multilimb Coordination","Near Vision","Arm-Hand Steadiness",
        ],
        "data-code" => [
          "47-2051.00"
        ]    
    ],
    "Brickmasons and Blockmasons" => [
        "Tasks" => [
            "Measure distance from reference points and mark guidelines to lay out work, using plumb bobs and levels.", "Construct corners by fastening in plumb position a corner pole or building a corner pyramid of bricks, and filling in between the corners using a line from corner to corner to guide each course, or layer, of brick.", "Apply and smooth mortar or other mixture over work surface.",
        ],
        "Tools_used" => [
            "Cold chisels", "Hammers", "Levels",
        ],
        "Knowledge" => [
            "Building and Construction", "Mathematics","Public Safety and Security","Design",
        ],
        "Skills" => [
            "Coordination", "Critical Thinking","Active Listening","Monitoring","Operation Monitoring",
        ],
        "Abilities" => [
            "Trunk Strength", "Extent Flexibility","Arm-Hand Steadiness","Manual Dexterity ","Near Vision",
        ],
        "data-code" => [
          "47-2021.00"
        ]
    ],
    "Fence Erector" => [
        "Tasks" => [
            "Establish the location for a fence, and gather information needed to ensure that there are no electric cables or water lines in the area.", "Set metal or wooden posts in upright positions in postholes.", "Measure and lay out fence lines and mark posthole positions, following instructions, drawings, or specifications.","Align posts, using lines or by sighting, and verify vertical alignment of posts, using plumb bobs or spirit levels.","Attach rails or tension wire along bottoms of posts to form fencing frames.",
        ],
        "Tools_used" => [
            "Pipe or tube cutter", "Pullers", "Staple guns","Tape measures","Wire cutters",
        ],
        "Knowledge" => [
            "Customer and Personal Service", "Building and Construction","Transportation","Administration and Management","Design",
        ],
        "Skills" => [
            "Active Listening", "Coordination","Critical Thinking","Monitoring","Speaking",
        ],
        "Abilities" => [
            "Manual Dexterity", "Trunk Strength","Multilimb Coordination","Static Strength","Arm-Hand Steadiness",
        ],
        "data-code" => [
          "47-4031.00"
        ]
    ],
    "Surveying Technician" => [
        "Tasks" => [
            "Entry1", "Entry2", "Entry3",
        ],
        "Tools_used" => [
            "Entry1", "Entry2", "Entry3",
        ],
        "Knowledge" => [
            "Entry1", "Entry2",
        ],
        "Skills" => [
            "Entry1", "Entry2",
        ],
        "Abilities" => [
            "Entry1", "Entry2",
        ],
        "data-code" => [
          "17-3031.01"
        ]
    ],
    
];



$profcode ='53-7064.00';

function find($profcode, $indus) {
    foreach($indus as $key=>$value){
       if(is_array($value) && array_search($data-code, $profcode) !== false) {
          return $key;
       }
    }
    return false;
}

$cat = find($profcode, $indus);

echo $cat;
$profcode ='53-7064.00';

function find($profcode, $indus) {
    foreach($indus as $key => $value) {
        if (in_array($profcode, $value['data-code'])) {
            return $key;
        }
    }
    return false;
}

foreach($indus as $key => $values) {
    $cat = find($profcode, $values);
    if ($cat) {
        echo "Found in : $key -> $cat";
    }
}