PHP Push same data on different key

163
May 05, 2018, at 6:57 PM

I've data as follows

<?php
    $cart = array(
        'c' => array(
            array(
                'Cart' => array('course_type_quantity' => 3),
                'c' => array(
                    'id' => 2152,
                    'name' => 'Product1',
                    'duration' => 5,
                    'image' => 'IMG-2152.png',
                    'exams' => '606,652',
                    'exam' => 0
                )
            ),
            array(
                'Cart' => array('course_type_quantity' => 3),
                'c' => array(
                    'id' => 2152,
                    'name' => 'Product2',
                    'duration' => 5,
                    'image' => 'IMG-2153.png',
                    'exams' => '607,653',
                    'exam' => 0
                )
            )
        )
    );
?>

Now I want data from above array like:

<?php
    $exams => array(
        606 => 'Product1',
        652 => 'Product1',
        607 => 'Product2',
        653 => 'Product2',
    );
?>

My Code

foreach($data['c'] as $r){
    if($r['c']['exam']==0){
        $id = explode(',',$r['c']['exams']);
        for($i=0;$i<(count($id));$i++){
            $data['exams'][$id[$i]] = $r['c']['name'];
            $ids[] = $id[$i];
        }
    }
}

Which is working without any error...

But I don't want the nested loop. Because this is just a piece of code for this functionality, I've written before and after it.

Please suggest any short code for this...

Thanks

Answer 1

You can use array_reduce to summarise your array. You can use array_combine and array_fill

$cart = array(...); //Your array
$exams = array_reduce($cart['c'], function($c, $v){
    $e = explode(',',$v['c']['exams']);
    return $c + array_combine($e,array_fill(0,count($e), $v['c']['name']));
}, array());

This will result:

Array
(
    [606] => Product1
    [652] => Product1
    [607] => Product2
    [653] => Product2
)
Rent Charter Buses Company
READ ALSO
static files in Django does not upload when I changes the app name

static files in Django does not upload when I changes the app name

I ran through weird issueI have project for website

185
Responsive Images with srcset

Responsive Images with srcset

I have been looking into improving my page load speeds for mobile devices and have been reading about the srcset method to create responsive imagesI think this would be very helpful as my site has a number of high quality images that increase the load-time

155
Need different functionality of slick slider

Need different functionality of slick slider

I am using slick slider in my websiteUsing the slick syncing slider which has a main image and below that thumbnails as navigation which keep on sliding

146
Scroll table when highlighted row is the last row visible in table body

Scroll table when highlighted row is the last row visible in table body

Suppose I have a table with 20 rowsBy default first row is highlighted and 6 rows are visible in the screen

120