0

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

2
  • 1
    I don't understand the requirements, can you give more examples? Commented Aug 14, 2011 at 22:27
  • the end result would basically be the sentence at the end.....based on the position of each individual string specified by ytext and ytitles, the individual strings of text and titles would be organized as such Commented Aug 14, 2011 at 22:35

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0
$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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.