Welcome Guest, Not a member yet? Register   Sign In
Add additional data to multidimensional array
#1

[eluser]trta911[/eluser]
hi, please help me, i have multiple records in mysql, which i passing on array from model to controller loop, but I need to add another array key and it's value for each of nested array (based on another function from controller) as shown below:

Code:
Array
(
    [0] => Array
        (
            [something from mysql 1] => 500
            [something from mysql 2] => 30
            [something from mysql 3] => 6
            [something from mysql 4] => 43sfghsrtjhurt3bd21c2dba29fdgargarehgaqw
            [something from mysql 5] => 1
      -->   [something i need to add] => something
        )

    [1] => Array
        (
            [something from mysql 1] => 200
            [something from mysql 2] => 30
            [something from mysql 3] => 1
            [something from mysql 4] => 438f130be27a0d53bd21c2dba293cda55ee667c5
            [something from mysql 5] => 1
      -->   [something i need to add] => something
        )
thanks
#2

[eluser]PhilTem[/eluser]
Pretty simple:

Code:
// $array is the array mentioned above
$length = size($array);
for ( $i = 0; $i < $length; $i++ )
  $array[$i]['something i need to add'] = $this->function_to_call_to_get_it_added();
end
#3

[eluser]trta911[/eluser]
Hey! very very thanks, this works like a charm.
Very thanks again.
#4

[eluser]greenju[/eluser]
Or
Code:
foreach($array as $subarray)
array_push($subarray,"myvalue");
#5

[eluser]trta911[/eluser]
Very thanks again, but I inspected, other problem with array, value of added key is calculated with another key's value from this array. works perfect, but gives me same value for each of them.

Better: I pass to function 'id'(value that is in array) and this function gives me something else based on this 'id'. But as note erlier, this function returns to whole array same value.


EDIT: sory, i found that is fully working with this (modified erlier post):

Code:
// $array is the array mentioned above
$length = size($array);
for ( $i = 0; $i < $length; $i++ )
  $array[$i]['something i need to add'] = $this->function_to_call_to_get_it_added($array[$i]['existing_key_from_array']);
end




Theme © iAndrew 2016 - Forum software by © MyBB