Jump to content

Help with this array


total noob

Recommended Posts

Hello

 

Can someone help me with this array?

<?php 
Array 
( 
[inboundSMS] => Array 
( 
	[iD] => 3135765 
	[Originator] => +447537404702 
	[Destination] => +447537404702 
	[Keyword] => UCB2 
	[Date] => 2012-01-25 
	[Time] => 12:09:08 
	[body] => UCB2 this is a test "sms", with commas.. Test onlt for UCB Function 
) 
)
?>

Array ( [inboundSMS] => Array ( [iD] => 3135765 [Originator] => +447537404702 [Destination] => +447537404702 [Keyword] => UCB2 [Date] => 2012-01-25 [Time] => 12:09:08 [body] => UCB2 this is a test "sms", with commas.. Test onlt for UCB Function ) )

 

Ive been using a foreach loop to each the array results but it wont echo any result

<?php
foreach($qarray2 as $mnb => $keyws){
	echo $keyws[1];
}
?>

 

I would like to echo the array results but im having a hard time on it.. can someone help me on this?

 

Any help will be appreciated..

 

thanks guys.

Link to comment
Share on other sites

This is the original array..

<?php
Array 
( 
[GetSMSInboundResponse] => Array 
( 
	[Transaction] => Array 
	( 
		[Code] => 1 
		[Description] => Transaction OK 
	) 
	[sMSInbounds] => Array 
	( 
		[inboundSMS] => Array 
		( 
			[iD] => 3135765 
			[Originator] => +447537404702 
			[Destination] => +447537404702 
			[Keyword] => UCB2 
			[Date] => 2012-01-25 
			[Time] => 12:09:08 
			[body] => UCB2 this is a test "sms", with commas.. Test onlt for UCB Function 
		) 
	) 
)
) 
?>

 

I simplified the above array hoping to get an easier array by doing this.

<?php 	
$qarray2 = $myAray['GetSMSInboundResponse']['SMSInbounds']['InboundSMS'];
foreach($qarray2 as $keys){
	echo $keys;
}
?>

 

However if i echo the array using this

<?php 	
$qarray2 = $myAray['GetSMSInboundResponse']['SMSInbounds']['InboundSMS'];
foreach($qarray2 as $keys){
	echo $keys[1];
}
?>

or

<?php 	
$qarray2 = $myAray['GetSMSInboundResponse']['SMSInbounds']['InboundSMS'];
foreach($qarray2 as $keys){
	echo $keys['Body'];
}
?>

 

I cant get any results.. I would like to echo each data on a newline but im stuck on how to do it... Can someone help me?

 

Thanks guys..

- thanks scootstah...

 

 

Link to comment
Share on other sites

Just cos i'd written it lol...


<?php 
$test = Array 
( 
'InboundSMS' => Array 
( 
	'ID' => '3135765', 
	'Originator' => '+447537404702 ',
	'Destination' => '+447537404702 ',
	'Keyword' => 'UCB2' ,
	'Date' => '2012-01-25' ,
	'Time' => '12:09:08' ,
	'Body' => 'UCB2 this is a test "sms", with commas.. Test onlt for UCB Function '
) 
);

echo $test['InboundSMS']['ID'];

foreach($test['InboundSMS'] as $key => $value){

echo $key . " " . $value;

}

?>


 

 

Hello

 

Can someone help me with this array?

<?php 
Array 
( 
[inboundSMS] => Array 
( 
	[iD] => 3135765 
	[Originator] => +447537404702 
	[Destination] => +447537404702 
	[Keyword] => UCB2 
	[Date] => 2012-01-25 
	[Time] => 12:09:08 
	[body] => UCB2 this is a test "sms", with commas.. Test onlt for UCB Function 
) 
)
?>

Array ( [inboundSMS] => Array ( [iD] => 3135765 [Originator] => +447537404702 [Destination] => +447537404702 [Keyword] => UCB2 [Date] => 2012-01-25 [Time] => 12:09:08 [body] => UCB2 this is a test "sms", with commas.. Test onlt for UCB Function ) )

 

