Jump to content

php array help


Recommended Posts

Dear reader,

 

On my own server I created the following code that is working perfectly:

 

 

$items = [];
foreach ($rows as $row)
{
$items[] = [
"item" => $row["item"],
"brand" => $row["brand"],
"number" => $row["number"],
"price" => $row["price"],
"start" => $row["start"],
"end" => $row["end"],
"shop" => $row["shop"],
"location" => $row["location"],
"link" => $row["link"]
];
}
However my hosting party is not supporting php 5.4 and therefor I transformed the code in:
$items = array();
foreach ($rows as $row)
{
$items = array("item" => $row["item"],
"brand" => $row["brand"],
"number" => $row["number"],
"price" => $row["price"],
"start" => $row["start"],
"end" => $row["end"],
"shop" => $row["shop"],
"location" => $row["location"],
"link" => $row["link"]
);
}
and now I am getting the error msgs: "Uninitialized string offset: 0".
Does anyone knows what I am doing wrong?
Thanks inadvance for your help!!!
Edited by bwouters
Link to post
Share on other sites

On thing I notices is that you still need the square backets here in order to add elements to the array:

$items[] = array(

 

As for the uninitialized string offset error, check and make sure that $row isn't a string.

  • Like 1
Link to post
Share on other sites

The answer:

 

 

$items = array();
foreach ($rows as $row)
{
$items[] = array("item" => $row["item"],
"brand" => $row["brand"],
"number" => $row["number"],
"price" => $row["price"],
"start" => $row["start"],
"end" => $row["end"],
"shop" => $row["shop"],
"location" => $row["location"],
"link" => $row["link"]
);
}
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...