Viewed   88 times

print_r($samplearr) prints the following for my array containing 3 items:

Array ( [4722] => Array ( [value1] => 52 [value2] => 46 )
Array ( [4922] => Array ( [value1] => 22 [value2] => 47 )
Array ( [7522] => Array ( [value1] => 47 [value2] => 85 )

I want to put these into an HTML table so I was doing a foreach but its not doing what I expected:

foreach($samplearr as $item){
     print "<tr><td>" . key($item) . "</td><td>" . $samplearr['value1'] . "</td><td>" . $samplearr['value2'] . "</td></tr>";
}

Which is returning:

<tr><td>value1</td><td>52</td><td>46</td></tr>

This would be the first output I am wanting:

<tr><td>4722</td><td>52</td><td>46</td></tr>

What function do I need to be using instead of key($item) to get the 4722?

 Answers

2

Try this:

foreach($samplearr as $key => $item){
  print "<tr><td>" 
      . $key 
      . "</td><td>"  
      . $item['value1'] 
      . "</td><td>" 
      . $item['value2'] 
      . "</td></tr>";
}
Friday, September 9, 2022
3
foreach( $codes as $code and $names as $name ) { }

That is not valid.

You probably want something like this...

foreach( $codes as $index => $code ) {
   echo '<option value="' . $code . '">' . $names[$index] . '</option>';
}

Alternatively, it'd be much easier to make the codes the key of your $names array...

$names = array(
   'tn' => 'Tunisia',
   'us' => 'United States',
   ...
);
Wednesday, August 31, 2022
2

This should work for you:

Just array_reduce() your array into a one dimensional array.

$result = array_reduce($arr, "array_merge", [])

EDIT:

To get your result you have to rewrite your code a bit, so that you collect all values into one array by go throw all elements of $linkObjs with array_map().

$linkObjs = $html->find('h3.r a'); 
$i = count($linkObjs);
$result = array_map(function($v){
    return trim($v->plaintext);
}, $linkObjs);

print_r($result);
Thursday, September 1, 2022
 
1

unset it first in case it is already in the proper format, otherwise you will remove what you just defined:

foreach($array as $key => $value)
    {
        unset($array[$key]);
        $array[ucfirst($key)] = $value;
    }
Thursday, August 11, 2022
 
kchetan
 
4

If the FOR LOOP is not mandatory (and I don't see this requirement in the quoted text) you should use the filter method.

When you invoke filter on an array you get a new array containing only the values that do respect the closure you passed to filter. The original array is not mutated.

struct Circle {
    let radius: Double
}

let circles = [Circle(radius: 1), Circle(radius: 5.1), Circle(radius: 4), Circle(radius: 10.8)]

let bigCircles = circles.filter { $0.radius > 5 }

Why this approach is better than mutating the array in a FOR LOOP

  1. Since circles is a constant, you don't have problems related to multithreading programming. If circles was mutable then other threads could change it while you are looping it with very scary side effects.
  2. It's less error prone. You are not writing what the CPU should do, instead you are describing how the results should be. So less potential misunderstandings between you and the compiler :)
  3. You are writing less code which does mean less potential mistakes.

These are some of the benefits of writing Functional Programming code.

Wednesday, October 12, 2022
 
tee
 
tee
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :