total noob Posted January 25, 2012 Share Posted January 25, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255757-help-with-this-array/ Share on other sites More sharing options...
scootstah Posted January 25, 2012 Share Posted January 25, 2012 They don't have numerical indexes so [1] isn't going to work. If you do a print_r($keyws) you should see all the indexes you can use. Quote Link to comment https://forums.phpfreaks.com/topic/255757-help-with-this-array/#findComment-1311065 Share on other sites More sharing options...
AyKay47 Posted January 25, 2012 Share Posted January 25, 2012 a nested foreach loop will work for displaying the values of the internal array. foreach($qarray2 as $mnb => $keyws){ foreach($keyws as $val) { echo $val . "<br />"; } } Quote Link to comment https://forums.phpfreaks.com/topic/255757-help-with-this-array/#findComment-1311075 Share on other sites More sharing options...
total noob Posted January 25, 2012 Author Share Posted January 25, 2012 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... Quote Link to comment https://forums.phpfreaks.com/topic/255757-help-with-this-array/#findComment-1311082 Share on other sites More sharing options...
total noob Posted January 25, 2012 Author Share Posted January 25, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/255757-help-with-this-array/#findComment-1311084 Share on other sites More sharing options...
Drongo_III Posted January 25, 2012 Share Posted January 25, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255757-help-with-this-array/#findComment-1311085 Share on other sites More sharing options...
total noob Posted January 25, 2012 Author Share Posted January 25, 2012 Thanks Drongo_III However i cant change the array anymore.. That array result was the result after parsing an XML string.. I have a hard time on getting the data on the array.. Im hoping that someone could solve this problem.. thanks guys.. Quote Link to comment https://forums.phpfreaks.com/topic/255757-help-with-this-array/#findComment-1311090 Share on other sites More sharing options...
AyKay47 Posted January 25, 2012 Share Posted January 25, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255757-help-with-this-array/#findComment-1311131 Share on other sites More sharing options...
total noob Posted January 26, 2012 Author Share Posted January 26, 2012 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.. Quote Link to comment https://forums.phpfreaks.com/topic/255757-help-with-this-array/#findComment-1311335 Share on other sites More sharing options...
phpSensei Posted January 26, 2012 Share Posted January 26, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/255757-help-with-this-array/#findComment-1311354 Share on other sites More sharing options...
total noob Posted January 26, 2012 Author Share Posted January 26, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/255757-help-with-this-array/#findComment-1311357 Share on other sites More sharing options...
AyKay47 Posted January 26, 2012 Share Posted January 26, 2012 something like: <?php foreach($qarray2 as $mnb => $keyws){ echo $keyws["ID"]; } ?> should work, if it doesn't, there might be an error else where in your code? Quote Link to comment https://forums.phpfreaks.com/topic/255757-help-with-this-array/#findComment-1311368 Share on other sites More sharing options...
total noob Posted January 26, 2012 Author Share Posted January 26, 2012 something like: <?php foreach($qarray2 as $mnb => $keyws){ echo $keyws["ID"]; } ?> should work, if it doesn't, there might be an error else where in your code? This code does not work sir.. I think the error is cause by my array.. Quote Link to comment https://forums.phpfreaks.com/topic/255757-help-with-this-array/#findComment-1311373 Share on other sites More sharing options...
AyKay47 Posted January 26, 2012 Share Posted January 26, 2012 perhaps if you post the relavant code so that we do not have to guess where $qarray2 is coming from. Quote Link to comment https://forums.phpfreaks.com/topic/255757-help-with-this-array/#findComment-1311377 Share on other sites More sharing options...
total noob Posted January 26, 2012 Author Share Posted January 26, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/255757-help-with-this-array/#findComment-1311384 Share on other sites More sharing options...
AyKay47 Posted January 26, 2012 Share Posted January 26, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/255757-help-with-this-array/#findComment-1311387 Share on other sites More sharing options...
total noob Posted January 26, 2012 Author Share Posted January 26, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/255757-help-with-this-array/#findComment-1311392 Share on other sites More sharing options...
total noob Posted January 26, 2012 Author Share Posted January 26, 2012 Based on your code sir <?php <?php /*$myAray is equal to the arrays above*/ $qarray2 = $myAray['GetSMSInboundResponse']['SMSInbounds']['InboundSMS']; foreach($qarray2 as $key => $mnb){ echo "$key = $mnb"; } ?> ?> could i echo just 1 element on my array? thanks Quote Link to comment https://forums.phpfreaks.com/topic/255757-help-with-this-array/#findComment-1311394 Share on other sites More sharing options...
AyKay47 Posted January 26, 2012 Share Posted January 26, 2012 <?php /*$myAray is equal to the arrays above*/ $qarray2 = $myAray['GetSMSInboundResponse']['SMSInbounds']['InboundSMS']; foreach($qarray2 as $key => $mnb){ if($key == "ID") echo "$key = $mnb"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/255757-help-with-this-array/#findComment-1311396 Share on other sites More sharing options...
total noob Posted January 26, 2012 Author Share Posted January 26, 2012 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/255757-help-with-this-array/#findComment-1311401 Share on other sites More sharing options...
AyKay47 Posted January 26, 2012 Share Posted January 26, 2012 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/255757-help-with-this-array/#findComment-1311403 Share on other sites More sharing options...
Adam Posted January 27, 2012 Share Posted January 27, 2012 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/255757-help-with-this-array/#findComment-1311693 Share on other sites More sharing options...
total noob Posted January 27, 2012 Author Share Posted January 27, 2012 Thanks AyKay47! Quote Link to comment https://forums.phpfreaks.com/topic/255757-help-with-this-array/#findComment-1311701 Share on other sites More sharing options...
phpSensei Posted January 28, 2012 Share Posted January 28, 2012 I dont get it, your taking a value grabbed from an Array ($qarray2) and then for looping it or does it contain another array? Quote Link to comment https://forums.phpfreaks.com/topic/255757-help-with-this-array/#findComment-1311887 Share on other sites More sharing options...
AyKay47 Posted January 28, 2012 Share Posted January 28, 2012 I dont get it, your taking a value grabbed from an Array ($qarray2) and then for looping it or does it contain another array? it contains another array. Quote Link to comment https://forums.phpfreaks.com/topic/255757-help-with-this-array/#findComment-1311976 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.