简体   繁体   English

从多维数组PHP中获取每个元素

[英]Get elements from multidimensional array PHP in for each

Using below code I am trying to get the array for each key each in side for loop, but at last the array result is wrong. 使用下面的代码,我试图为循环中的每个键获取数组,但是最后数组结果是错误的。 The DataIds returned is merged from all the arrays, I want to get the elements from single array. 返回的DataIds从所有数组中合并,我想从单个数组中获取元素。 Not sure how to achieve using above code in PHP? 不确定如何在PHP中使用上述代码来实现吗? Need help? 需要帮忙? In this for each loop I want the ids to be attached to that particular key. 在每个循环中,我希望将ID附加到该特定键。

$student = [ABC, DEF];

public function getData()
{


     $newData = $newDataId = $newDataStudIds = $DataIds = [];
        foreach ($student as $key => $value) {

            $Ids   = [69, 70 ,71];

            //for above Ids foreach is executed
            foreach ($Ids as $Id) {

                $data = $this->api->request('marks', [
                    'Id'          => $Id,
                    'teacherId'   => 1
                ]);

                if ($data != 'error') {

                    $body = json_decode($data->getBody(), true);

                    foreach ($body['student'] as $student) {
                        $newData[$body['profile']['stud_id']][]   = $student['stud_id'];
                        /* so $newData will contain
                        array(69 => array('0'=>1,'1'=>"2"),
                             70 => array('0'=>2,'1'=>"3"),
                             71 => array('0'=>3,'1'=>"1")
                        )*/
                    }
                }
            }

            $newDataId = array_keys($newData);
            $newDataStudIds = array_values($newData);

            $DataIds = $this->implodeAllArray(',', $newDataStudIds);
            $DataIds = implode(',',array_unique(explode(',', $DataIds)));
            /*
                i want $DataIds = [69] => [1 ,2]
                [70] => [2, 3]
                [71] => [3, 1]

            But this $DataIds return array like 
            [69] => [1, 2 ,2 ,3]
            [70]  => [1, 2 ,2 ,3]
            [71]  => [1, 2 ,2 ,3]
            */

        }

}

public function implodeAllArray($glue, $arr){           
    for ($i=0; $i<count($arr); $i++) {
        if (@is_array($arr[$i])) 
            $arr[$i] = $this->implodeAllArray($glue, $arr[$i]);
    }            
    return implode($glue, $arr);
}

i want $DataIds = 

    [69] => [1 ,2]
    [70] => [2, 3]
    [71] => [3, 1]

 But this $DataIds return array like 
    [69] => [1, 2 ,2 ,3]
    [70]  => [1, 2 ,2 ,3]
    [71]  => [1, 2 ,2 ,3]

Based on my interpretation of your question, you can convert: 根据我对您问题的解释,您可以进行以下转换:

$newData=array(69 => array('0'=>1,'1'=>"2"),
               70 => array('0'=>2,'1'=>"3"),
               71 => array('0'=>3,'1'=>"1"));

To ( $DataIds ): 到( $DataIds ):

array(69 => array(1,2),
      70 => array(2,3),
      71 => array(3,1),
)

Using: 使用方法:

$DataIds=array_map(function($v){return [$v[0],(int)$v[1]];},$newData);
var_export($DataIds);

I am effectively converting all strings (keys and values) to integers. 我有效地将所有字符串(键和值)转换为整数。 Demo 演示版

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM