将数组的第一个索引值作为键,将第二个索引的值作为新数组的值

时间:2016-11-29 06:08:37

标签: php codeigniter

以下代码:

 $data = $student_internal_mark->$internalmark_col;
        $d = explode("|",$data);
        foreach ($d as $value) {
              $register_no_arr = explode(":",$value);
              $marks = explode(',',$register_no_arr[1]);
              print_r($register_no_arr);
        }

我输出如下:

Array
        (
            [0] => SD00000001
            [1] => 9.8,3,2,3,4
        )
        Array
        (
            [0] => SD00000002
            [1] => 6.8,2.4,34,34,34
        )
        Array
        (
            [0] => SD00000003
            [1] => 4.6,2.4,23,23,23
        )
        Array
        (
            [0] => SD00000004
            [1] => 6.4,1.2,32,32,32
        )

我希望以这样一种方式检索值:我将得到(输出)一个数组:

student_marks[SD00000001]=9.8,3,2,3,4
student_marks[SD00000002]=6.8,2.4,34,34,34
student_marks[SD00000003]=4.6,2.4,23,23,23
.......
student_marks[SD0000000N]=................

这样我就可以在每个数组中显示标记,如下所示

注册号码|马克1 |马克2 |马克3 |总

S5001 | 3 | 2 | 3 | 8

S5002 | 4 | 4 | 6 | 14

S5003 | 1 | 3 | 5 | 9

5 个答案:

答案 0 :(得分:1)

你可以使用array_walk,demo here

   <?php
    $register_no_arr = array(
      array("SD00000001", "9.8,3,2,3,4"),
      array("SD00000002", "6.8,2.4,34,34,34"),
      array("SD00000003", "4.6,2.4,23,23,23"),
      array("SD00000004", "6.4,1.2,32,32,32"),
    );


    $o = [];
    array_walk($register_no_arr, function($v) use(&$o)
    {
      $o[$v[0]] = $v[1];
    });
    var_dump($o);

结果:

array(4) {
  ["SD00000001"]=>
  string(11) "9.8,3,2,3,4"
  ["SD00000002"]=>
  string(16) "6.8,2.4,34,34,34"
  ["SD00000003"]=>
  string(16) "4.6,2.4,23,23,23"
  ["SD00000004"]=>
  string(16) "6.4,1.2,32,32,32"
}

答案 1 :(得分:1)

只需使用foreach。试试这个:

$arr = array(
  array("SD00000001", "9.8,3,2,3,4"),
  array("SD00000002", "6.8,2.4,34,34,34"),
  array("SD00000003", "4.6,2.4,23,23,23"),
  array("SD00000004", "6.4,1.2,32,32,32"),
);

$student_marks = array();
foreach($arr as $values) {
    $student_marks[$values[0]] = $values[1];
}

echo '<pre>';
    print_r($student_marks);
echo '</pre>';

<强>更新

这是你给出的:

$data = 'SD00000001:9.8,3,2,3,4|SD00000002:6.8,2.4,34,34,34|SD000000‌​03:4.6,2.4,23,23,2‌​3|‌​SD00000004:6.4,1.2,3‌​2,32,32'; 
$student_marks = array();
$d = explode("|",$data);
foreach($d as $values) {
    $valuesArr = explode(":", $values);
    $student_marks[$valuesArr[0]] = $valuesArr[1];
}

echo '<pre>';
print_r($student_marks); 
echo '</pre>';

答案 2 :(得分:1)

这将为您提供预期的数组

$str = "S5001:31,32,33|S5002:42,44,46|S5003:21,23,25";
$d = explode("|",$str);

foreach ($d as $value) {
        $register_no_arr = explode(":",$value);
        $marks[$register_no_arr[0]] = $register_no_arr[1];

}

print_r($marks);

结果:

Array
(
    [S5001] => 31,32,33
    [S5002] => 42,44,46
    [S5003] => 21,23,25
)

答案 3 :(得分:0)

试试这段代码:

foreach ($d as $value) {
              $register_no_arr = explode(":",$value);
              $new_arr['student_marks']$register_no_arr[0] = $register_no_arr[1];
        }

print_r($new_arr);

答案 4 :(得分:0)

尝试这样的事情......

     $student_marks = array(
        '1' => array(
            '0' => 'SD00000001',
            '1' => '9.8,3,2,3,4',
        ),
        '2'=>array(

          '0' => 'SD00000002',
           '1' => '6.8,2.4,34,34,34',
        ),
        '3'=> array (
            '0' => 'SD00000003',
            '1' => '4.6,2.4,23,23,23',
        )
    );

    foreach ($student_marks as $key => $value) {
        $student_marks1[$value['0']] = $value['1'];
    }
    echo "<pre>";
    print_r($student_marks1);