1

Good day, I am trying to convert an array "$list" into string or object. I have used following methods:

<?php

include "medclass.php";

session_start();
if (isset($_SESSION['mail'])) 
{
    $list = $_SESSION['basket'];
}
else
header("location: clientsigninpage.php?msg= Log-in First");

$obj = new med_class;
$obj->connectdb();

$val = implode(";",$list);    //implode method
$val = (object) $list;        //object method
$val = serialize($list);      //serialize method

$result = $obj->searchMed($val);

while ($row = $result->fetchObject()) 
{
  echo $row->MedPrice;
}

?>

With "(object)" its giving me following error: "Object of class stdClass could not be converted to string", with "implode": "Array to string conversion" and with "serialize()" it does not print anything.

The function that I am passing value is:

function searchMed($v1)
    {
        $sql = "select * from storepreview where MedName = '$v1'";
        $ret = $this->con->query($sql);
        return $ret;
    }

I have used these methods by seen following links: (http://www.dyn-web.com/php/arrays/convert.php) ; (Convert an array to a string); (How to convert an array to object in PHP?)

M A S S
  • 23
  • 5
  • 2
    So what do you want from us? – u_mulder May 25 '18 at 07:42
  • You can store array in your $_SESSION['basket'], it would save you some work and its safer. So you can have something like [0 => ['name' => Some med, 'count' => 2]], so you can do $obj->searchMed($list[0]['name'] or you can loop through every item in basket by using foreach... – Eakethet May 25 '18 at 07:43
  • @u_mulder, As I said earlier, I am trying to convert my array into string or object. Is there any other method or way to do it? Because the ones I have tried wont help. – M A S S May 25 '18 at 07:55
  • @Eakethet Yes I have stored my array in $_SESSION['basket'] look: `//from: array_push($_SESSION['basket'], array($_GET['id']));` to `$_SESSION['basket'] = array();` – M A S S May 25 '18 at 07:57
  • You've already converted `$list` to string with `implode`. What else do you want? – u_mulder May 25 '18 at 07:58
  • @MASS your code is quite messy, looks like you are doing something else than you want. With your searchMed method, you need to search for every item in basket, you cant make it string, it wont find anything (i think that you expect it will return everything) So you need to loop foreach($_SESSION['basket'] as $item){ $obj->searchMed($item); } and you need to rewrite search method, so it will look for id and not name... – Eakethet May 25 '18 at 08:07
  • @u_mulder, As I mentioned earlier, the "implode" method is doing me no good as it gives me an error; "Array to string conversion". – M A S S May 25 '18 at 08:19
  • @Eakethet Sorry for that messy code of mine. As for the searchMed method, it is giving me all the data, I know it because I am also using this function to search medicine for admin. What I am trying to do is I want to convert my "$list" array to string or object before sending it to searchMed($value) function and when the array is converted it would look for the related medicine name as it is also unique and return me that specific data. I can use medicine ID instead of medicine name, no problem there. – M A S S May 25 '18 at 08:26

2 Answers2

0

You can use json_encode to convert Array to String:

$FINAL_VALUE = json_encode($YOUR_OBJECT);

For more information, you can refer this link.

Nirmalsinh Rathod
  • 5,079
  • 4
  • 26
  • 56
  • Yes, thank you that was useful but the thing is I am using pure **php** code so, that is why I am having trouble with converting the array to string. I don't know why the "implode" or the "(object)" method wont work for me. – M A S S May 25 '18 at 09:56
0

I managed to reproduce your "Array to string conversion" error when using the implode command by running the following line of code:

implode(";", [[]]); // PHP Notice:  Array to string conversion in php shell code on line 1

For converting a nested array into a string I found that a foreach loop worked:

$nestedArray = ['outerKeyOne' => ['innerKeyOne' => 'valueOne'], 'outerKeyTwo' => ['innerKeyTwo' => 'valueTwo']];
$arrayOfStrings = [];
foreach ($nestedArray as $key => $value) {
    $arrayOfStrings[] = implode(",", $value);
}
implode(";", $arrayOfStrings); // string(17) "valueOne;valueTwo"

The second error associated with the line $val = (object) $list; is from trying to embed an object into the $sql string. It seems like an object is not what you want here, unless it is an object that has a __toString() method implemented.

I hope this is of some help. Using var_dump or something similar would provide more debug output to better diagnose the problems along with the above error messages. That's how I came up with the above code.

GigaAdam
  • 36
  • 6