You are viewing our Forum Archives. To view or take place in current topics click here.
Making this multid array an associative multid array
Posted:

Making this multid array an associative multid arrayPosted:

Orry
  • Ladder Climber
Status: Offline
Joined: Mar 01, 201113Year Member
Posts: 358
Reputation Power: 24
Status: Offline
Joined: Mar 01, 201113Year Member
Posts: 358
Reputation Power: 24
Hello everyone. i have this multidimensional array that has 3 names and 3 dates that go with the names. I have a working code that i thought was an associative array, but recently been told its not associate, but just multidimen. So could somebody please help me out in making this associative.


$win = array('Name'=>
                        array('Jane Doe ', 'Nash Patel ', 'Joe Public '),
             'Date'=>
                        array('7 October 2015 ', '14 October 2014 ', '12 October 2016 '));

foreach($win as $element => $namedate) {
    echo '<strong>' . $element . '</strong><br>';
    foreach($namedate as $both) {
       echo $both . '<br/>';
    }
}
#2. Posted:
Cyimking
  • V5 Launch
Status: Offline
Joined: May 02, 201212Year Member
Posts: 1,129
Reputation Power: 34
Status: Offline
Joined: May 02, 201212Year Member
Posts: 1,129
Reputation Power: 34
"Associative" = Arrays with named keys (W3 Schools).

So your data structure will be the following:

$win = [
    'Jane Doe' => '7 October 2015',
    'Nash Patel' => '14 October 2014',
    'Joe Public' => '12 October 2016'
];


Now you're foreach loop:

foreach($win as $key => $entree) {
    echo '<strong>' . $key . '</strong> - ' . $entree . PHP_EOL;
}


If you want a person to have multiple dates:

// Data Structure
$win = [
    'Jane Doe' => ['7 October 2015', '14 October 2014', '12 October 2016'],
    'Nash Patel' => ['7 October 2016', '14 October 2016', '8 October 2016'],
    'Joe Public' => ['7 October 2017', '14 October 2017', '12 October 2017'],
];


// implode() is a very useful function!
foreach($win as $key => $entree) {
    echo '<strong>' . $key . '</strong>: ' . implode(' , ', $entree) . PHP_EOL;
}
#3. Posted:
Orry
  • Ladder Climber
Status: Offline
Joined: Mar 01, 201113Year Member
Posts: 358
Reputation Power: 24
Status: Offline
Joined: Mar 01, 201113Year Member
Posts: 358
Reputation Power: 24
Cyimking wrote "Associative" = Arrays with named keys (W3 Schools).

So your data structure will be the following:

$win = [
    'Jane Doe' => '7 October 2015',
    'Nash Patel' => '14 October 2014',
    'Joe Public' => '12 October 2016'
];


Now you're foreach loop:

foreach($win as $key => $entree) {
    echo '<strong>' . $key . '</strong> - ' . $entree . PHP_EOL;
}


If you want a person to have multiple dates:

// Data Structure
$win = [
    'Jane Doe' => ['7 October 2015', '14 October 2014', '12 October 2016'],
    'Nash Patel' => ['7 October 2016', '14 October 2016', '8 October 2016'],
    'Joe Public' => ['7 October 2017', '14 October 2017', '12 October 2017'],
];


// implode() is a very useful function!
foreach($win as $key => $entree) {
    echo '<strong>' . $key . '</strong>: ' . implode(' , ', $entree) . PHP_EOL;
}

Thanks bro, i appreciate this. Do you know how I can sort these and output them using printr(). I have this code snippet but im not sure how to get it to work with your code you supplied. Wondering if you could give me a hand? Like to get the names in ascending order then print the output, and separately getting the dates to be in ascending order and printing separate to the name?

foreach($win as $c=>$key) {
        $sort_date[] = $key['Date'];
        $sort_name[] = $key['Name'];
    }

    array_multisort($sort_name, SORT_ASC, $sort_date, SORT_ASC, $win);
    print_r($win);
#4. Posted:
Cyimking
  • TTG Senior
Status: Offline
Joined: May 02, 201212Year Member
Posts: 1,129
Reputation Power: 34
Status: Offline
Joined: May 02, 201212Year Member
Posts: 1,129
Reputation Power: 34
Yea no problem.

If you want to sort the key ("name"), then just use ksort($win) then loop over the array to print them.

So:

$win = [
    'Jane Doe' => '7 October 2015',
    'Nash Patel' => '14 October 2014',
    'Joe Public' => '12 October 2016'
];

ksort($win);

foreach($win as $key => $entree) {
    echo '<strong>' . $key . '</strong> - ' . $entree . PHP_EOL;
}


For multiple dates, you have a few options... Either, sort the dates before adding them into the $win array, or use usort with a callback function to sort the dates.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.