-1

This is my code :

$cars = array(
    array("Volvo" , '22,18,'),
    array("BMW" , '15,13'),
    array("Saab", '5,2' ),
    array("Land Rover", '17,15' )
);

print_r($cars);

this is the output :

Array ( [0] => Array ( [0] => Volvo [1] => 22,18, ) [1] => Array ( [0] => BMW [1] => 15,13 ) [2] => Array ( [0] => Saab [1] => 5,2 ) [3] => Array ( [0] => Land Rover [1] => 17,15 ) ) 

I want to dynamically push values into (for example) the string numeric values inside that multidimensional array but not overwrite the existed cell (as the same as you append to string $string .= $string.' + Extra Content';)

for example this is the original :

array("Volvo" , '22,18,'),

lets add '21,' and later lets add '27,' and later lets add '14,'

DYNAMICALLY under that specific cell.

so by the end of the day it would be :

array("Volvo" , '22,18,21,27,14,')

Is that possible ?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
iprophesy
  • 175
  • 3
  • 9
  • `$cars[0][1] .= '21,';`, `$cars[0][1] .= '27,';`, and `$cars[0][1] .= '14,';`? – Sean Jan 09 '15 at 22:27
  • 1
    A string value in an array behaves exactly the same as a string value in a separate variable, so you can do exactly the same thing. What exactly is the problem and what have you tried? – jeroen Jan 09 '15 at 22:27
  • OMG so simple but why this is not working ===>>> $cars['Volvo'][1] .= '21,'; ?? – iprophesy Jan 09 '15 at 22:31
  • Because `Volvo` is not the key, it is the value -> `[0] => Volvo` – Sean Jan 09 '15 at 22:32
  • So I want to ask how can I add the same exact thing under value (and not under key) ? – iprophesy Jan 09 '15 at 22:33
  • You should rethink your data structure. I don't know where it comes from, but if you build it yourself, you could use the name as the key and replace the string of numbers with an array of numbers. That would make manipulation and for example searching a lot easier. – jeroen Jan 09 '15 at 22:36
  • But If I don't know what is the key ? I know under which name I should add dynamically the new value but I don't know the key – iprophesy Jan 09 '15 at 22:38
  • And what do you mean with `by the end of the day`? As soon as the script finishes, all variables are gone. Except session variables but even those don't necessarily last a day. – jeroen Jan 09 '15 at 22:39
  • by the end of the day = when script completely executed – iprophesy Jan 09 '15 at 22:40
  • And I thought that is extremely simple question... – iprophesy Jan 09 '15 at 22:46

1 Answers1

-1

Sample solution with comments explaining it step by step (for PHP >= 5.5) For PHP < 5.5 you can use array_walk combination

<?php

$cars = array(
    array("Volvo" , '22,18,'),
    array("BMW" , '15,13'),
    array("Saab", '5,2' ),
    array("Land Rover", '17,15' )
);

print_r($cars);

$labels = array_column($cars, 0); // Get list of car manufacturers PHP>=5.5
$labels = array_map(function($element){return $element[0];}, $cars); // Get list of car manufacturers PHP 4+
$id = array_search('Volvo', $labels); // Find id of 'Volvo'
$cars[$id][1].='21,'; //append value
$cars[$id][1].='27,'; //append value

print_r($cars);

Which u can test on ideone

mleko
  • 11,650
  • 6
  • 50
  • 71