Ive been using a foreach loop to each the array results but it wont echo any result

<?php
foreach($qarray2 as $mnb => $keyws){
	echo $keyws[1];
}
?>

 

I would like to echo the array results but im having a hard time on it.. can someone help me on this?

 

Any help will be appreciated..

 

thanks guys.

Link to comment
Share on other sites

Wow great!

 

thanks AyKay47!

 

Im just wondering.. Why i cant get the correct data by using this?

 

<?php
foreach($qarray2 as $mnb => $keyws){
	echo $val[0]. '<br/>' .$val[1];
}
?>

 

this is the result..

3

1+

4+

4U

C2

01

2U

C

 

Does some have any ideas?

 

well, since you have named array keys, you must access them by their name, using integers will not work, if your array keys were the default integers, then your code would work.

Link to comment
Share on other sites

Wow great!

 

thanks AyKay47!

 

Im just wondering.. Why i cant get the correct data by using this?

 

<?php
foreach($qarray2 as $mnb => $keyws){
	echo $val[0]. '<br/>' .$val[1];
}
?>

 

this is the result..

3

1+

4+

4U

C2

01

2U

C

 

Does some have any ideas?

 

well, since you have named array keys, you must access them by their name, using integers will not work, if your array keys were the default integers, then your code would work.

 

If i used a name i.e. echo $val['ID'] by getting the results of the array it wont echo any results... Im just curios where did it went wrong..

Link to comment
Share on other sites

Wow great!

 

thanks AyKay47!

 

Im just wondering.. Why i cant get the correct data by using this?

 

<?php
foreach($qarray2 as $mnb => $keyws){
	echo $val[0]. '<br/>' .$val[1];
}
?>

 

this is the result..

3

1+

4+

4U

C2

01

2U

C

 

Does some have any ideas?

 

well, since you have named array keys, you must access them by their name, using integers will not work, if your array keys were the default integers, then your code would work.

 

If i used a name i.e. echo $val['ID'] by getting the results of the array it wont echo any results... Im just curios where did it went wrong..

 

 

because $val isn't defined in your code. You Mean $keyws in your case

Link to comment
Share on other sites

Wow great!

 

thanks AyKay47!

 

Im just wondering.. Why i cant get the correct data by using this?

 

<?php
foreach($qarray2 as $mnb => $keyws){
	echo $val[0]. '<br/>' .$val[1];
}
?>

 

this is the result..

3

1+

4+

4U

C2

01

2U

C

 

Does some have any ideas?

 

well, since you have named array keys, you must access them by their name, using integers will not work, if your array keys were the default integers, then your code would work.

 

If i used a name i.e. echo $val['ID'] by getting the results of the array it wont echo any results... Im just curios where did it went wrong..

 

 

because $val isn't defined in your code. You Mean $keyws in your case

 

Im so sorry that's not $val it was $keyws... Do you have any ideas why it cant view any results? all i see is a white page..

 

thanks

Link to comment
Share on other sites

perhaps if you post the relavant code so that we do not have to guess where $qarray2 is coming from.

 

This is my two arrays sir..

<?php

/*First array*/

Array 
( 
	[GetSMSInboundResponse] => Array 
		( 
			[Transaction] => Array 
				( 
					[Code] => 1 
					[Description] => Transaction OK 
				) 
				[sMSInbounds] => Array 
					(	 
						[inboundSMS] => Array 
						( 
							[0] => Array 
								( 
									[iD] => 3135899 
									[Originator] => +447537404702 
									[Destination] => +447537404702 
									[Keyword] => UCB2 
									[Date] => 2012-01-25 
									[Time] => 12:20:24 
									[body] => UCB2 test "SMS", with comma and qoutes for UCB.. 
								) 
							[1] => Array 
								( 
									[iD] => 3135903 
									[Originator] => +447537404702 
									[Destination] => +447537404702 
									[Keyword] => UCB2 
									[Date] => 2012-01-25 
									[Time] => 12:20:24 
									[body] => UCB2 test "SMS", with comma and qoutes for UCB.. the second message... 
								) 
						) 
					) 
		) 
)

