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', ) ) ) );
$schemais nested array. You should modify it or iterateforeachof$schema[0]is_array($schema[0])and if true assign$schema(your local variable) to$schema[0].$schemavariable, 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)