?
Solved

Array of arrays

Posted on 2009-07-13
11
Medium Priority
?
347 Views
Last Modified: 2013-12-13
hi all,
i have an array called $results; if i print its values(using print_r($results)) i get the values bellow.
My problem is i need to get the value of key, its an array within an array. Does anyone know what im supposed to do.
Array ( [AATSMS] => Array ( [0] => 0 [1] => 2 ) [SUBMITRESULT] => Array ( [0] => 1 ) ) Vals array Array ( [0] => Array ( [tag] => AATSMS [type] => open [level] => 1 ) [1] => Array ( [tag] => SUBMITRESULT [type] => complete [level] => 2 [attributes] => Array ( [ACTION] => enqueued [KEY] => 17730529 [RESULT] => 1 [NUMBER] => 888888888 ) ) [2] => Array ( [tag] => AATSMS [type] => close [level] => 1 ) )
regards
0
Comment
Question by:Dawie de Villiers
[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
  • 6
  • 4
11 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24841461
Can you post that in the code snippet - it would be a lot easier to read!
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24841479
Better yet, print the arrays this way and post the output in the code snippet.  Thanks, ~Ray
echo "<pre>\n";
var_dump($array_thing);

Open in new window

0
 

Author Comment

by:Dawie de Villiers
ID: 24841568
thnaks, here is the results in code snippet
array(3) {
  [0]=>
  array(3) {
    ["tag"]=>
    string(6) "AATSMS"
    ["type"]=>
    string(4) "open"
    ["level"]=>
    int(1)
  }
  [1]=>
  array(4) {
    ["tag"]=>
    string(12) "SUBMITRESULT"
    ["type"]=>
    string(8) "complete"
    ["level"]=>
    int(2)
    ["attributes"]=>
    array(4) {
      ["ACTION"]=>
      string(8) "enqueued"
      ["KEY"]=>
      string(8) "17731279"
      ["RESULT"]=>
      string(1) "1"
      ["NUMBER"]=>
      string(9) "888888888"
    }
  }
  [2]=>
  array(3) {
    ["tag"]=>
    string(6) "AATSMS"
    ["type"]=>
    string(5) "close"
    ["level"]=>
    int(1)
  }
}
 
Vals array
Array
(
    [0] => Array
        (
            [tag] => AATSMS
            [type] => open
            [level] => 1
        )
 
    [1] => Array
        (
            [tag] => SUBMITRESULT
            [type] => complete
            [level] => 2
            [attributes] => Array
                (
                    [ACTION] => enqueued
                    [KEY] => 17731279
                    [RESULT] => 1
                    [NUMBER] => 888888888
                )
 
        )
 
    [2] => Array
        (
            [tag] => AATSMS
            [type] => close
            [level] => 1
        )
 
)
mykeyvalue=Arraymykeyvalue=Arraymykeyvalue=Array

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Accepted Solution

by:
JoachimMartinsen earned 2000 total points
ID: 24841728
This should do the trick:
$result[1]["attributes"]["KEY"];

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24842259
I think what JoachimMartinsen posted will do the trick on this set of arrays, but I am curious - this looks like something that was interpreted from an XML file.  In that case (or if the significance of the tag called SUBMITRESULT matters) you might want a more generalized solution rather than just having the numeric index [1].  If you want to tell us more about it, there may be an easy-to-use programmatic solution available.

Best regards, ~Ray
0
 

Author Comment

by:Dawie de Villiers
ID: 24847515
thats true ray, im reading an xml file and convert it into an array.
i might have problems in future since i didnt quite understand whats happening here.
but the solution  i got from JoachimMartinsen: works for the particular problem.
Is there a better way of reading xml data
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24849377
"Is there a better way of reading xml data" - maybe yes.  I like to use SimpleXML.  It makes the XML into an object and you can use OOP constructs and iterators to access the internals.  If you want to post a sample of the XML, I may be able to show you a way to get this information without the intermediate step of converting it into arrays.

Best regards, ~Ray
0
 

Author Comment

by:Dawie de Villiers
ID: 24953105
hi, here is the sample xml im reading:

<?xml version="1.0" ?>
- <aatsms>
<submitresult action="enqueued" key="3951103" result="1" />
</aatsms>
</span>
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24953312
The ending "span" tag is not part of the XML.  Try running this and see what you get.  Please post back here if you have any questions, ~Ray
<?php // RAY_temp_xml_27.php
error_reporting(E_ALL);
echo "<pre>\n";
 
// TEST DATA FROM THE POST AT EE
$xml = '<?xml version="1.0" ?>
<aatsms>
<submitresult action="enqueued" key="3951103" result="1" />
</aatsms>';
 
// MAKE AN OBJECT
$obj = SimpleXML_Load_String($xml);
 
// VISUALIZE THE OBJECT
var_dump($obj);
 
// ITERATE OVER THE OBJECT
foreach ($obj as $thing)
{
// EXTRACT EACH FIELD AND CAST IT AS A STRING VARIABLE
    $action = (string)$thing["action"];
    $key    = (string)$thing["key"];
    $result = (string)$thing["result"];
// SHOW THE VARIABLES WE FOUND
    echo "\nACTION=$action KEY=$key RESULT=$result";
}

Open in new window

0
 

Author Comment

by:Dawie de Villiers
ID: 25131338
thanks guys,
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 25167647
@dawie: Going forward, please remember that you can split the points if you feel that more than on expert contributed useful information to the solution.  Thanks, ~Ray
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.
Suggested Courses

770 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