Loop through foreach and put elements from that foreach into a new multi dimensional array

kemosabe

I have a foreach that is looping over an array as it's supposed to, and then I'm pulling information out of that array from the foreach. I need to put this information into a separate empty array. I've got working, kind of. Here's my code:

newArray = array();
foreach ($result as $value) {
    newArray[] = $value['sample1'];
    newArray[] = $value['sample2'];
    newArray[] = $value['sample3'];
}
print_r(newArray);

This prints an associative array that looks like:

{
    0: "1",
    1: "1",
    2: "blah",
    3: "etc through as many values as I have",
}

Instead I need to have it form an array that looks like:

{
    0: {
        0: "1",
        1: "1",
        2: "blah"
    },
    1: {
        0: "etc"
    }
}

Any recommendations on how I should go about creating the new multidimensional array?

RiggsFolly

Build a temp array containing the parts of the original you want, then add that temporary array to newArray like this

foreach ($result as $value) {
    $t = [ $value['sample1'], $value['sample2'], $value['sample3'] ];
    $newArray[] = $t;
}

Or is you are using an old PHP

foreach ($result as $value) {
    $t = array( $value['sample1'], $value['sample2'], $value['sample3'] );
    $newArray[] = $t;
}

OR shorthand

foreach ($result as $value) {
    $newArray[] = [ $value['sample1'], $value['sample2'], $value['sample3'] ];
}

and old PHP

foreach ($result as $value) {
    $newArray[] = array( $value['sample1'], $value['sample2'], $value['sample3'] );
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to make a multi dimensional array in a foreach loop

foreach loop inside multi-dimensional array

Multi-Dimensional Array if array elements are missing in foreach

How to use foreach loop to display [offer] from this multi-dimensional array

php - Getting Data from multi dimensional array (foreach)

Foreach loop through an array of JSON from an API

Dynamically create multi multidimensional array while looping through foreach loop

Foreach array and create new array based on index then loop through that

Print multi-dimensional array using foreach

PHP Nested foreach on multi dimensional array

store foreach results in multi dimensional array

Foreach loop through multidimensional array

loop through two muti-dimensional array at same time using foreach loop

Foreach loop through results from array items from current month

Looping Through array from json responce using foreach loop

Convert "for" loop to "foreach" loop for multi array

How to loop through elements inside a foreach()

Loop through NodeList: Array.prototype.forEach.call() vs Array.from().forEach

multi-dimensional javascript array only affecting last array in forEach

Implement for foreach loop in javascript on Multi-dimensional arrays Issue

PHP Use array (from earlier while loop) to query new foreach

Put values into array using foreach loop

forEach processes array of DOM elements through an one

Using JS forEach to convert multi-dimensional array to html table

Can't pass Multi-Dimensional array to foreach function - PHP

Return Array from foreach loop

Loop through each multi dimensional array and echo one array

How to Loop through multi dimensional array of $_FILES array

Loop through an array of objects using forEach