如何设置数组键以使其在多维数组中开始为零?
How to set array keys to make it start to zero inside a multidimensional array?
我有一个多维数组,我想在 for 循环中打印它。第一个数组维度,例如 $arrayName[0]
的数组大小为 2,键从 0 => value1
开始,然后是 1 => value2
。然后 $arrayName[1]
也使用数组大小 2,但键从第一个数组维度开始顺序,看起来像这样 2 => value3
和 3 => value4
。而不是 2 和 3 键,有没有一种方法可以使它成为 0 和 1,因为它是另一个第二个数组维度?请帮我解决一下这个。谢谢。请看下图。
我想要这样
for ($i=0; $i < count($qualified_applicants); $i++) {
for ($j=0; $j < count($appExp); $j++) {
if($qualified_applicants[$i]->id == $appExp[$j]->applicant_id){
$temp[$appExp[$j]->applicant_id][$j] = $appExp[$j]->work.', '.$appExp[$j]->company_name.' - '.date("F j, Y", strtotime($appExp[$j]->start_date)).' - '.date("F j, Y", strtotime($appExp[$j]->end_date));
}else{
}
}
# code...
}
以上是我创建多维数组的代码
@Eli try this one
<?php
$arr1 = array(
array("i m first value of 0th array", "i m Second value of 0th array"),
array(
2 => "I m first value of 1st array but my key started with 2 and i want to start it with 0",
3 => "I m Second value of 1st array but my key is 3 and i want to set it with 1")
);
echo "<pre>";
print_r($arr1); // array before
// above is you array i as understand
$newFormattedArray = array();
foreach($arr1 as $key => $value){
$newFormattedArray[$key] = array_values($value); // array_values() will set the order in asc, starts with 0
}
echo "<pre>";
print_r($newFormattedArray); // array after
?>
我有一个多维数组,我想在 for 循环中打印它。第一个数组维度,例如 $arrayName[0]
的数组大小为 2,键从 0 => value1
开始,然后是 1 => value2
。然后 $arrayName[1]
也使用数组大小 2,但键从第一个数组维度开始顺序,看起来像这样 2 => value3
和 3 => value4
。而不是 2 和 3 键,有没有一种方法可以使它成为 0 和 1,因为它是另一个第二个数组维度?请帮我解决一下这个。谢谢。请看下图。
我想要这样
for ($i=0; $i < count($qualified_applicants); $i++) {
for ($j=0; $j < count($appExp); $j++) {
if($qualified_applicants[$i]->id == $appExp[$j]->applicant_id){
$temp[$appExp[$j]->applicant_id][$j] = $appExp[$j]->work.', '.$appExp[$j]->company_name.' - '.date("F j, Y", strtotime($appExp[$j]->start_date)).' - '.date("F j, Y", strtotime($appExp[$j]->end_date));
}else{
}
}
# code...
}
以上是我创建多维数组的代码
@Eli try this one
<?php
$arr1 = array(
array("i m first value of 0th array", "i m Second value of 0th array"),
array(
2 => "I m first value of 1st array but my key started with 2 and i want to start it with 0",
3 => "I m Second value of 1st array but my key is 3 and i want to set it with 1")
);
echo "<pre>";
print_r($arr1); // array before
// above is you array i as understand
$newFormattedArray = array();
foreach($arr1 as $key => $value){
$newFormattedArray[$key] = array_values($value); // array_values() will set the order in asc, starts with 0
}
echo "<pre>";
print_r($newFormattedArray); // array after
?>