PHP Add Elements to Multidimensional Array with Array_Push

PHP add elements to multidimensional array with array_push

if you want to add the data in the increment order inside your associative array you can do this:

$newdata =  array (
'wpseo_title' => 'test',
'wpseo_desc' => 'test',
'wpseo_metakey' => 'test'
);

// for recipe

$md_array["recipe_type"][] = $newdata;

//for cuisine

$md_array["cuisine"][] = $newdata;

this will get added to the recipe or cuisine depending on what was the last index.

Array push is usually used in the array when you have sequential index: $arr[0] , $ar[1].. you cannot use it in associative array directly. But since your sub array is had this kind of index you can still use it like this

array_push($md_array["cuisine"],$newdata);

Pushing array into multidimensional array (php)

You're assigning the return value of array_push to the games array.
The return value of array_push is the amount of elements after pushing.

Just use it as

array_push($array, $newElement);

(Without assignment)

If you're only pushing one element at he time, $array[] = $newElement is preferred to prevent overhead of the function call of array_push

array_push multidimensional array

Because there's no element in $return_array indexed by 'preference'. You can append $preference with this instead

$return_array[$count_answers]['preference'][] = $preference;

or initialize with an empty array first

$return_array[$count_answers]['preference'] = array();

If you don't want to add an array of preferences, but just one element 'preference', append it with

$return_array[$count_answers]['preference'] = $preference;

Append several one-dimentional arrays to a one-dimentional array with array_push

array_push adds an element(s) to the end of the array.
since you are pushing an array onto your result, it will add it as an array instead of concatenating, which is the reason it is creating a 2D array.

You will need to use a technique that will concatenate/append the elements of the array to the result array.

One way you can do this by using the array_merge function like so:

$arr_Total_WordText = array_merge($arr_Total_WordText,$arrWordText);

Another way is to iterate the elements of the array $arrWordText
one by one and append them to your result.

PHP - array_push to create a multidimensional array

You just need to create the new array and add it to the $userArray, I use [] instead of array_push() though...

$userArray[] = [ 'user_id' => $user->ID, 'user_name' => $user->name];

Adding elements to multidimensional array

You have to do it like below:-

<?php

$result[] = ['aa' => 'bb', 'cc' => 'dd', 'ee' => 'ff'];
$result = array_merge($result[0], ['gg' => 'hh', 'ii' => 'jj']);

print_r($result);

https://eval.in/850034

PHP: array_push on multidimensional arrays and display its elements

<?php foreach($query_56 as $key=>$notes):
$each_complaints[$key]["date"] = $notes->date;
$each_complaints[$key]["text"] = $notes->corresponding_text ;
endforeach;

echo "<pre>";print_r($each_complaints);
?>
**Output :**
(
[0] => Array
(
[date] => 01-11-2014
[text] => rrrrrr
)

[1] => Array
(
[date] => 02-11-2014
[text] => fffff
)

[2] => Array
(
[date] => 03-11-2014
[text] => ddddd
)

)

how to push values in multidimensional array in php

You can try the following way with the help of array_walk().

$classType = [
["class" => "1", "type" => "A"],
["class" => "2", "type" => "A"],
["class" => "3", "type" => "B"]
];

$clollege['studentDetails'] = [
["grade" => "2", "hobbies" => ["A" , "B"] ],
["grade" => "2", "hobbies" => ["A" ] ],
["grade" => "3", "hobbies" => [ "C" ] ]
];

$classType = array_column($classType, 'type', "class");
array_walk($clollege['studentDetails'], function(&$item) use($classType) {
$item['type'] = $classType[$item['grade']];
});

echo '<pre>';
print_r($clollege['studentDetails']);
echo '</pre>';

Working demo.



Related Topics



Leave a reply



Submit