Jump to content

Getting the data in this array..


total noob

Recommended Posts

Hello all..

 

I have here my array results.. does anyone knows how to simplify this array? i would like to get the following value and echo it...

 

Array 
( 
[GetSMSInboundResponse] => Array 
	( 
		[Transaction] => Array 
			( 
				[Code] => 1 
				[Description] => Transaction OK 
			) 
		[sMSInbounds] => Array 
			( 
				[inboundSMS] => Array 
					( 
						[0] => Array 
							( 
								[iD] => 3126861 
								[Originator] => +447537404702 
								[Destination] => +447537404702 
								[Keyword] => UCB2 
								[Date] => 2012-01-24 
								[Time] => 13:21:45 
								[body] => UCB2 this is a "quotes", single quote... Test for receiving a message having commas, double quotes and single quote... 
							) 
						[1] => Array 
							( 
								[iD] => 3126867 
								[Originator] => +447537404702 
								[Destination] => +447537404702 
								[Keyword] => UCB2 
								[Date] => 2012-01-24 
								[Time] => 13:21:45 
								[body] => UCB2 this is a "quotes", single quote... Test for receiving a message having commas, double quotes and single quote 2... 
							) 
					) 
			) 
	) 
) 

 

I would like to echo the result on this part of array

[0] => Array 
( 
	[iD] => 3126861 
	[Originator] => +447537404702 
	[Destination] => +447537404702 
	[Keyword] => UCB2 
	[Date] => 2012-01-24 
	[Time] => 13:21:45 
	[body] => UCB2 this is a "quotes", single quote... Test for receiving a message having commas, double quotes and single quote... 
) 
[1] => Array 
( 
	[iD] => 3126867 
	[Originator] => +447537404702 
	[Destination] => +447537404702 
	[Keyword] => UCB2 
	[Date] => 2012-01-24 
	[Time] => 13:21:45 
	[body] => UCB2 this is a "quotes", single quote... Test for receiving a message having commas, double quotes and single quote 2... 
) 

 

but i have problems on how to do it.. please help me guys..

 

thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/255676-getting-the-data-in-this-array/
Share on other sites

I'm not exactly sure if you printed that array, but you would usually do this.

<?php

$array[0]['name'] = "John";
$array[0]['age'] = 22;
$array[0]['sex'] ="male";

$array[1]['name'] = "Alice";
$array[1]['age'] = 20;
$array[1]['sex'] ="female";


echo $array[0]['age']; //Will print out 22
echo $array[1]['name']; //Will print out Alice
echo $array[1]['sex']; //Will print out female

 

This is actually an array.. This array is the result after parsing an XML string.. Below is the function that returns the array

 

<?php function xml2array($contents, $get_attributes=1, $priority = 'tag') {
    if(!$contents) return array();

    if(!function_exists('xml_parser_create')) {
       
        return array();
    }

   
    $parser = xml_parser_create('');
    xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); 
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
    xml_parse_into_struct($parser, trim($contents), $xml_values);
    xml_parser_free($parser);

    if(!$xml_values) return;

    
    $xml_array = array();
    $parents = array();
    $opened_tags = array();
    $arr = array();

    $current = &$xml_array; 

    $repeated_tag_index = array();
    foreach($xml_values as $data) {
        unset($attributes,$value);

        
        $result = array();
        $attributes_data = array();
        
        if(isset($value)) {
            if($priority == 'tag') $result = $value;
            else $result['value'] = $value; 
        }


        if(isset($attributes) and $get_attributes) {
            foreach($attributes as $attr => $val) {
                if($priority == 'tag') $attributes_data[$attr] = $val;
                else $result['attr'][$attr] = $val; 
            }
        }


        if($type == "open") {
            $parent[$level-1] = &$current;
            if(!is_array($current) or (!in_array($tag, array_keys($current)))) { 
                $current[$tag] = $result;
                if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
                $repeated_tag_index[$tag.'_'.$level] = 1;

                $current = &$current[$tag];

            } else { 

                if(isset($current[$tag][0])) {
                    $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
                    $repeated_tag_index[$tag.'_'.$level]++;
                } else {
                    $current[$tag] = array($current[$tag],$result);
                    $repeated_tag_index[$tag.'_'.$level] = 2;
                    
                    if(isset($current[$tag.'_attr'])) { 
                        $current[$tag]['0_attr'] = $current[$tag.'_attr'];
                        unset($current[$tag.'_attr']);
                    }

                }
                $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
                $current = &$current[$tag][$last_item_index];
            }

        } elseif($type == "complete") { 
         
            if(!isset($current[$tag])) { 
                $current[$tag] = $result;
                $repeated_tag_index[$tag.'_'.$level] = 1;
                if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;

            } else { 
                if(isset($current[$tag][0]) and is_array($current[$tag])) {

                  
                    $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
                    
                    if($priority == 'tag' and $get_attributes and $attributes_data) {
                        $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
                    }
                    $repeated_tag_index[$tag.'_'.$level]++;

                } else { /
                    $current[$tag] = array($current[$tag],$result);
                    $repeated_tag_index[$tag.'_'.$level] = 1;
                    if($priority == 'tag' and $get_attributes) {
                        if(isset($current[$tag.'_attr'])) { 
                            
                            $current[$tag]['0_attr'] = $current[$tag.'_attr'];
                            unset($current[$tag.'_attr']);
                        }
                        
                        if($attributes_data) {
                            $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
                        }
                    }
                    $repeated_tag_index[$tag.'_'.$level]++;
                }
            }

        } elseif($type == 'close') { 
            $current = &$parent[$level-1];
        }
    }
    
    return($xml_array);
} 
$XMLS = '	
<?xml  version="1.0"  encoding="UTF-8"?><GetSMSInboundResponse><Transaction><Code>1</Code><Description>Transaction  OK</Description></Transaction><SMSInbounds><InboundSMS><ID>3126861</ID><Originator>+447537404702</Originator><Destination>+447537404702</Destination><Keyword>UCB2</Keyword><Date>2012-01-24</Date><Time>13:21:45</Time><Body>UCB2  this  is  a  "quotes",  single  quote...  Test  for  receiving  a  message  having  commas,  double  quotes  and  single  quote...</Body></InboundSMS><InboundSMS><ID>3126867</ID><Originator>+447537404702</Originator><Destination>+447537404702</Destination><Keyword>UCB2</Keyword><Date>2012-01-24</Date><Time>13:21:45</Time><Body>UCB2  this  is  a  "quotes",  single  quote...  Test  for  receiving  a  message  having  commas,  double  quotes  and  single  quote  2...  </Body></InboundSMS></SMSInbounds></GetSMSInboundResponse> ';

print_r(xml2array($XMLS, $get_attributes=1, $priority = 'tag'));
?>

 

Do you have any ideas on how to extract the information that i want?

 

thanks guys..

To get the inner array:

 

$innerArray = $array['GetSMSInboundResponse']['SMSInbounds']['InboundSMS'];

 

To echo it, outside a loop:

echo $innerArray[0]['ID'];

 

If you want it in a loop to echo everything:

foreach ($innerArray as $item) {
    echo $item['ID'] . PHP_EOL;
}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.