14

I need to create an array using a object using different format/structure

I have:

$t = object()
$t > user = object()
$t > user > 0 (object) name = 'wilson';
$t > user > 0 (object) first = 'carl';

I need to get:

$t = array(
 name = wilson
 first name = phil

Here's what I tried and where I'm stuck

foreach($t as $a) { 
      foreach($a as $l) {
          $arr[$l->0->name] = $l->0->first; // line 10
      }
  }
  print_r($arr);

Now I get an error:

PHP Parse error: syntax error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE or '{' or '$' in homework1-a-1.php on line 10

What can I do to fix it?

8
  • Looks like a simple syntax error. Can't see line 10 so no idea. Commented Dec 30, 2011 at 21:21
  • i put line 10 in my edit Commented Dec 30, 2011 at 21:22
  • object properties must start with an character. However, I don't understand, what you want to achieve, because it seems, that you mixed up objects and arrays a little bit. Commented Dec 30, 2011 at 21:23
  • 2
    Can you do a print_r($t); and paste the output? Commented Dec 30, 2011 at 21:24
  • Is it part of the assignment that you need to construct the object as well? If so, check that you've done everything right with constructing it before you go to the second part where you convert it to array. Commented Dec 30, 2011 at 21:24

2 Answers 2

18

Your question is confusing. This is what I understand:

You have the following:

  • $t, which is an object.
  • $t->user, also an object.
  • $t->user[0]->name = 'wilson'
  • $t->user[0]->first = 'carl'

You say you need to get:

  • $t->name = 'wilson'
  • $t->first = 'carl'

You say 'phil' in the question, but the given object $t has no reference to a 'phil' so I don't know if 'phil' appears out of thin air, or what.

Is this a correct view of the problem? If so, you need to clarify this in the question. saying $t > user > 0 (object) name makes no sense.

Sorry this is an "answer", I just couldn't fit all of this in a comment. I will delete it if you clarify the question. Hopefully I am not the only person confused by this.

Sign up to request clarification or add additional context in comments.

Comments

0
$t = object()
$t > user = object()
$t > user > 0 (object) name = 'wilson';
$t > user > 0 (object) first = 'carl';

This is not valid PHP code. If you want $t in the format you show, just do this:

$t = array(
 'name' => 'wilson',
 'first name' => 'phil'
);

Now $t is an array, with keys 'name' and 'first name'.

Your foreach loop doesn't make any sense. $t is an array of two strings. Looping over it will just give the values 'wilson' and 'phil'.

EDIT: Assuming $t was given to you, then your for loop should look like this:

foreach($t as $a) { 
      foreach($a as $l) {
          $arr[$l->{0}->name] = $l->{0}->first;
      }
  }

You can't do $l->0. You need to wrap the 0 in {}. $l->{0}.

2 Comments

The point is it's a homework assignment, the object is given, build an array from it.
@Truth: Sorry, the question was a little confusing. The "code" for $t makes no sense, I have no idea what $t looks like. He then says he wants $t in a certain format (which you said was given), then in his loop, he references $arr. So, I'm not sure what he wants.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.