Jump to content

php array help


bwouters

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 comment
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 comment
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 comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...