Solved

Chunking and Assi=ociative array into smaller arrays.

Posted on 2011-09-22
7
401 Views
Last Modified: 2012-05-12
OK I have an multidimensional array $arrLists:-

AStatus
            For Sale
                        ID          125
                        Other       99
            Hold Sale
                        ID          56
                        Other       12
            Not for Sale
                        ID          22
                        Other       32
Base
            Canvas
                        ID          326
                        Other       99
            Cloth
                        ID          17
                        Other       9

Open in new window


I want to split it into two smaller arrays $arrAStatus:-
For Sale
            ID          125
            Other       99
Hold Sale
            ID          56
            Other       12
Not for Sale
            ID          22
            Other       32

Open in new window


and $arrBase:-

Canvas
            ID          326
            Other       99
Cloth
            ID          17
            Other       9

Open in new window


How do I achieve this?
0
Comment
Question by:cescentman
  • 3
  • 3
7 Comments
 
LVL 9

Expert Comment

by:ghodder
ID: 36585313
$arrAStatus = $arrLists['AStatus'];
$arrBase = $arrLists['Base'];

Open in new window

0
 
LVL 1

Author Comment

by:cescentman
ID: 36585431
Sorry I can see now that I have been less that specific. Indeed what you suggest would work but I want to loop through $arrLists and create each array automatically based on the key.
0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
ID: 36586089
Try this

<?php
    $a = Array('AStatus' => Array('For Sale' => Array('ID' => 125, 'Other' => 99),
                                    'Hold Sale' => Array('ID' => 56, 'Other' => 12),
                                    'Not for Sale' => Array('ID' => 22, 'Other' => 32)
                                   ),
                'Base' => Array('Canvas' => Array('ID' => 326, 'Other' => 99),
                                 'Cloth' => Array('ID' => 17, 'Other' => 9)
                                )
               );
?>

    <pre>
    <?php print_r($a); ?>
    </pre>
    
<?php
    foreach($a as $key => $val){
        
        $vn = "arr".$key;
        $nvn = $$vn = $val;
        
        echo"<pre>";
        print_r($nvn);
        echo"</pre>";
    }
?>

Open in new window

   
0
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 1

Author Closing Comment

by:cescentman
ID: 36586113
Thanks that's brilliant.

I'm not familiar with the syntax:-

$nvn = $$vn = $val;

Can you enlightne me?
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 36586134
Sure,

// this below makes the array name based on the key,
// so there are two arrays arrAStatus, arrBase
// vn - variable name
$vn = "arr".$key;

// nvn - new variable name
// adding $ to $ makes a variables variable, because the $vn is the string
// for example "arrBase", so adding an extra $ will produce a variable named $arrBase
// and assign the $val to it, which in this case is the array below
$nvn = $$vn = $val;

Hope it's clear
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 36586138
in other words:

$newVariable = $("arrAStatus"/"arrBase") = the array for the proper key
0
 
LVL 1

Author Comment

by:cescentman
ID: 36592076
Brilliant thanks. Learnt something new
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question