I'm trying to get each array generated from the following foreach loop so I can then insert them in another array:
function twprp_criteria_field() {
$data = '';
$criteria_string = get_post_meta( 50, 'twprp_site_criteria', true );
$criteria = explode( ',', $criteria_string );
foreach( $criteria as $key => $criterion ) {
$data[] = array(
'id' => sanitize_title_with_dashes( $criterion ),
'title' => $criterion,
'type' => 'slider',
'step' => 0.5,
'min' => 1,
'max' => 10,
'default' => '',
);
}
return $data;
}
This returns:
Array (
[0] => Array ( [id] => hello [title] => Hello [type] => slider [step] => 0.5 [min] => 1 [max] => 10 [default] => )
[1] => Array ( [id] => good-bye [title] => Good Bye [type] => slider [step] => 0.5 [min] => 1 [max] => 10 [default] => )
)
The problem is I need the arrays to be separate but currently they are part of one parent array. I know I can use twprp_criteria_field()[0] and twprp_criteria_field()[1] to get the individual arrays, but I'm returning an unknown number of arrays. I'm sure I'm missing something easy, but I just can't see it.
What you are missing and are looking for is a for
loop:
$arr = twprp_criteria_field();
for($i = 0; $i < count($arr); $i++){
echo 'Id:' . $arr[$i]['id'] . " ";
echo 'title:' . $arr[$i]['title'];
// ...
}