/*Second array*/

Array
( 
	[GetSMSInboundResponse] => Array 
		( 
			[Transaction] => Array 
				( 
					[Code] => 1 
					[Description] => Transaction OK 
				) 
				[sMSInbounds] => Array 
					( 
						[inboundSMS] => Array 
						( 
							[iD] => 3135765 
							[Originator] => +447537404702 
							[Destination] => +447537404702 
							[Keyword] => UCB2 
							[Date] => 2012-01-25 
							[Time] => 12:09:08 
							[body] => UCB2 this is a test "sms", with commas.. Test onlt for UCB Function 
						) 
					) 
		) 
) 
?>

 

the two arrays are a separate array.. Now below is my code for viewing the arrays

 

<?php
/*$myAray is equal to the arrays above*/
$qarray2 = $myAray['GetSMSInboundResponse']['SMSInbounds']['InboundSMS'];
foreach($qarray2 as $mnb){
		$q1 =  $mnb['ID'];
}
?>

 

Now the code above works on my first array but im having trouble of getting the correct output for the second array..  Could you help me sir?

 

thanks in advance

Link to comment
Share on other sites

for the second array it would be:

 

<?php
/*$myAray is equal to the arrays above*/
$qarray2 = $myAray['GetSMSInboundResponse']['SMSInbounds']['InboundSMS'];
foreach($qarray2 as $key => $mnb){
		echo "$key = $mnb";
}
?>

 

are you accessing each array separately or together?

Link to comment
Share on other sites

for the second array it would be:

 

<?php
/*$myAray is equal to the arrays above*/
$qarray2 = $myAray['GetSMSInboundResponse']['SMSInbounds']['InboundSMS'];
foreach($qarray2 as $key => $mnb){
		echo "$key = $mnb";
}
?>

 

are you accessing each array separately or together?

 

Im accessing each array separately sir however i would like to do it only in one foreach loop thats applicable to both arrays.. Is this possible sir?

 

thanks

Link to comment
Share on other sites

<?php
/*$myAray is equal to the arrays above*/
$qarray2 = $myAray['GetSMSInboundResponse']['SMSInbounds']['InboundSMS'];
foreach($qarray2 as $key => $mnb){
if($key == "ID")
   echo "$key = $mnb";
}
?>

 

Last question sir what if

<?php
/*$myAray is equal to the arrays above*/
$qarray2 = $myAray['GetSMSInboundResponse']['SMSInbounds']['InboundSMS'];
foreach($qarray2 as $key => $mnb){
if($key == "ID" &&  $key == "Originator")
   $id = "$key = $mnb";
   $originator = "$key = $mnb";
}
?>

 

Is the code above works? Im not in the house today so i can't test my code. Later i will test it..

 

thanks

Link to comment
Share on other sites

no that code will not work, a key cannot equal two values at once, you will need to change the and operator to an or operator, however if you want to store specific values in separate variables, two if statements are needed, and the logic is a little off.

 

<?php
/*$myAray is equal to the arrays above*/
$qarray2 = $myAray['GetSMSInboundResponse']['SMSInbounds']['InboundSMS'];
foreach($qarray2 as $key => $mnb){
if($key == "ID")
   $id = $mnb;
if($key == "Originator")
   $originator = $mnb;
}
?>

Link to comment
Share on other sites

<?php
/*$myAray is equal to the arrays above*/
$qarray2 = $myAray['GetSMSInboundResponse']['SMSInbounds']['InboundSMS'];
foreach($qarray2 as $key => $mnb){
if($key == "ID")
   $id = $mnb;
if($key == "Originator")
   $originator = $mnb;
}
?>

 

What is this??

 

$qarray2 = $myAray['GetSMSInboundResponse']['SMSInbounds']['InboundSMS'];

// $qarray2['ID'] contains the ID
// $qarray2['Originator'] contains the originator

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.