How to Add Elements to an Empty Array in PHP

How to add elements to an empty array in PHP?

Both array_push and the method you described will work.

$cart = array();
$cart[] = 13;
$cart[] = 14;
// etc

//Above is correct. but below one is for further understanding
$cart = array();
for($i=0;$i<=5;$i++){
$cart[] = $i;
}
echo "<pre>";
print_r($cart);
echo "</pre>";

Is the same as:

<?php
$cart = array();
array_push($cart, 13);
array_push($cart, 14);

// Or
$cart = array();
array_push($cart, 13, 14);
?>

php - declare empty array and dynamically add element to that array

There is the array_push method in PHP:

array_push($list, "value");

How can I add a value from one array to a new empty associative array in PHP?

Here you go:

<?php
$aNumberArray = array();
$aArray = array(4,4,5,7,4,8,7,9,4,3);
foreach ($aArray as $value) {
if (!isset($aNumberArray[$value])) {
$aNumberArray[$value] = 0;
}
$aNumberArray[$value] += 1;
}
print_r($aNumberArray);

Will give you:

Array
(
[4] => 4
[5] => 1
[7] => 2
[8] => 1
[9] => 1
[3] => 1
)

How to add element to empty array

As CinCout mentioned - the issue is that of scope - specifically you are resetting arr back to an empty within the function scope and then pushing the single value into it - meaning it will never have an more that the single value in it.

The solution is to set the array outside the scope of the function and then you will be able to push the value into the array.

    let arr =[];        function _check(){      let name = document.querySelector('.name').value;      arr.push(name);      console.log(arr);    }
<button type="button" onclick="_check()" class="name" value="test">Click me</button>

How add elements (key =value) to an empty array?

$arr = array();
foreach ($result as $row) {
$arr[$row->key] = $row->value;
}

How to add values for empty place in array for recursive formula in php

Thanks guys for your solutions
I created the target array and fixed it by using CarbonPeriod and Laravel Collection merging as followings:

        // Fetch workouts
$tssData = Workout::select('workoutDay', 'tss')->get();

//changing the data to Key:value
$sumTss = $tssData->groupBy('workoutDay')->map(function ($item) {
return $item[0]['tss'];
});

// creating default array included the all missed dates with zero value
$date2 = new Carbon($tssData->last()->workoutDay);
$date1 = new Carbon($tssData->first()->workoutDay);
$period = CarbonPeriod::create($date1, $date2);
foreach ($period as $date) {
$p[$date->format('Y-m-d')] = 0;
}

// using collection merging for comparing and replacing quickly
$tssData = collect($p)->merge(collect($sumTss))->toArray();
return $tssData;

How do I insert an array inside an empty array in PHP?

You can use this piece of code:

$array[] = ['Name' => 'aa', 'Surname' => 'bb'];

How to add value in the second/child layer of an array

Instead of overwrite array every time just use array_push like:

$popular[] = ['media_type' => $row["type"],$path];

Reference:

  • array_push


Related Topics



Leave a reply



Submit