Reputation: 7543
I have a huge multidimensional array, let's name it $big_array
.
In addition I have this set of data that I have to put into above array:
$input = array('one','two','three','four');
That's what I need to push into $big_array (based on $input from above):
$value = array(
'id' => $number,
'title' => 'foo',
'type' => 'options',
'options' => array('one','two','three','four'),
'page' => 'font_manager',
);
array_push($big_array, $value);
$big_array structure looks like this:
$big_array = array(
(...)
array(
'id' => 'Bar',
'title' => 'Foo',
'type' => 'select',
'page' => 'font_manager',
),
array(
'id' => 'ss_font',
'title' => __("Main font:",'solidstyle_admin'),
'type' => 'select',
'page' => 'font_manager',
'description' => __("This font will be used for all pages.",'solidstyle_admin'),
'default' => '',
'options' => array(
'option1' => 'option1',
'option2' => 'option12,
),
),
(...)
);
What I exactly want to do will look like that if it will be possible to include loops in arrays (yes, I know how wrong is that, just trying to explain it better):
$input = array('one','two','three','four');
$value = array(
'id' => $number,
'title' => 'foo',
'type' => 'options',
'options' => array(
foreach($input as $number) {
echo $number.',';
};
),
'page' => 'font_manager',
);
Upvotes: 1
Views: 7265
Reputation: 633
If i understand well. You wish to get this :
$input = array('one','two','three','four');
$value = array(
'id' => $number,
'title' => 'foo',
'type' => 'options',
'options' => array(
foreach($input as $number) {
echo $number.',';
};
),
'page' => 'font_manager',
);
And to push it into $big_array, am i right ?
Then the solution is pretty simple.
$input = array('one','two','three','four');
$value = array(
'id' => $number,
'title' => 'foo',
'type' => 'options',
'options' => array(
),
'page' => 'font_manager',
);
Build your array. Then your loop :
foreach($input as $number)
{
$value['options'][] = $number;
}
If you wan to add $input "as is", you may do
$value['options'] = $input;
If you want to have 'one' => 'one'
foreach($input as $number)
{
$value['options'][$number] = $number;
}
It will produce the exact same result than a loop, withouth the loop overhead.
And finally :
array_push($big_array, $value);
Upvotes: 1
Reputation: 1035
I think what you are going for is getting
$input = array('one','two','three','four');
inserted into
$value = array(
'id' => $number,
'title' => 'foo',
'type' => 'options',
'page' => 'font_manager',
);
so that it looks like this:
$value = array(
'id' => $number,
'title' => 'foo',
'type' => 'options',
'options' => array('one','two','three','four'),
'page' => 'font_manager',
);
and can then be pushed onto $bigArray . If that is the case, it should be as simple as
$input = array('one','two','three','four');
$value = array(
'id' => $number,
'title' => 'foo',
'type' => 'options',
'page' => 'font_manager',
);
$value['options'] = $input;
$bigArray[] = $value; // equivalent to array_push($bigArray, $value);
If I misunderstood your goal, please let me know.
edit: If you are going for something like this
$value = array(
'id' => $number,
'title' => 'foo',
'type' => 'options',
'options' => array('one,two,three,four'),
'page' => 'font_manager',
);
then it you would just change
$value['options'] = $input;
to
$value['options'] = implode(",",$input);
Upvotes: 4
Reputation: 38183
There are a few ways you could emulate the foreach
loop at $array['options']
— the simplest would be to define a function elsewhere that does that, and then return that options array! Like:
function return_array_options($input) {
$array = array();
foreach($input as $number) {
$array[] = $number
// or, $array['option'.$number] = $number
};
return $array;
}
Then elsewhere in your code you can use this function:
$my_array = array();
$my_array['options'] = return_array_options(array(1,2,3,4));
With programming, it's always better to break things down into solutions to smaller problems that you then combine.
Also, check out PHP's functional programming functions, like array_map()
, and array_filter()
. They help to transform things just like a loop woud, but without actually using a loop. So, they are syntatically more flexible in some ways.
Upvotes: 1
Reputation: 42450
Have a look at the code below. I have commented it so you can understand what I'm doing.
$input = array('one','two','three','four');
// set the base info here, i.e., info that is common to each push
$baseInfo = array(
'title' => 'foo',
'type' => 'options',
'page' => 'font_manager',
);
// now loop
foreach($input as $number) {
// fill in base info with data that is specific to this iteration
$baseInfo['id'] = $number;
$baseInfo['options'] = $input;
// do the push
array_push($big_array, $baseInfo);
}
I'm not entirely sure I got your question correct. If you were asking something else, please clarify and I will edit my answer.
Upvotes: 1