PHP Compare 2 Arrays - array to string conversion error - php

foreach($this->galleryPhotoDBInitialArray as $initialArrayElement) {
$photoExists = false;
foreach($this->galleryPhotoDBFinalArray as $finalArrayElement) {
if($initialArrayElement == $finalArrayElement) {
$photoExists = true;
echo $initialArrayElement;
}
}
if(!$photoExists){
echo 'delete............................................';
echo $initialArrayElement;
}
}
I have the above code. I know the arrays have data as I can print_r() it and see the data. the Array data looks like this:
What do I need to do so I can access the $initialArrayElement value - I need to process this further but I get the PHP error 'array to string conversion error'...
thankyou very much :)

echo $initialArrayElement;
use print_r on this and check if this is still an array.. because at what i observe this was still an array..

Please use
print_r($initialArrayElement); insted of echo $initialArrayElement;
that will work.

Related

PHP Array inside of an array issue

I'm having some problems getting the data through my PHP loop.
<?php
$url = 'https://www.fibalivestats.com/data/1653309/data.json';
$content = file_get_contents($url);
$json = json_decode($content, true);
?>
<?php
foreach($json['totallds']['sPoints'] as $item); {
echo $item;
}
?>
The error I'm getting is an array to string conversion error. What I'm trying to get is the data from the sPoints array that will give me a Top 5 points scorers for a basketball game.
I'll build a table for this in HTML later but for now, it's not displaying the data at all and I'm getting errors. I feel like I may have confused arrays and strings too. Any thoughts about what I'm doing wrong? JSON file can be found in the $url variable.
Also if it helps, here's the link to where I have gotten the data from and what context the Top 5 is from https://www.fibalivestats.com/u/NSS/1653309/lds.html
Thanks!
Your $item is an array, so you can't just echo it like that. You can, however, echo its columns, for example:
foreach($json['totallds']['sPoints'] as $item) {
echo $item['firstName'] . ' ' . $item['familyName'];
}
Notice the removed semicolon between the foreach () and {.
Well, array to string conversion error means you're trying to echo an array.
If you see the return of the url you are looking at, you can verify that the "sPoints" key returns an array with several objects.
Try to change echo to print_r or var_dump to view entire data or complete your business logic.
Try changing:
echo $item;
to:
var_dump($item);

array_push strings which comes from json_encode for sql insertion

Hello I would like to store string which comes from json encode to get it store in MySQL, but the thing is that the array returned nothing. Here is my code:
$com = array("1-1","1-2","1-3");
$classcom=array();
for ($i=1; $i<(int)$totaleleve; $i++) {
$eleve=array();
foreach($com as $value)
{
array_push($eleve,'2');
}
$final="'".json_encode($eleve)."'";
array_push($classcom,$final);
echo $final;
echo $classcom;
}
echo $classcom;
For your information, $totaleleve='20', and I do $final="'".json_encode($eleve)."'"; so that I can concentrate strings of the array to be able to insert it into the SQL statement, which looks something like this:
$sql="INSERT INTO table VALUES (".explode(",", $classcom).")"
// so that it looks like something like this:
$sql="INSERT INTO table VALUES ('["2","2","2"]','["2","2","2"]','["2","2","2"]')
the $final gave me '["2","2","2"]' which i wanted, but when i do array_push($classcom,$final);, it just gave me a blank array :
Array
Can someone help me, please? Thank you!
If $totaleleve is greater than 0 the array is not empty. Just use print_r or var_dump to show its values.
print_r($classcom)
Instead of echo $classcom
Just write print_r($classcom); instead of echo $classcom; and you will see that your array isn't empty.

Function for reverse order of values in an array php

I'm new to PHP and I was asked to write a function that accepts an array as a parameter and then prints the array in reverse order. Here is what I have so far:
<?php
function RevOrder ($arr1) {
$arr1 == array();
echo array_reverse($arr1);
}
RevOrder (array(1,4,2,5,19,11,28));
?>
It is supposed to output 28, 11, 19, 5, 2, 4, 1 but I keep getting an array to string conversion error.
echo expects a string while you are passing an array, hence the array to string conversion error. It also looks like you are not properly checking if the param passed is an array. Try this:
<?php
function RevOrder($arr1) {
if (is_array($arr1)) {
return array_reverse($arr1);
}
return false;
}
$reversedArray = RevOrder(array(1,4,2,5,19,11,28));
// Option 1
print_r($reversedArray);
// Option 2
echo(implode(', ', $reversedArray));
<?php
function RevOrder (array $arr1) {
echo implode(", ", array_reverse($arr1));
}
RevOrder (array(1,4,2,5,19,11,28));
But note that this isn't particularly good design - your functions should do one thing. In this case you should instead write a function to print an array according to your liking and then pass it reversed array. Although in this case I guess it's ok to have a helper function for printing the array in reversed order but when you're doing something more complicated you should consider this.
EDIT:
You could do something like this:
function printArray(array $arr){
echo implode(", ", $arr);
}
printArray(array_reverse($arr));
As for why you can't just echo array see this
Arrays are always converted to the string "Array"; because of this,
echo and print can not by themselves show the contents of an array. To
view a single element, use a construction such as echo $arr['foo'].
See below for tips on viewing the entire contents.
Also I added type-hints for array so that when you pass something that's not an array you get an error.

Passing multiple variable for php page

Hey guys I need help on passing multiple values for my PHP
http://s596.beta.photobucket.com/user/kingbookal/media/Capture.png.html?sort=3&o=0
That's the code from the 1st page and for the next page it is
$year = $_GET['yearlevel'];
I tried to alert the value but its null.
Please guide me well..
https://www.dropbox.com/sh/fokl2hnrjtpsfbn/Rcm8EfApm1
This is the link for my scripts
Do you want to echo the value from a key inside an array? Then you do like this:
echo $rowProfName['professor_name'];
If it doesn't return anything, you could check if a key exists in the array by using the following code:
if(!isset($rowProfName['professor_name']))
{
echo "Array key 'professor_name' is not set.";
}
If you want to check what your array contains, simply run this code:
$yourArray=array("value1","value2");
print_r($yourArray);
//If you want clean output as HTML, use this:
echo "<pre>";
print_r($yourArray);
echo "</pre>";
I hope I understood your question.

Last comma in JSON Array to android

I am having a problem with the last comma when I am using PHP echoing JSON Array to android
Here is my code
If ($commentResult>0)
echo "[";
{
while ($row = mysql_fetch_array($commentResult)) {
echo json_encode($row).",";
}
echo "]";
Android can't read this, it printed out JSONException:Vale at 3 is null
Why are you trying to re-invent the wheel? If you want to give an entire array then put json_encode over the entire array instead of trying to manually build it.
$comments=array();
if($commentResult>0){
while($row=mysql_fetch_array($commentResult)){
$comments[]=$row;
}
}
echo json_encode($comments);
*Also, a side tip, don't use mysql_ functions. Instead use PDO or mysqli, which are better supported and get you rid of this whole while($row) business.*
The way you form you json is really strange. Don't know if it's an issue but you can try it in a more clean way:
if ($commentResult) {
for ($data = array(); $row = mysql_fetch_array($commentResult); $data[] = $row);
echo json_encode($data);
}
What ever JSON you are generating using PHP, Just verify that in http://jsonlint.com/. if output is wrong it wont validate other wise its OK from PHP side.

Categories