Adding data to an array
NickName:Brooke. Ask DateTime:2011-09-18T16:31:22

Adding data to an array

I am trying a add data to an array using a while loop but it seems to be adding the data as a string not array. Loops/arrays are something I'm still learning any help would be great.

$c = 0;
$numberofcustom = 5;
$defaults = array(
'title' => __('Follow Us!', 'smw'),
 'text' => ''
);
while ($c < $numberofcustom) {
    $customnumber = $c + 1;
    $defaults.=array(
        'custom' . $customnumber . 'name' => __('', 'smw'),
        'custom' . $customnumber . 'icon' => __('', 'smw'),
        'custom' . $customnumber . 'url' => __('', 'smw')
    );
    $c++;
}

print_r($defaults);

The problem seems to be with adding the data from the loop if I do a print_r just on that I just get "array" back.

Any help would be appreciated.

UPDATE

I decided I don't need a multi dimensional array so I used the suggestions below and came up with

while( $c < $numberofcustom){
    $customnumber = $c+1;
        $defaults['custom'.$customnumber.'name'] = __('', 'smw');
        $defaults['custom'.$customnumber.'icon'] = __('', 'smw');
        $defaults['custom'.$customnumber.'url'] = __('', 'smw');
    $c++;       
    }

Copyright Notice:Content Author:「Brooke.」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/7460356/adding-data-to-an-array

Answers
Matteo Riva 2011-09-18T08:39:08

Don't do this:\n\n$defaults.=array(\n\n 'custom'.$customnumber.'name' => __('', 'smw'),\n 'custom'.$customnumber.'icon' => __('', 'smw'),\n 'custom'.$customnumber.'url' => __('', 'smw')\n );\n\n\ndynamic array keys are almost as bad as variables with a dynamic name. Use another array level instead:\n\n$defaults[$customernumber] = array(\n 'customname' => __('', 'smw'),\n 'customicon' => __('', 'smw'),\n 'customurl' => __('', 'smw'),\n);\n",


More about “Adding data to an array” related questions

Adding data to an array

I am trying a add data to an array using a while loop but it seems to be adding the data as a string not array. Loops/arrays are something I'm still learning any help would be great. $c = 0; $

Show Detail

How to adding array data to existing array data in PHP?

how to adding array data to existing array? For example I already have array from cookie: Array ( [id] =&gt; 1 [name] =&gt; People 1 [address] =&gt; Indonesia ) I want to add another...

Show Detail

reducer: adding to array data

if i pull some data from an external source fro the initial state, then want to add additional information like for example 'liked'? i've tried adding to the products array but its go messy, I'm th...

Show Detail

Adding data to highstocks by pushing array

Been struggling to get my data right from the array to the chart. This is my code, &lt;script type="text/javascript"&gt; $(document).ready(function () { $.ajax({ type: "POS...

Show Detail

Problem in adding data to an array in Objective-C

I am grappling with adding an NSData object to a NSMutable array. The code is executing fine but it is not adding the objects in the array.The code is as follows: NSData * imageData =

Show Detail

Adding data to associative array

I am having an issue populating my array. I have a csv file which has 1 column containing some ids. I load and process the csv and end up with an array like so array:5 [▼ 0 =&gt; array:2 [▼ ...

Show Detail

Adding gaussian noise in fortran to an array of data

I need some help in adding Gaussian noise to my Fortran code. I have to basically construct an array of size 4. I enter the value into the array and then I have to add the noise. I have tried looking

Show Detail

Adding data to array through loop php

I have a problem where I am getting data from database and need to put that in an array. The array is associative and I am not sure about this practice so I thought I should ask the community. Is t...

Show Detail

Adding data to an array of struct

I'm trying to add/remove data from an array of a defined struct. struct process { public int Proc_Id; public int Proc_BurstTime; public int Proc_Priority; public override string To...

Show Detail

Oxyplot, adding data from an array

I have an array with 42 double values (double[] data = new double[42];). Now I want to visualize this data with the oxyplot chart, but I did not figure out how I can do that, because on the website...

Show Detail