更改每个值的数组索引

时间:2014-08-08 10:12:58

标签: php arrays foreach

到目前为止,$ record阵列的var_dump()看起来像是:

array (size=1)
  25 => 
    array (size=1)
      0 => 
        object(stdClass)[51]
          public 'id' => 25
          public 'name' => info...
          public 'surname' => info...

我不想将该0索引名称更改为对象id(25)名称,但它只是在我当前的名称之上添加了一个维度。我就是这样做的:

foreach ($records as $value) {
    $records = array($value->id=>$records);
}

我希望我的数组看起来像这样:

array (size=1)
  25 => 
    object(stdClass)[51]
     public 'id' => 25
     public 'name' => info...
     public 'surname' => info...

1 个答案:

答案 0 :(得分:1)

更新密钥以使其等于ID:

$tmp= array();
foreach ($records as $value) {
    $tmp[$value->id] = $value;
}
$records = $tmp;