How to join arrays with a common one

What I have is this:
<span style="font-weight: bold; color: darkslategray">take</span>, <span style="font-weight: bold; color: darkslategray">taking</span>, <span style="font-weight: bold; color: darkslategray">took</span>, <span style="font-weight: bold; color: darkorange">with</span>, <span style="font-weight: bold; color: darkorange"> for </span>, <span style="font-weight: bold; color: yellowgreen"> son</span>, <span style="font-weight: bold; color: mediumturquoise">water</span> and <span style="font-weight: bold; color: cyan">earth</span>.

If I set up an array manually this is the way I want:
$keyword_colorArr = array( array(array("take","darkslategray"), array("taking","darkslategray"), array("took","darkslategray")), array(array("with","darkorange"), array("for","darkorange")), array(array("son","yellowgreen")), array(array("water","mediumturquoise")), array(array("earth","cyan")) );

I was trying it out but it didn’t work:
`$rh = explode(“;”, $summarySummary[0]);
//print_r($rh);

$keyword_colorArr = array();
for($c=0;$c<count($rh);$c++){
$rhd = explode(“-”, $rh[$c]);
for($d=0;$d<count($rh[$c]);$d++){
echo $rhd[1].“
\n”;
$n = $d+1;
if(($rhd[$d]==$rhd[$n])){
//implode($rh[$c]);
array_push ($keyword_colorArr, array($rhd[$n], $rhd[$d]));
}else{
array_push ($keyword_colorArr, $rhd);
}
}
}
var_dump($keyword_colorArr);`

I am not sure exactly what you want to do.

I think this may be useful:

<?php 

  $colours = [
  "take"    => "darkslategray",
  "taking"  => "darkslategray",
  "took"    => "darkslategray",
  "with"    => "darkorange",
  "for"     => "darkorange",
  "son"     => "yellowgreen",
  "water"   => "mediumturquoise",
  "earth"   => "cyan",
  ];

  foreach($colours as $item => $color):
    echo '<span style="color: ' .$color .'">' .$item .'</span>, &nbsp;';
  endforeach;  
?>

Output:

This is the outcome that I want:
$keyword_colorArr = array( array(array("take","darkslategray"), array("taking","darkslategray"), array("took","darkslategray")), array(array("with","darkorange"), array("for","darkorange")), array(array("son","yellowgreen")), array(array("water","mediumturquoise")), array(array("earth","cyan")) );

Notice take, taking and took are individual arrays within an array along with a similar color.

This is how it’s stored in the db table:
take-darkslategray;taking-darkslategray;took-darkslategray;with-darkorange; for -darkorange; son-yellowgreen;water-mediumturquoise

How do you formulate to bring to this:
`PHP

          John_Betong

      


      
    

    





    
    
    
    
    
    
    
    
    
    
    
    
    
    











  This is the outcome that I want:

$keyword_colorArr = array(
array(array(“take”,“darkslategray”), array(“taking”,“darkslategray”), array(“took”,“darkslategray”)),
array(array(“with”,“darkorange”), array(“for”,“darkorange”)),
array(array(“son”,“yellowgreen”)),
array(array(“water”,“mediumturquoise”)),
array(array(“earth”,“cyan”))
);`

<?php

$source_text = '<span style="font-weight: bold; color: darkslategray">take</span>,
<span style="font-weight: bold; color: darkslategray">taking</span>,
<span style="font-weight: bold; color: darkslategray">took</span>,
<span style="font-weight: bold; color: darkorange">with</span>,
<span style="font-weight: bold; color: darkorange"> for </span>,
<span style="font-weight: bold; color: yellowgreen"> son</span>,
<span style="font-weight: bold; color: mediumturquoise">water</span> and
<span style="font-weight: bold; color: cyan">earth</span>.';

$keyword_colorArr = array();

preg_match_all ('/<span\s+?.*?style\s*?=\s*?"(?:.+?;\s*?|)color:\s*?([a-z]+)(?:\s*?;.*?|)".*?> *?(.+?)\s*?<\/span>/si', $source_text, $array1, PREG_SET_ORDER);

if ($array1)
{
    $array2 = array();

    foreach ($array1 as $i)
    {
        $color = $i[1];
        $a = array (trim ($i[2]), $color);
        if (isset ($array2[$color])) $array2[$color][] = $a;
        else $array2[$color] = array ($a);
    }

    foreach ($array2 as $i) $keyword_colorArr[] = $i;
}

var_export ($keyword_colorArr);

echo "\n";

Does this code work correctly according to your requirements? Is the order of elements in the array important?

The end result is what I’m looking for. But the beginning isn’t html. It’s taken from the db table like this:
take-darkslategray;taking-darkslategray;took-darkslategray;with-darkorange; for -darkorange; son-yellowgreen;water-mediumturquoise

Then why did you post the HTML string?

to simply show that the colors in the array are related to the words.

Ok I made this change on your code and it worked:

<?php 
//for overview.php

$bk2=2;
$ch2=30;


$sqlSummary = "SELECT * FROM ".strtolower($bookTitle[0]).$chapter[0]." WHERE rel_book_title = 'Genesis' AND rel_chapter = '8' ORDER BY ".$recordType[0].$chapter[0]."_ID ASC";

$resultSummary = $db->query($sqlSummary);

// Step 4: Iterate over the results
while($rowSummary = $resultSummary->fetch(PDO::FETCH_ASSOC)){
    $idSummary[] = $rowSummary[$recordType[0].$chapter[0]."_ID"];
    $datetimeSummary[] = $rowSummary['date_time'];
    $bookSpokeSummary[] = $rowSummary['book_spoke'];
    $chapterSpokeSummary[] = $rowSummary['chapter_spoke'];
    $relBookSummary[] = $rowSummary['rel_book'];
    $relBookTitleSummary[] = $rowSummary['rel_book_title'];
    $relChapterSummary[] = $rowSummary['rel_chapter'];
    $summarySummary[] = $rowSummary['summary'];
    $url_linksSummary[] = $rowSummary['url_links'];
}
//print_r($summarySummary);

$rh = explode(";", $summarySummary[0]);
//print_r($rh);

$array1 = array();
for($c=0;$c<count($rh);$c++){
    $rhd = explode("-", $rh[$c]);
    for($d=0;$d<count($rh[$c]);$d++){
        //echo $rhd[1]."<br />\n";
        array_push ($array1, $rhd);
    }
}

//var_export ($array1);

if ($array1)
{
    $array2 = array();

    foreach ($array1 as $i)
    {
        $color = $i[1];
        //$a = array (trim ($i[2]), $color);
        $a = array ($i[0], $i[1]);
        if (isset ($array2[$color])) $array2[$color][] = $a;
        else $array2[$color] = array ($a);
    }

    foreach ($array2 as $i) $keyword_colorArr[] = $i;
}

//var_export ($keyword_colorArr);

$commentsarray = array(
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
""
);

$highlight = array(
array(array("they that","black"))
);

give();
?>

However if I want to join other arrays how can I express within the code? I also want to associate $highlight to the 2nd array.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.