在嵌套数组中查找并打印相应的名称

时间:2015-11-25 21:48:02

标签: php arrays mysqli

我想在提供did时打印人名。

我用它来获取数组:

$getdepts_another=mysqli_query($connection,
    "select did,name,photo,dabout,fname,lname 
     from dept 
     where (acid='1' and name !='Admin')"); 
$deparray=array(); 
while($res = mysqli_fetch_row($getdepts_another))
{ 
    array_push($deparray, $res); 
} 
$_SESSION['deps_array']=$deparray;

这是我的阵列:

array(9) {
    [0]=> array(6) {
        [0]=> string(1) "2"
        [1]=> string(8) "Accounts"
        [2]=> string(29) "1851530771msstore_400x400.jpg"
        [3]=> string(0) ""
        [4]=> string(0) ""
        [5]=> string(0) ""
    }
    [1]=> array(6) {
        [0]=> string(1) "3"
        [1]=> string(14) "Human Resource"
        [2]=> string(9) "photo.png"
        [3]=> string(0) ""
        [4]=> string(0) ""
        [5]=> string(0) ""
    }
    [2]=> array(6) {
        [0]=> string(1) "4"
        [1]=> string(9) "Documents"
        [2]=> string(9) "photo.png"
        [3]=> string(0) ""
        [4]=> string(0) ""
        [5]=> string(0) ""
    }
    [3]=> array(6) {
        [0]=> string(1) "5"
        [1]=> string(5) "Sales"
        [2]=> string(9) "photo.png"
        [3]=> string(0) ""
        [4]=> string(0) ""
        [5]=> string(0) ""
    }
    [4]=> array(6) {
        [0]=> string(1) "6"
        [1]=> string(7) "General"
        [2]=> string(9) "photo.png"
        [3]=> string(0) ""
        [4]=> string(0) ""
        [5]=> string(0) ""
    }
    [5]=> array(6) {
        [0]=> string(1) "7"
        [1]=> string(10) "Marketting"
        [2]=> string(9) "photo.png"
        [3]=> string(0) ""
        [4]=> string(0) ""
        [5]=> string(0) ""
     }

这是我搜索和打印did

名称的功能
function search($ids, $array) {

    $idArray = explode(",", $ids);
    $result = Array();

    foreach($idArray as $id)
    {

        foreach ($array as $key => $val) {
            if ($val['did'] === $id) {
                echo "name:".$val['fame'];
                array_push($result, $val['fame']);
            }
        }

    }

    return $result;

}//end function

$res = search('3,6', $array); 

如果我使用search(1,2);,则应打印Accounts,Human Resource

1 个答案:

答案 0 :(得分:1)

首先,尝试使用mysql_fetch_assoc代替mysqli_fetch_row。这可能就是数组中没有关键字来搜索的原因。

$getdepts_another=mysqli_query($connection, 
    "
    SELECT did,name,photo,dabout,fname,lname 
    FROM dept 
    WHERE (acid='1' and name !='Admin')
    "
); 
$deparray = array(); 
while($res = mysqli_fetch_assoc($getdepts_another))
{ 
    array_push($deparray, $res); 
} 
$_SESSION['deps_array']=$deparray;

其次,在您的搜索功能中,您正在退回键fame而不是fname

function search($ids, $array) {

    $idArray = explode(",", $ids);
    $result = Array();

    foreach($idArray as $id)
    {

        foreach ($array as $key => $val) {
            if ($val['did'] === $id) {
                echo "name:".$val['fname'];
                array_push($result, $val['fname']);
            }
        }

    }

    return $result;

}//end function

$res = search('3,6', $array); 

希望这有帮助。