1

I have a function that returns an multidimensional array() I want to concatenate a string to every value of that array. How can I do this

for example my string:

$this->$string = 'helloAddMeToArray';

and my array is:

array(array('url' => 'PleaseAddAStringToMeIAmLonely'));

So i need my array value to be like this: helloAddMeToArrayPleaseAddAStringToMeIAmLonely

I tried concatenating these with '.' but does not allow me

2
  • 5
    Unrelated: multidementional is an awesome Freudian typo Commented Dec 12, 2014 at 12:25
  • hehe silly auto correct on chrome 'multidimensional' xD Commented Dec 12, 2014 at 12:29

3 Answers 3

2
$oldArray = array(array('url' => 'PleaseAddAStringToMeIAmLonely'));
$newArray = array();
$this->string = 'helloAddMeToArray';

foreach($oldArray as $o) {
 $newArray[] = array('url' => $this->string . $o['url']);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Note the difference in the '$this->$string = 'helloAddMeToArray';' on this too. $this->$string is incorrect. @Mave has quite rightly changed it to $this->string = 'helloAddMeToArray';
1

Try this:
First get string from your multidimentional Array, and type cast it.

$myString2 = (string)$myArray[0]->url;

Now use concatination: $string.$myString2;

Comments

0

Assuming your array could look like :

[
    "key"=>[
           "keyK"=>"val"
            ],
     "key2"=>"val2"
]

and you want to concatenate a string to every value from that array you should use array_walk_recursive function . This is a short snnipet doing this job :

$stringToConcatenate=$this->$string = 'helloAddMeToArray';
$callback($val,$key) use ($stringToConcatenate){
$val.=$val.$stringToConcatenate;
}
array_walk_recursive($youArray,$callback);

I hope it helps you .

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.