How can two arrays created in the same way become different? - php

The var_dumps of the two arrays are different but they were created the exact same way . Why is this?????
first array dump:
array(2) {
[0]=>
array(0) {
}
[1]=>
string(6) ",2,2,1"
}
second array dump:
array(3) {
[0]=>
array(0) {
}
[1]=>
string(1) "2"
[2]=>
string(1) "2"
[3]=>
string(1) "1"
}
array 1 is made on fileA.php
while ($row = mysql_fetch_assoc($res))
{
$iState[] = $row["state"];
}///end while
then i use ajax to send to fileB.php
js_array=<? echo json_encode($iState); ?>;
var url_js_array=js_array.join(',');
xmlhttp.open("GET","fileB.php?istate="+ url_js_array,true);
xmlhttp.send();
then in fileB.php(ajax response file) i retrieve the array
$iStateValues[] =$_GET["istate"] ;
then I create array 2 on fileB.php
while ($row = mysql_fetch_array($res))
{
$currentiState[]= $row["state"];
}///end while
then I compred the two
echo"\n\nsame test\n";
if($iStateValues==$currentiState)
echo "same";
else
echo "not same";

The problem:
Currently, you're sending the comma-separated string to fileB.php.
$iStateValues[] = $_GET["istate"] ;
$_GET["istate"] contains the comma-separated string, and in the above statement, you're pushing the value into an empty array $iStateValues.
Now, in fileB.php, you create another array $currentiState using a while loop. Then you try to compare them.
$iStateValues is a string, not an array. $currentiState is a real array. So the comparison will always return FALSE.
How to fix the issue:
Instead of sending the comma-separated string to fileB.php, send the actual JSON string. So your code will look like:
js_str=<? echo json_encode($iState); ?>;
xmlhttp.open("GET","fileB.php?istate="+ js_str,true);
xmlhttp.send();
Now, in fileB.php, you can receive the JSON string and then decode it, like so:
$jsonArray = json_decode($_GET['istate'], true);
(The second parameter in JSON decode says it should be decoded to an associative array instead of an object).
Then do the comparison.

Related

php- get associative array values

pls, i would like to get the values of the $aa variable, i'm using the mysqli_fetch_all because all the values need to be used in another layer.
Thanks
$aa = mysqli_fetch_all($ttt,MYSQLI_ASSOC);
Output with var_dump($aa):
array(2) { [0]=> array(1) { ["followe"]=> string(8) "bammyww " } [1]=> array(1) { ["followe"]=> string(5) "demo " } }
i have tried using $aa['followe'] , but i'm getting invalid index error.
Just loop through it. It's an array containing associative arrays.
foreach($aa as $item)
{
$item['followe'] // do something with this.
}
Instead of $aa['followe'], try:
$aa[0]['followe'];
as its a multi-dimension array. And the right approach to get all the array element is using foreach() like:
foreach($aa as $item)
{
$item['followe']
}
use
$aa[0]['followe'];
$aa[0] is array(1) { ["followe"]=> string(8) "bammyww " }
$aa[0]['followe'] is string(8) "bammyww "
You can also use array_column as
array_column($aa,'followe');//retrieves values associated with the key followe

Accessing array with nested objects

I have an api wrapper i am using that returns something like this
object(stdClass)#7 (1) {
[0]=>
object(stdClass)#6 (2) {
["contactId"]=>
string(2) "nV"
["email"]=>
string(31) "email#domain.com"
}
}
how do i access the email part with PHP
Cast your API returned data to an array.
For example you are saving API returned data in $result variable. Cast it to an array.
$arrayResult = (array) $result;
echo $arrayResult[0]->email;
Try this.

How to resolve Cannot use object of type stdClass as array error in php

Hello friends here is my code
foreach ($pageList as $page) {
echo var_dump($page);
}
this is my output using var_dump
array(1) { [0]=> object(stdClass)#41 (2) { ["access_token"]=> string(182) "long string goes here" ["id"]=> string(15) "849929535061042" } }
I want to get access token and id in the form of variable like $page['access_token']=$pageaccesstoken;. How can i do that from that output.
You should use like that
`foreach ($pageList as $page) {
$arr = json_encode($page,true);
$arr1 = json_decode($arr,true);
//print_r($arr1);
$pageaccesstoken=#$arr1[0]['access_token'];
$id=#$arr1[0]['id'];`
to access page id and access token.
#CollinD was almost correct, you do need to use an "arrow operator", but you should do that on the 0th element of the array.
Let's look at your var_dump output:
array(1) { [0]=> object(stdClass)#41 (2) { ... } }
What this means is that your $page is actually an array of size one, and the 0th element is the object of stdClass, and it is that class that has two fields.
The fields are named access_token and id. In order to access access_token you would write:
$page[0]->access_token;

parse a json array inside a json array in php

I am having a json object $result
I do this:
$json = json_decode($result, true);
Here is the output if I use this:
var_dump($json)
is this:
array(15) { ["id"]=> int(1) ["name"]=> array(16) { ... } }
If I do this:
echo $json['id'];
echo $json['name'];
The id is printed correctly : 1
But in the name this is printed: Array
How can I get that array and print it?
Several ways:
print_r($json['name']);
var_dump($json['name']);
Or manual with preferred delimiter:
echo implode(", ", $json['name']);
However you should check the function responsible for making that JSON string, because you expect a string instead of array.

How extract data from $_SESSION created array

In the first HTML + PHP page I use the following command:
$_SESSION['myarray'] = $rows;
where $rows is an array.
In the next page I am trying to retrieve data from the array:
$dhoondo = $_SESSION['myarray'];
foreach ($dhoondo as list($ntag, $strTitle))
{
echo $ntag, $strTitle;
}
But it shows "Array" instead of values, whereas Var_dump($dhoondo); clearly displays the data, even $arrlength=count($dhoondo); shows the exact number of items.
What's wrong?
The
var_dump($dhoondo);
result is array(20) { [0]=> array(13) { ["ISBN"]=> string(13) "9780849329500" ["TTAG"]=> string(5) "20752" ["TITLE"]=> string(76) "CRC HANDBOOK OF HIGH RESOLUTION INFRARED .......
Just use:
$dhoondo = $_SESSION['myarray'];
foreach ($dhoondo as $row)
{
echo $row["TTAG"]." - ".$row["TITLE"];
}

Categories