Solved

Chunking and Assi=ociative array into smaller arrays.

Posted on 2011-09-22
7
425 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

627 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