PHP 2d Array Push Issue

php array push
php add to multidimensional array
php associative array push
php array add key value pair to existing array
php array push with key
php add to multidimensional array with key
php arrays
php add to associative array

I'm in a bind..

This is the output I would like;

Array
(
[country] => Array(
                    [0] => England
                    [1] => Channel Islands
                )
[gor] => Array(
                    [0] => North East
                    [1] => North West
                )
[parliamentaryconstituency] => Array(
                    [0] => Aldershot
                    [1] => Aldridge-Brownhills
                )
)

This is what I currently have;

Array
(
[0] => Array
    (
        [country] => England
    )
[1] => Array
    (
        [country] => Channel Islands
    )
[2] => Array
    (
        [gor] => North East
    )
[3] => Array
    (
        [gor] => North West
    )
[4] => Array
    (
        [parliamentaryconstituency] => Aldershot
    )
[5] => Array
    (
        [parliamentaryconstituency] => Aldridge-Brownhills
    )
)

My code is;

foreach ($input as $key => $value) {
    foreach ($value as $subkey => $subvalue) {
        switch ($key)
        {
            case 'country':
                $country = Country::description($subvalue)->get()->first();
                array_push($new_input, array('country' => $country->description));
              break;                    
            case 'gor':
                $gor = Gor::description($subvalue)->get()->first();
                array_push($new_input, array('gor' => $gor->description));
              break;
            case 'parlc':
                $parliamentaryconstituency = ParliamentaryConstituency::description($subvalue)->get()->first();
                array_push($new_input, array('parliamentaryconstituency' => $parliamentaryconstituency->description));
              break;
        }
    }
}

I considered array_push($new_input['country'], $country->description); and having $new_input['country'] above the foreachs but that will output an empty country array if no countries are selected and I'd prefer it not to appear at all if that's the case.


You could try this one:

$result = array();
foreach ($input as $key => $value) {
    foreach ($value as $subkey => $subvalue) {
        switch ($key)
        {
            case 'country':
                $country = Country::description($subvalue)->get()->first();
                if ($country) {
                    $result['country'][] = $country;
                }
                break;
            case 'gor':
                $gor = Gor::description($subvalue)->get()->first();
                if ($gor) {
                    $result['gor'][] = $gor;
                }
                break;
            case 'parlc':
                $parliamentaryconstituency = ParliamentaryConstituency::description($subvalue)->get()->first();
                if ($parliamentaryconstituency) {
                    $result['parliamentaryconstituency'][] = $parliamentaryconstituency;
                }
                break;
        }
    }
}
print_r($result);

array_push - Manual, array_push — Push one or more elements onto the end of array There is problem with pushing references to array, introduced in PHP 5.4 - did someone decide it is not needed? Array_push also works fine with multidimensional arrays. array_push() treats array as a stack, and pushes the passed variables onto the end of array.The length of array increases by the number of variables pushed. Has the same effect as:


array_push always add a new element at the end of the array. Instead, initialize the array with all the keys like this

$result = array("country" => array(), "gor" => array(), etc);

And in your loop assuming the value you want to insert is in $value do

$result[$key][] = $value

Or if you prefer using array_push

array_push($result[$key], $value);

php, It really isn't clear from the code you've posted what your starting array structure or desired finished array structure should be. At a guess, you� Whats the syntax for adding a new element (array) to the recipe_type array with array_push? I could never get my head around multidimensional arrays and I'm a bit confused. php multidimensional-array array-push


'$list=array();'


/add array value
'array_push($list,array("name"=>"jone","surname"=>"doe"));'

//get array value
'$list[index][columnname]'

PHP Multidimensional Arrays, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java� Dimensions: Dimensions of multidimensional array indicates the number of indices needed to select an element. For a two dimensional array two indices to select an element. Two dimensional array: It is the simplest form of a multidimensional array. It can be created using nested array.


Multidimensional arrays in PHP, Multidimensional arrays in PHP. Multi-dimensional arrays are such type of arrays which stores an another array at each index instead of single element. In other� I have a 2d array that I created like such: var images = new Array(50); for(var i=0;i<=51;i++) { images[i]=new Array(2); } Now I would like to push values into this array, but I can't get the


How to create two dimensional array in JavaScript?, How to take user input for two dimensional (2D) array in PHP ? How to create a link in JavaScript ? Object.create( ) In JavaScript � How to create a style tag using � This inbuilt function of PHP is used to push new elements into an array. We can push one or more than one element into the array and these elements gets inserted to the end of the array and because of the pushed elements into the array, the length of the array also gets incremented by the number of elements pushed into the array.


Multidimensional Arrays in PHP, Examples of multidimensional arrays in PHP: accessing and modifying the array_push function can be used to add elements to a sub-array as follows:. Parameters. needle. The searched value. Note: . If needle is a string, the comparison is done in a case-sensitive manner.. haystack. The array. strict. If the third parameter strict is set to TRUE then the in_array() function will also check the types of the needle in the haystack.