0

Sorry if using wrong terms below, hopefully you understand what I mean. Also, please note this is just a learning task and not actually used.

I have some JSON data which has been imported and decoded into an array. The array is about 20 elements of different products for a webshop which has multiple values (id,name,price,stock,category,discount etc). I want to print a list of all the items based on name. However, one of the elements does not have a name. The "name":"" is completely missing in the JSON data, therefore in my array too. So, it's not NULL, it just doesn't exist.

I have tried various of if functions (isset, !empty) and put usort and foreach inside the if loop, but I believe those I've tried demand that the certain value name actually exists but is not set and not that it is not there at all.

Code example to print list:

echo "<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Price</th>
        </tr>
    </thead>
    <tbody>";
                        
            usort($product_data, fn($a,$b) => strcmp($a->name, $b->name) );
            foreach($product_data as $product) { 
            echo "<tr>";
            echo "<td>". $product->name ."</td>";
            echo "<td>". $product->price ."</td>";
            }
        }
            echo"</td>
            </tr>
    </tbody>
    </table>";

I get this error no matter what I do: Notice: Undefined property: stdClass::$name in [filename]

I'm thinking there must be a quick way to say "if 'name' exists do this (otherwise nothing)" but cannot find it at all. Please help!

emmab
  • 5
  • 1
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – 04FS Sep 24 '20 at 11:18
  • First, execute your code without the "echo "". $product->name ."";" line. I think you will have the same error but with $price, because the problem is you dont call the fields properly. Or try $product.name. – MR Mark II Sep 24 '20 at 11:20
  • So, this `echo isset($product->name)? $product->name : '';` is not working? – Manish Pareek Sep 24 '20 at 11:22
  • Not sure how to call the fields correctly tbh. I do not get the same error with price, so fairly certain it's related to name not existing. $product.name does not work, "Fatal error: Uncaught Error: Object of class stdClass could not be converted to string in C:\xampp\htdocs\index.php:140 Stack trace: #0 {main} thrown in C:\xampp\htdocs\index.php". Could however be related to my JSON work. `$json_data = file_get_contents('./testapi.txt');` `$response = json_decode($json_data);` `$product_data = $response->products;` – emmab Sep 24 '20 at 12:09
  • Err, should isset be used here or somewhere else? `echo "".isset($product->name)? $product->name : ''."";` If here, does not work. – emmab Sep 24 '20 at 12:18

2 Answers2

1

What about $product->name ?? '';

It's like isset($product->name) ? $product->name : '';

Mark
  • 11
  • 2
0

solution may be

<pre>if(property_exists($product, 'name')){
 //put here your td
 echo "<td>". $product->name ."</td>";

}</pre>

use property_exists(object,attribtue betwen quotation) for checking if each attribute that you are fetching exist

Ammar Dje
  • 80
  • 1
  • 10
  • Thanks, seems to work fine if I put this inside the foreach loop. However, I believe I have the same issue with my usort (same error in output, however 4 times so might be something else too). This is ofc still the same since that occurs before the loop. Any tip how I can exclude the attribute in usort too? Since the $product object is declared in the loop, I believe the same piece of code cannot be used before it, correct? – emmab Sep 24 '20 at 11:44
  • well here it's another problem you need to reconstruct your object so assign your old object to a new object with a foreach and if you find a missing attribute put it empty in the new one – Ammar Dje Sep 25 '20 at 06:32