Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7571379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:39:41+00:00 2026-05-30T15:39:41+00:00

I wanna create and store graph in php. I have bus schedule, so I

  • 0

I wanna create and store graph in php. I have bus schedule, so I decided to create 2 classes:

class Vertex 
{
  public $city_id;
  public $time;
}

class Edge
{
  public routeId;
  public end_vertex;
}

after this I’m trying to fill my graph. It should be something like hashtable where key will be Vertex object and it’ll have many edges.

prototype example:

foreach ($data as $route)
{
  $v = new Vertex($route->startCity, $route->startTime)
  if(!graph[$v]) {
    graph[$v] = [];
  }

  graph[$v].add(new Edge($route->routeId, new Vertex($route->city_id, $route->startTime + $route->arrivalTime)));
}

but there is one really big problem, as I understand object cannot be used as array key! Maybe I’m in a wrong way? How to create graphs correctly in php? I’m a newbie in this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:39:42+00:00Added an answer on May 30, 2026 at 3:39 pm

    In PHP, only simple types can be used as array indices. Complex types, like arrays, objects and resources do not work properly.

    Edit: Oh, if memory serves me right, you should watch out for booleans as well, I seem to recollect an issue I had with them.

    Edit2: In your case, the object graph should be pointing at the objects, not an array.

    So, for example, your code would look like:

    $v = new Vertex();
    
    $v->add(new Edge());
    
    $vertices[] = $v;
    

    Edit3: I noticed some serious syntactic flaws in your code. I don’t know the exact reason, but if you really can’t get them straight, I would advice that you give the PHP manual a couple of looks.

    Edit4: By the way, you are using an object as an array index, not a class. There is no PHP data type for classes, there is only class names, which are plain strings.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanna create query by primary key. Supposed I have class primary key, PersonKey,
I wanna create Contextual Tab using Webpart In Sandbox Solution..that time I am getting
I have create several canvas with transparent background and wanna make some move event
I have a C# library with the following Namespace/Class: namespace Helper { public static
i've got 2 tables: Seat(roomID,seatID,...) SeatState(roomID,seatID,date,state) i wanna create a seat class and i
i wanna ask again,still have problems with my php code i wanna ask how
I wanna create a single instance of a class. How can I create a
I have a question but dont have a answer, i wanna create a application
ı wanna create xml file on aspx.cs but ı have no idea what ı
I wanna create an app that will have three view controllers. To navigate between

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.