开发者

get value from multidimensional array and store into associative array

I want to store all employee's salary as an $salary Array by looping the following multiimentional array.

Can anyone help me? Thanks!

<?php   
$employees["employee 1"]["name"] = "Dana";
$employees["employee 1"]["ti开发者_如何学运维tle"] = "Owner";
$employees["employee 1"]["salary"] = "$60,000";

$employees["employee 2"]["name"] = "Matt";
$employees["employee 2"]["title"] = "Manager";
$employees["employee 2"]["salary"] = "$40,000";

$employees["employee 3"]["name"] = "Susan";
$employees["employee 3"]["title"] = "Cashier";
$employees["employee 3"]["salary"] = "$30,000";
?>


Following code may help you:

$salary = array();
foreach ($employees as $key => $value) {
  $salary[$key] = $value['salary'];
}


you can do this like below code.

foreach($employees as $key =>$values){
    $emp[$key]['name'] = $values['name'];
    $emp[$key]['sal'] = $values['salary'];
}

echo '<pre>';
print_r($emp);

Out put will be :

Array
(
    [employee 1] => Array
        (
            [name] => Dana
            [sal] => $60,000
        )

    [employee 2] => Array
        (
            [name] => Matt
            [sal] => $40,000
        )

    [employee 3] => Array
        (
            [name] => Susan
            [sal] => $30,000
        )

)

Thanks..


foreach ($employees as $employee) {
  foreach ($employee as $var => $val) {
    $$var[$employee] = $val;
  }
}

And then you should have three arrays, $name, $title, $salary.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