0

I have a function which name is repeat.

I want to repeat array values if array key == 'repeat'

But my if ($k == 'repeat') comparement fails.

What is wrong with my comparement?

function repeat($schema, $repeat = array()){ foreach($schema as $k => $v){ 

if($k == 'repeat')

3rd line does not working properly inside repeat function.

$schema = array( array( 'tag' => 'div', 'class' => 'lines', 'repeat' => array( 'tag' => 'div', array( 'tag' => 'span', 'style' => 'margin:10px; padding:10px', 'key' => 'title', ), 'key' => 'subject', ) ) ); $repeat = array('Country Name' => 'Usa', 'City Name' => 'Newyork'); // Recursive String Replace - recursive_array_replace(mixed, mixed, array); function recursive_array_replace($find, $replace, $array){ if (!is_array($array)){ return str_replace($find, $replace, $array); } $newArray = array(); foreach ($array as $key => $value) { $newArray[$key] = recursive_array_replace($find, $replace, $value); } return $newArray; } function repeat($schema, $repeat = array()){ foreach($schema as $k => $v){ if($k == 'repeat'){ foreach($repeat as $rk => $rv){ $value = recursive_array_replace('title', $rk, $v); $value = recursive_array_replace('subject', $rv, $value); $info[] = $value; } } } //$schema = recursive_array_replace('repeat', $repeat, $schema); return $info; } print_r(repeat($schema, $repeat)); 

UPDATE1

Schema might be

$schema = array( 'tag' => 'div', 'class' => 'lines', array( 'tag' => 'div', 'class' => 'lines', 'repeat' => array( 'tag' => 'div', array( 'tag' => 'span', 'style' => 'margin:10px; padding:10px', 'key' => 'title', ), 'key' => 'subject', array( 'tag' => 'span', 'style' => 'margin:10px; padding:10px', 'key' => 'title', ), 'key' => 'subject', ) ) ); 

UPDATE 2

$schema = array( array( 'tag' => 'div', 'class' => 'lines', array( 'tag' => 'div', 'repeat' => array( 'tag' => 'div', array( 'tag' => 'span', 'style' => 'margin:10px; padding:10px', 'key' => 'title', ), 'key' => 'subject', ) ) ) ); 
8
  • Your $schema is nested array. You should modify it or iterate foreach of $schema[0] Commented Aug 25, 2013 at 20:39
  • $schema does not have constant value, always can change. $schema[0] might fail other situations. I need to compare index name of value Commented Aug 25, 2013 at 20:40
  • You may check is_array($schema[0]) and if true assign $schema (your local variable) to $schema[0]. Commented Aug 25, 2013 at 20:41
  • @Voitcus there might be different arrays before and after 'repeat' tag is_array can fail. Commented Aug 25, 2013 at 20:45
  • I can't help you as I don't know what is your intention. The compartment fails because there is no "repeat" key in your $schema variable, but there exist one in $schema[0]. I didn't test validity of your code (that's why I don't post it as an answer) Commented Aug 25, 2013 at 20:47

1 Answer 1

0

In my opinion compartment fails because your $schema variable does not have 'repeat' key.

You do foreach of all items in $schema variable, however (before update) there was only one item: the array. After the update there are three items, having keys 'tag', 'class', and one array which has no key, so it is automatically assigned to 0.

In both cases you are able to reach 'repeat' key by referring to the 0th item of the array (via $schema[0]['repeat']).

If there are no other errors (I haven't checked) you should replace

foreach($schema as $k => $v){ 

to

foreach($schema[0] as $k => $v){ 

This will refer to the nested array; however, only in case there are no other numeric keys in the $schema array.

To ensure this you might for example first check which item is array, eg.

foreach($schema as $item){ // this is only an auxiliary loop if(is_array($item)){ $new_schema=$item; break; } } 

and then move to main loop:

foreach($new_schema as $k => $v){ 

etc.

After UPDATE 2

You need to find your own way to access the 'repeat' key. You may do this iteratively. In your code you access the wrong array, the first level, which has no "repeat" key, but you should go deeper.

Sign up to request clarification or add additional context in comments.

3 Comments

How can I do this iteratively? Can you explain little bit more?
Instead of iterating in repeat function why won't you locate the 'repeat' key? It's the only one you use. In the manual there are lots of examples of finding specific key in multi-dimensional array. Check also StackOverflow
Finding is ok. But I could not find to compare.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.