Reputation:
I have an array that looks like this
$foreacharray = array(
"model" => array ("samsung", "sony", "philips", "acer", "hp"),
"qty" => array("3", "7", "5", "1", "8"),
);
What I'm trying to do is foreach model, draw the logo for the amount of its qty so Samsung = 3, Sony = 7 and so on will draw 3 Sony logos and 7 Samsung logos.
I came up with something like this
foreach ($foreacharray["model"] as $model)
{
foreach($foreacharray["qty"] as $qty)
{
echo $model;
echo '<br>';
}
}
But of course all that does is for each of the array entries, echo out the name so I end up with 5 samsungs being printed 5 sonys being printed etc.
How can I make this use the value of the qty array rather than the number of entries?
Upvotes: 2
Views: 3524
Reputation: 1966
Your $foreacharray["qty"]
has 5 elements, because of that when You loop this by foreach
, it prints all $foreacharray["model"]
elements 5 times. Your code works how, how it written.
Upvotes: 0
Reputation: 46359
I think you're trying to do this:
for( $i = 0, $e = count( $foreacharray["model"] ); $i < $e; ++ $i ) {
echo $foreacharray["model"][$i] . ' = ' . $foreacharray["qty"][$i] . '<br />';
}
Basically, a single loop references both arrays at the same index.
You could also use foreach, if you prefer, like this:
foreach( $foreacharray["model"] as $key => $model ) {
echo $model . ' = ' . $foreacharray["qty"][$key] . '<br />';
}
Upvotes: 0
Reputation: 9200
If you are in control of the array generation in the first place, then you are better off just generating a single array with the model as the key and the qty as the value, rather than generating 2 separate arrays.
Upvotes: 1
Reputation: 70540
// #1 using one for keys, another for values:
foreach(array_combine($foreacharray['model'],$foreacharray['qty']) as $model => $nr){
var_dump($model,$nr);
}
// #2 map them with null:
foreach(array_map(null,$foreacharray['model'],$foreacharray['qty']) as $data){
var_dump($data);
}
// #3 iterators!
$iterator = new MultipleIterator();
$iterator->attachIterator(new ArrayIterator($foreacharray['model']));
$iterator->attachIterator(new ArrayIterator($foreacharray['qty']));
foreach($iterator as $data){
var_dump($data);
}
But best would indeed be changing this at the source.
Upvotes: 0
Reputation: 9
Iterate using array the array index instead, for example:
for (i=0; i<count($foreacharray["model"]); i++)
{
$model = $foreacharray["model"][i];
$qty = $foreacharray["qty"][i];
echo "$model: $qty";
}
Upvotes: 0
Reputation: 572
Why not make this simpler by using the function array_combine
?
http://www.php.net/manual/en/function.array-combine.php
Example;
$newArray = array_combine($foreacharray[0], $foreacharray[1]);
var_dump($newArray);
// will output
array (
"samsung" => 3,
"sony" => 7,
"philips" => 5,
"acer" => 1,
"hp" => 8,
)
Then you can easily get all values or display them like so...
foreach($newArray as $key => $val) {
echo $key . ' has a quantity of ' . $val . '.';
}
Hope this makes it easier.
Upvotes: 1
Reputation: 225281
You can use array_combine
to use models as keys and quantities as values, assuming models are unique:
$combined = array_combine($foreacharray['model'], $foreacharray['qty']);
foreach($combined as $model => $qty) {
# …
}
Upvotes: 0