Home » Php » php – Add data dynamically to an Array

php – Add data dynamically to an Array

Posted by: admin April 23, 2020 Leave a comment

Questions:

I want to add data to an array dynamically.

How to&Answers:

There are quite a few ways to work with dynamic arrays in PHP.
Initialise an array:

$array = array();

Add to an array:

$array[] = "item";
$array[$key] = "item";
array_push($array, "item", "another item");

Remove from an array:

$item = array_pop($array);
$item = array_shift($array);
unset($array[$key]);

There are plenty more ways, these are just some examples.

Answer:

$array[] = 'Hi';

pushes on top of the array.

$array['Hi'] = 'FooBar';

sets a specific index.

Answer:

Let’s say you have defined an empty array:

$myArr = array();

If you want to simply add an element, e.g. ‘New Element to Array’, write

$myArr[] = 'New Element to Array';

if you are calling the data from the database, below code will work fine

$sql = "SELECT $element FROM $table";
$query = mysql_query($sql);
if(mysql_num_rows($query) > 0)//if it finds any row
{
   while($result = mysql_fetch_object($query))
   {
      //adding data to the array
      $myArr[] = $result->$element;
   }
}

Answer:

You should use method array_push to add value or array to array exists

$stack = array("orange", "banana");
array_push($stack, "apple", "raspberry");
print_r($stack);

/** GENERATED OUTPUT
Array
(
    [0] => orange
    [1] => banana
    [2] => apple
    [3] => raspberry
)
*/

Answer:

Like this?:

$array[] = 'newItem';

Answer:

In additon to directly accessing the array, there is also

array_push — Push one or more elements onto the end of array

Answer:

$dynamicarray = array();

for($i=0;$i<10;$i++)
{
    $dynamicarray[$i]=$i;
}

Answer:

Adding array elements dynamically to an Array And adding new element
to an Array

$samplearr=array();
$count = 0;
foreach ($rslt as $row) {
        $arr['feeds'][$count]['feed_id'] = $row->feed_id;
        $arr['feeds'][$count]['feed_title'] = $row->feed_title;
        $arr['feeds'][$count]['feed_url'] = $row->feed_url;
        $arr['feeds'][$count]['cat_name'] = $this->get_catlist_details($row->feed_id);
        foreach ($newelt as $cat) {
            array_push($samplearr, $cat);              
        }
        ++$count;
}
$arr['categories'] = array_unique($samplearr); //,SORT_STRING

$response = array("status"=>"success","response"=>"Categories exists","result"=>$arr);

Answer:

just for fun…

$array_a = array('0'=>'foo', '1'=>'bar');
$array_b = array('foo'=>'0', 'bar'=>'1');

$array_c = array_merge($array_a,$array_b);

$i = 0; $j = 0;
foreach ($array_c as $key => $value) {
    if (is_numeric($key)) {$array_d[$i] = $value; $i++;}
    if (is_numeric($value)) {$array_e[$j] = $key; $j++;}
}

print_r($array_d);
print_r($array_e);

Answer:

Fastest way I think

       $newArray = array();

for($count == 0;$row = mysql_fetch_assoc($getResults);$count++)
    {
    foreach($row as $key => $value)
    { 
    $newArray[$count]{$key} = $row[$key];
    }
}