hi guys, i've two array 3d $data and $array_bold, then i want to bold value $data be based value $array_bold, how i can do this???

<?php 
$data = array(
            array(

                '0'=>array(
                        '0'=>'chelsea everton villa liverpool',
                ),

                     array(
                        '0'=>'milan inter juventus napoli',
                ),
        ),
                '1'=>array(
                        array(
                            '1'=>'madrid barcelona altetico getafe', 
                ),
                        array(
                            '1'=>'milan inter juventus napoli',
                )
        )
);

var_dump($data);
$array_bold = array(
                array(
                    '0'=>array(
                        '0'=>'chelsea everton',
                    ),
                        array(
                        '0'=>'juventus napoli',
                    ),
                ),
                    '1'=>array(
                        array(
                        '1'=>'barcelona altetico', 
                    ),
                        array(
                        '1'=>'inter juventus',
                    )
                )
);

var_dump($array_bold);
?>

so, the result like this:

array(2) { [0]=> array(2) { [0]=> array(1) { [0]=> string(31) "<b>chelsea everton<b/> villa liverpool" } [1]=> array(1) { [0]=> string(27) "milan inter <b>juventus napoli<b/>" } } [1]=> array(2) { [0]=> array(1) { [0]=> string(32) "madrid <b>barcelona altetico<b/> getafe" } [1]=> array(1) { [0]=> string(27) "milan <b>inter juventus<b/> napoli" } } }

help me :(

There is something missing from your example, there is no way to define for example $data[0][0][0] = 'chelsea everton villa Liverpool' and in the var_dump to get '<b>chelsea everton<b/> villa liverpool'.

Also there are many inconsistencies in the way that array. For example $data[0][0][0] is a string while $data[1][0][0] doesn’t exists and then $data[1][0][1] again is a string. And of course you have some commas that don’t make any sense e.g. after “villa liverpool'” .

Let’s assume that you want to extract bold tags from strings in a x Multi Dimensional Array

<?php

libxml_use_internal_errors(true);

/**
 * Here I use the data from your example. As I mentioned and as you can see
 * there seems to be something wrong about that array.
 */
$data = array(
          array('0'=>
                      array('0'=>'<b>chelsea everton</b> villa liverpool',),
                      array('0'=>'milan inter <b>juventus napoli</b>',),
                      ),
                '1'=> 
                array(
              array('1'=>'madrid <b>barcelona altetico</b> getafe',),
                    array('1'=>'milan <b>inter juventus</b> napoli',)
                    )
                );

var_dump(parseBoldsArray($data));





/**
 * Takes an array and returns an array with same dimentions that as values has 
 * only the text inside bold tags. If a value haven't bold tag or is not 
 * a string it puts an empty string. 
 * @param array $array
 */
function parseBoldsArray($array)
{
  if(!is_array($array))
  {
    throw new Exception(var_dump($array)." is not an array", 55501);
  }
  $parsedArray = array();
  foreach($array as $key => $value)
  {
    if(is_array($value))
    {
      $parsedArray[$key] = parseBoldsArray($value);
    }
    else if(is_string($value))
    {
      $parsedArray[$key] = parseBoldText($value);
    }
    else
    {
      $parsedArray[$key] = "";
    }
  }
  return $parsedArray;
}

/**
 * Takes a string and replace each contents that are inside bold tags or 
 * any empty string if there is no bold tag. 
 * @param string $string
 * @return string
 */
function parseBoldText($string)
{
  if(!is_string($string))
  {
    throw new Exception(var_dump($string)." is not a string", 55502);
  }  
  $doc = new DOMDocument();
  $doc->loadHTML($string);
  $boldNodesList = $doc->getElementsByTagName("b");
  $boldsArray = array();
  foreach($boldNodesList as $boldNode)
  {
    $boldsArray[] = $boldNode->textContent;
  }
  return implode(" ", $boldsArray);
}

?>

If the structure is going to remain consistent:

<?php 

$data = array(
    array(
        array(
            0 => 'chelsea everton villa liverpool',
        ),
        array(
            0 => 'milan inter juventus napoli',
        ),
    ),
    array(
        array(
            1 => 'madrid barcelona altetico getafe', 
        ),
        array(
            1 => 'milan inter juventus napoli',
        ),
    ),
);

$bold = array(
    array(
        array(
            0 => 'chelsea everton',
        ),
        array(
            0 => 'juventus napoli',
        ),
    ),
    array(
        array(
            1 => 'barcelona altetico', 
        ),
        array(
            1 => 'inter juventus',
        ),
    ),
);

$data_bold = array();

foreach($data as $i => $array_i)
{
    foreach($array_i as $j => $array_j)
    {
        foreach($array_j as $k => $value)
        {
            if(isset($bold[$i][$j][$k]))
            {
                $replace = $bold[$i][$j][$k];
                $value = str_replace($replace, "<strong>{$replace}</strong>", $value);
            }

            $data_bold[$i][$j][$k] = $value;
        }
    }
}

echo '<pre>'; var_dump($data_bold); echo '</pre>'; die;
Member Avatar for diafol

@cussel

Please reply to posts that are made by contributors. Many of your threads have been left hanging. Mark them solved if they answer your question. Many of your threads on this theme have failed to elicit a reply from you. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.