How to receive an array and print your content using PHP?

Asked

Viewed 14,275 times

0

I need to know how to store data from an array in a variable, using the looping below:

Data collection:

$sql="SELECT `devicetoken` FROM `devicetokensios`  ORDER BY `index`";

  $resultado = mysql_query($sql) or die ("Erro .:" . mysql_error());

$deviceToken = array();

  while($r = mysql_fetch_assoc($resultado))
{
    $deviceToken [] = $r;
}
mysql_close();

After receiving the data, how do I go through a looping and have control of the array data using its index:

Example of how to use array data:

for($index = 0; $index <= count($deviceToken); $index ++){

$outraVarial = $deviceToken[$index];

}
  • 2

    Can you better explain what you want with "use this same array to print the data on the screen"? Note that $deviceTokens exists in your code with and without "s" at the end...

  • 1

    And why do you need to pass one array to another array? Is there any real reason or you just don’t know how to use the array original*. The most modern technique for sweeping a array existing is the foreach. There inside you do what you want with each element.

  • You would like to show each position of the array or want to print the array all at once?

  • I just want to return the contents of the :D array

  • You want to return or print the $deviceTokens? How do you need this data from the array? What is their finality?

  • I need them in string format. It will return the Devices tokens to push in the APNS. But now I’m using the implode() function and I’m getting an error saying : Warning: implode(): Argument must be an array in

  • I found the error, I was using the <= signal in the looping, so it accessed a value above what was in the array. But I’m still worrying about the guy... I’m gonna do some checking.

Show 2 more comments

2 answers

3


Disregarding the syntax error pointed out by Sergio and considering the requirement for the array to be printed as a string, you have, without iterating, at least three options but all followed by an echo or print:

  1. implode()

    <?php echo implode( '', $deviceTokens );
    

The implode() problem is that it does not work with associative array so if the array has string indexes, they will be ignored and the output will not be as expected.

  1. serialize()

    <?php echo serialize( $devideTokens );
    

The problem of serializing is the rigidity of the resulting information. Once you have serialized an array you cannot do anything useful without de-serializing it. Hence it is a more suitable format for storing data that does not require normalization.

  1. json_encode()

    <?php echo json_encode( $deviceTokens );
    

The most flexible of the options because for being universal can be manipulated even by other languages.

And you have the option to iterate, if all else fails:

$output = NULL;

foreach( $deviceTokens as $key => $value ) {

    // É óbvio que você não vai fazer assim :p

    $output . 'Chave: ' . $key . "\nValor: " . $value;
}

echo $output;
  • i would need an input control. To control it with a for(); For example: ;// Como preciso&#xA;&#xA;for ($index = 0; $index<= count($array);$index++){&#xA; &#xA;&#xA; $valorRecebido = $array[$index];&#xA;&#xA;}

  • That’s another question. You wanted to know how to show this array. Although it’s a much simpler question than database tampering, I answered. If it is still the same question, edit the question and provide more details. I edit mine as much as possible to answer.

0

Okay, I got what I wanted!

Thank you.

Below the code:

$meuArray = array();

$meuArray []= "A";
$meuArray []= "k";
$meuArray []= "Blan";


for($lop = 0; $lop < count($meuArray);$lop++){

echo ($meuArray[$lop]);


}
  • 1

    You know, considering this structure, a simple echo implode( '', $meuArray ) would already solve your problem, right?

  • Yeah, you did, but I didn’t solve everything. I’ll explain, I’m trying to send a devicetoken to a push notification server, however, I need to do this for hundreds of Vices, so to automate, I’m trying to create a small PHP system for this. I can already manually trigger a push. And I can already run the system I’m doing, but with the system the push’s are not enough. On screen comes the success message, but in practice nothing happens.

  • I am using the $minhaString = implode($meuArray);.

  • I will start another question more focused on the new problems that are emerging. But Thank you!!!

  • If the true array and the array above are both indexed (numeric) and one-dimensional the problem is elsewhere. I think it’s worth one var_dump() on the real array to investigate. Suddenly there is an extra dimension that may be ending implode(). But even so, even more that seems to me to be having interaction with another language (right JS?), JSON is even more indicated.

  • JSON appears in the middle of the project, but does not interact with this array. I’m going to start another question and I’m going to show you the code in full and explain the scenario. I’ll add the link here so you can follow.

  • Guys, here’s the new question with the full code: http://answall.com/questions/40583/como-construir-um-simples-server-apns-comphp

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.