i have an array i and i want to show the array values if the name of same array repeat in the another array and have true
value
my arrays like this
$array1 = [ array( 'name' => internal_evidence 'price' => 30 'course_id' => 3 ), array( 'name' => international_evidence 'price' => 450 'course_id' => 3 ), array( 'name' => internal_evidence 'price' => 10 'course_id' => 1 ), array( 'name' => technical_evidence 'price' => 134 'course_id' => 3 ), ]; $array2 = [ array( 'id' => 3 'name' => graphic 'price' => 150 'attr' => array( 'internal_evidence' => 'true', 'international_evidence' => 'false', 'technical_evidence' => 'true' ) ), array( 'id' => 5 'name' => 3dmax 'price' => 300 'attr' => array( ) ), array( 'id' => 1 'name' => ICDL 'price' => 480 'attr' => array( 'internal_evidence' => 'true', ) ), ];
i want to showing this all attr selected with true value in like this
also I want to sum price of array2
member and array1
<h2>graphic</h2> <p>internal_evidence</p> <p>technical_evidence</p> <small>course price: 150</small> <small>314</small> <!-- Price with selected evidence --> <h2>3dmax</h2> <small>course price: 300</small> <!-- its not have attr evidence --> <h2>ICDL</h2> <p>internal_evidence</p> <small>course price: 480</small> <small>490</small> <!-- Price with selected evidence -->
i try this but its don`t work properly
$priceOfAttr = 0; foreach($array2 as $key => $cat): echo "<h2>{$cat['name']}</h2>"; foreach($array1 as $pr): if($pr['course_id'] == $cat['id']): foreach($cat['attr'] as $m => $optionV): if($m == $pr['name'] && $optionV == "true"){ echo $m .'<br>'; $priceOfAttr += $pr['price']; // echo "<small>{$cat['price']}</small><br>"; // echo $cat['price'] + $pr['price']. "<br>"; } endforeach; echo $priceOfAttr + $cat['price'] . '<br>'; endif; endforeach; echo '<br>'; endforeach;
Advertisement
Answer
I’d use a combination array_reduce
and array_map
to transform your data into what you need, then simply loop over that to display your view:
<?php // Index your $array1 by [id][name] $array1ByIdAndName = array_reduce($array1, static function ($byIdAndName, $entry) { $byIdAndName[$entry['course_id']][$entry['name']] = $entry; return $byIdAndName; }); // Transform $array2's `attr` entries into attribute list + compute total price $array2 = array_map(static function ($entry) use ($array1ByIdAndName) { $entry['total_price'] = $entry['price']; $entry['attr'] = array_reduce(array_keys($entry['attr']), static function ($attrs, $attrName) use ($array1ByIdAndName, &$entry) { if ($entry['attr'][$attrName] === 'true') { $attrs[] = $attrName; $entry['total_price'] += $array1ByIdAndName[$entry['id']][$attrName]['price']; } return $attrs; }, []); return $entry; }, $array2); // Display your view ?> <?php foreach ($array2 as $entry): ?> <h2><?= $entry['name'] ?></h2> <?php foreach ($entry['attr'] as $attrName): ?> <p><?= $attrName ?></p> <?php endforeach ?> <small>course price : <?= $entry['price'] ?></small> <?php if ($entry['total_price'] > 0): ?> <small><?= $entry['total_price'] ?></small> <?php endif ?> <?php endforeach ?>
Demo: https://3v4l.org/nS3Gl