re1man
re1man

Reputation: 2367

implode array and store as complete string based on positions of another array

I have four arrays: $text[], $ytext[], $titles[], $ytitles[]

What I want to do is implode the arrays, text and titles, by " " to store them as a complete string. But I want to arrange them based on their position (which is a y coordinate integer).

For example :

$text[] = 
{1 => hello
2=>   again
3 => more text
}

$ytext[] = 
{1 => 5
2=>  10
3 => 14
}

$titles[] = 
{1=> title
2=> title2
}

$ytitles[] = 
{1=> 2
2=> 11
}

SO this would look like: title hello again title2 more text

Upvotes: 0

Views: 131

Answers (2)

Paul
Paul

Reputation: 141827

Expanding on Andreas' solution, which was a very good idea, but doesn't quite work because array_merge() doesn't preserve numeric keys. You can use this solution:

$arr = array_combine($ytext, $text) + array_combine($ytitles, $titles);
ksort($arr);
echo implode(' ', $arr);

Upvotes: 1

Andreas
Andreas

Reputation: 2678

$newText = array_combine($ytext, $text);    // combine $ytext as keys and $text as values
$newTitles = array_combine($ytitles, $titles);
print implode(' ', array_merge($newText, $newTitles)); // merges and implode array

Upvotes: 0

Related Questions