1

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',
       );
5
  • are the id, title, type and page keys from your $big_array? or are them keys that you want to add to the big array too? I think showing how does your $big_array looks like would help a lot to understand the question Commented Nov 26, 2012 at 8:53
  • I want to add them too, editing question. Commented Nov 26, 2012 at 11:32
  • Unless I'm missing something, you haven't specified that the $input array should be pushed into 'options'... Commented Nov 28, 2012 at 19:35
  • As you probably can see in 5th line of 2nd code - its already there, the question is - how to put it there :) Commented Nov 28, 2012 at 19:39
  • Include loops in array ??? Really your question is not clear ... are you replacing element in the array or you are just adding element to the array ?? Commented Nov 29, 2012 at 7:39

4 Answers 4

4
+50

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

Your code doesn't seem to add anything to 'options' array, I've checked other options and looks like the problem lies in $baseInfo['options'] = $input;. $fontsArray['options'] = 'something' doesn't work too. But $fontsArray['options'] = array('test'); works. Any ideas why?
1

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.

Comments

1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.