I have array values . i need to use foreach loop and i want to 2 array only how can i use this. Below is array data's. I want to display 2nd array only.How can i display 2nd array in foreach loop, i want to display only for the [2] => stdClass Object data only please look in to some please help..
I want to use foreach loop here only.
[2] => stdClass Object
(
[other] => Array
(
[0] => stdClass Object
(
[questionText] => Bangala
[questionVal] => 2021-10-15
[questionId] => other_0
)
[1] => stdClass Object
(
[questionText] => Native
[questionVal] => 2021-10-16
[questionId] => other_1
)
)
Php array code:
Array
(
[0] => stdClass Object
(
[default] => stdClass Object
(
[passport_renewal] => 2021-10-14
[visa_renewal] => 2021-10-14
[other_renewal1] => 2021-10-14
[other_renewal2] => 2021-10-14
[custom_question] => MOTIVE
[reminder_date] =>
)
)
[1] => stdClass Object
(
[questions] => stdClass Object
(
[fitness_certificate] =>
[pollution_certificate] =>
[amc_renewal] =>
[license_renewal] =>
)
)
[2] => stdClass Object
(
[other] => Array
(
[0] => stdClass Object
(
[questionText] => Bangala
[questionVal] => 2021-10-15
[questionId] => other_0
)
[1] => stdClass Object
(
[questionText] => Native
[questionVal] => 2021-10-16
[questionId] => other_1
)
)
)
)
CodePudding user response:
Inside loop check the key value, if the key value is equal to 2, then push that items to new array and display that array on next line.
$arr=[];
foreach($rows as $key=>$value){
if($key==2){
array_push($arr,$value);
}
}
print_r($arr);
CodePudding user response:
If you only want to process the 3rd occurance i.e. array[2] then only look at that occurance. This does assume you know in advance you only need to look at that occurance alone
foreach( $yourArray[2] as $obj) {
echo ......
}
Looking closer at your [2] occurance, it contains an object with a property called other
and all the info is in there, so maybe it would be simpler to do
foreach( $yourArray[2]->other as $obj ) {
echo $obj->questionText . '<br>';
echo $obj->questionVal . '<br>';
echo $obj->questionId . '<br>';
}