在多维数组中如何设置数组键使其从零开始?

问题描述 投票:0回答:1

我有一个多维数组,我想在 for 循环中打印它。第一个数组维度 E.g

$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...
           }

上面是我创建多维数组的代码

php arrays multidimensional-array codeigniter-2
1个回答
1
投票
$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
© www.soinside.com 2019 - 2024. All rights reserved.