最后在第二个数组插入数组索引

时间:2016-08-10 05:54:11

标签: php arrays wordpress

我有情况。我想将一个数组元素插入另一个数组元素但无法找到确切的方法。

这是我的功能

public function get_applicants($sort_array = array('apply_id DESC'))
{
    global $wpdb;
    $sql = 'SELECT * FROM ' . $this->tableac . " ORDER BY " . implode(',', $sort_array);
    //$sql = 'SELECT c. * , p. * FROM wp_apply c, wp_apply_files p WHERE c.apply_id = p.apply_id';
    $educations = $wpdb->get_results($sql);
    $getid=array();
    foreach($educations as $education){
        //print_r($education);
        $sqlfile = 'SELECT * FROM wp_apply_files WHERE apply_id = '.$education->apply_id;
        $getalls = $wpdb->get_results($sqlfile);
        $allvalues="";
            foreach($getalls as $getall){
                $allvalues= $getall->uploaded_file.", ";
                $getid[]=$getall->uploaded_file;
            }
           $allvaluesnew=rtrim($allvalues,", "); 
         // echo "<br>";
         // Here I want to insert getid into educations array
    }
    echo "<pre>";print_r($educations);
    die();
    //return array($educations,$getid);
}

print_r结果显示了这一点。

Array
(
[0] => stdClass Object
    (
        [apply_id] => 44
        [choose_position] => HR Manager
        [title] => testr
        [first_name] => waqas
        [last_name] => aamer
        [current_job] => developer

print_r get id显示如下。

Array
(
[0] => a75d138911c55df639fdd09fade511151-23.pdf
[1] => newone3.pdf
[2] => a75d138911c55df639fdd09fade511151-22.pdf
[3] => newone2.pdf
[4] => a75d138911c55df639fdd09fade511151 (2).pdf
[5] => newone.pdf
)

我希望迭代地插入这些元素。在教育中,第一次迭代到来时应该将所有元素插入第二个数组的一个索引,用逗号分隔。

1 个答案:

答案 0 :(得分:1)

如果我正确理解您的问题,那么以下内容可能有所帮助。 它会将来自$getid的逗号分隔值插入名为&#34; getid&#34;的新密钥中。在$educations数组中。

public function get_applicants($sort_array = array('apply_id DESC'))
{
    global $wpdb;
    $sql = 'SELECT * FROM ' . $this->tableac . " ORDER BY " . implode(',', $sort_array);
    //$sql = 'SELECT c. * , p. * FROM wp_apply c, wp_apply_files p WHERE c.apply_id = p.apply_id';
    $educations = $wpdb->get_results($sql);
    $getid=array();
    foreach($educations as $key => $education){
        //print_r($education);
        $sqlfile = 'SELECT * FROM wp_apply_files WHERE apply_id = '.$education->apply_id;
        $getalls = $wpdb->get_results($sqlfile);
            foreach($getalls as $getall){
                $getid[]=$getall->uploaded_file;
            }
         // echo "<br>";
         // Here I want to insert getid into educations array
         $educations[$key]["getid"] = implode(",", $getid);
    }
    echo "<pre>";print_r($educations);
    die();
    //return array($educations,$getid);
}

希望这会有所帮助,这就是你想要的

相关问题