2

I have an array which contains multiple other arrays. These arrays have a value called [note] which references a variable called $theNote which is located above/outside the array. This variable holds a simple template with a few spans.

$theNote = '<span class="icon"></span><span>Hello my name is $thename</span>';

I have an array called client_infos which contains multiple arrays inside like

  'client_infos' => array (
       array (
            'name' => 'John smith',
            'note' => $theNote,
            'prepend' => '',
            'append' => '',
            'formatting' => 'html',
        ),
        array (
            'name' => 'Mary smith',
            'note' => $theNote,
            'prepend' => '',
            'append' => '',
            'formatting' => 'html',
        ),
    );

There will be an unknown number of names eventually. What I need to be able to do is in my template, call $theNote in a loop or something and the following to be outputted...

<span class="icon"></span><span>Hello my name is John Smith</span>

As you can see, the [note] info in the array uses the $theNote varible, this outputs a block of code with a variable inside called $thename. I do not know how to get my [name] info.. into the $thename variable.

Basically some how get. $thename inside $theNote to = the current array [name] value.

The reason for all of this is so that I can easily update "the note" code once, without having to do it in all the child arrays.

I hope I am being clear enough, any ideas?

2
  • Also, any help with actually naming this question better? Commented Mar 6, 2014 at 9:04
  • So you want to loop over client_infos to output each ['name']? Is that it? Commented Mar 6, 2014 at 9:10

4 Answers 4

3

It may be better to use a class here:

class Client
{
    private $name;
    private $prepend;
    private $append;
    private $formatting;

    // place a constructor here

    // create getter and setter for the properties
    public function getNote() {
        return '<span class="icon"></span><span>Hello my name is ' . $this->name . '</span>'
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Would be good considering of Object oriented programming in such scenarios, wherein you can describe a client in far better way with a class object rather an array.
It's not always better to use classes, especially, when a procedure like this can be done with simple str_replace
0

If I get the question right, you can replace the $theName with a unique variable like %THE_NAME% and do something like this:

$theNote = '<span class="icon"></span><span>Hello my name is %THE_NAME%</span>';

$client_infos = array (
       array (
            'name' => 'John smith',
            'note' => $theNote,
            'prepend' => '',
            'append' => '',
            'formatting' => 'html',
        ),
        array (
            'name' => 'Mary smith',
            'note' => $theNote,
            'prepend' => '',
            'append' => '',
            'formatting' => 'html',
        ),
    );

foreach( $client_infos as $arr )
{
    echo str_replace( '%THE_NAME%', $arr['name'], $arr['note'] ) , '<br/>';
}

Output

Hello my name is John smith
Hello my name is Mary smith

Comments

0

Something like this:

     $theNote = '<span class="icon"></span><span>Hello my name is {name}</span>';

then to echo:

    echo str_replace('{name}', $client_infos[0]['name'], $client_infos[0]['note']);

Or to replace {name} in all the array

   for($i=0; $i<count($client_infos); $i++){
      $client_infos[$i]['note']=str_replace('{name}', $client_infos[$i]['name'], $client_infos[$i]['note']);
   }

Comments

-1

Try declaring part of $theNote within the array itself:

$theNote = '<span class="icon"></span><span>Hello my name is ';
$theNote2 = '</span>';
 'client_infos' => array (
       array (
            'name' => 'John smith',
            'note' => $theNote . $thename . $theNote2, 
            'prepend' => '',
            'append' => '',
            'formatting' => 'html',
        ),
        array (
            'name' => 'Mary smith',
            'note' => $theNote . $thename . $theNote2,
            'prepend' => '',
            'append' => '',
            'formatting' => 'html',
        ),
    );

Good luck!

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.