0

I am wondering how can I store all values from a foreach loop, I know that I am re-initialising in the loop but I'm not sure how to store the data. Heres my basic loop:

$array = array("v1", "v2", "v3", "v4");
foreach($array as $row){
    $arr = array('val' => $row);
    echo $row;
}
print_r($arr);

So when I use the print_r($arr) the only thing outputted would be v4 and I know that the values are there because the echo $row; does return each output individually.

My question would be how can I store each instance of row in my array?

2
  • 1
    $arr[] = array('val' => $row); perhaps? But what do you actually want $arr to be after the loop? Commented Jan 6, 2014 at 17:10
  • read an array and creating same array. right? or you want two dimentional array? Commented Jan 6, 2014 at 17:10

3 Answers 3

3

Create a new array, fill it:

$array = array("v1", "v2", "v3", "v4");

$newArray = array();

foreach($array as $row){
    // notice the brackets
    $newArray[] = array('val' => $row);

}

print_r($newArray);
Sign up to request clarification or add additional context in comments.

3 Comments

This sorts the issue, thanks also can I ask by creating a multi-dimensional array how can I get the $array? As right now this is what I get Array ( [0] => Array ( [val] => v1 ) [1] => Array ( [val] => v2 ) [2] => Array ( [val] => v3 ) [3] => Array ( [val] => v4 ) ) And I am wondering how can I get the values in Array?
@user3144542 please report a concrete use case [i.e. some real and thus non dummy data].
Well the values should be the same but in the end I would like to use the keys as a column name in an SQL statement and the values as the value I am searching for. To do this I will use CI built in method which searches with associative arrays heres a link. Thats why I want to get the values like this 'val' => v1 'val' => v2, 'val' => v2
0

It looks like you are storing your array wrong.

Try adjusting the $arr = array('val' => $row);

to:

$arr[] = array('val' => $row);

This will set it so you pick up each line as a separate array which you can easily navigate through.

Hope this helps!

Comments

0

If I'm reading correctly, you want to transform your array from simple values to key-value pairs of 'val'->number. array_map is a concise way of doing this sort of transformation.

$array = array("v1", "v2", "v3", "v4");
$arr = array_map(function($v) { return array('val'=>$v); }, $array);
print_r($arr);

While it doesn't matter in this case, array_map also has the handy feature of preserving your keys, in case that is desired.

Note that you can also provide a named function to array_map, instead of providing the implementation inline, which can be nice in the event that your transform method gets more complicated. More on array_map here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.