1

Ok, I need an array to be outputted like this:

$sections = array(
    5 => $americanFlag,
    6 => $americanFlag,
    22 => $russianFlag,
    23 => $russianFlag,
    24 => $russianFlag,
    25 => $russianFlag,
);

Ofcourse it is much longer than this.

So, say I have an array like this:

$russian = array(22, 23, 24, 25);
$american = array(5, 6);

And arrays like this:

$americanFlag = 'http://pathtomyAmericanFlag.png';
$russianFlag = 'http://pathtomyRussianFlag.png';

How can I do this quick and easy??

5 Answers 5

2

There are probably a few ways to do it. Here's a simple one:

$russian = array_fill_keys($russian, $russianFlag);
$american = array_fill_keys($american, $americanFlag);
$sections = ksort(array_merge($russian, $american));

Assumes you want them sorted by key. If not, just remove the ksort()

Sign up to request clarification or add additional context in comments.

3 Comments

Of course array_fill_keys() ... Need to remember it. btw: It seems, that the keys should be identifiers (like in a dictionary, or map), so I guess you can assume, that the order is unimportant.
Thanks, no need to sort it by key! Very much appreciated!
This untested answer will mislead and confuse researchers. $sections will be true, not an array.
0
$usFlags = array_combine($american, array_fill(0, count($american), $americanFlag);
$ruFlags = array_combine($russian, array_fill(0, count($russian), $russianFlag);
$sections = array_merge($usFlags, $ruFlags);

Should do it. However, I don't know, what you want to achieve, but it seems, that you want to output a flag depending on (I guess) an ID of something?

flags = array_merge(array_flip($russian), array_flip($american));
$helper = function ($id) use (flags) {
    return isset($flags[$id]) ? $flags[$id] : null;
}


echo 'http://pathtomy'. ucfirst($helper($id)) . 'Flag.png'; // returns 

2 Comments

Is there an easier way to build the arrays that could achieve the END RESULT of what I'm looking for? And, yes, definitely want to output the image based on an id of something.
Have a look at gorons answer. Semantically it's the same like my first solution, but it omits (in PHP) two steps (each) by using the builtin-function array_fill_keys() (which I forgot)
0

You can use a multi-dimensional array:

array (
 american => array ( [0] => american, [1] american );
 russian => array  ( [0] => russian, [1] russian );

Is that what you're wanting?

Comments

0

Something similar to this maybe.

 $countries = array($russian, $american);
   foreach($countries as $country){
     foreach($country as $flag) {
       echo $sections[$flag];
     }
}

Comments

0

Try this

<?php

$russian = array();
$american = array();

foreach($sections as $key=>$value){
   if($value == "http://pathtomyRussianFlag.png"){
      array_push($russian, $key);
   }
   elseif($value == "http://pathtomyAmericanFlag.png"){
      array_push($american, $key);
   }
}

?>

3 Comments

Especially some other answer already exists the use of array_push() seems oversized both from the semantic point of view (why treating the arrays as stack?) as well as the performance.
@KingCrunch I chose to write everything out since it might be easier for the OP to understand. Performance will take a hit because of the if statement, but I doubt it will matter much for what he is working with
Yes, in fact I was more concerned about the readabiltiy (array_push($foo, $bar) vs $foo[] = $bar). See php.net/array_push

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.