-1

I have an array from json_decode. And i want to reformat it. this is my array format.

["Schedule"]=>array(1) {
["Origin"]=>
string(3) "LAX"
["Destination"]=>
string(2) "CGK"
["DateMarket"]=>
array(2) {
  ["DepartDate"]=>
  string(19) "2015-02-01T00:00:00"
  ["Journeys"]=>
  array(6) {
    [0]=>
    array(6) {
      [0]=>
      string(2) "3210"
      [1]=>
      string(14) "Plane Name"
      [2]=>
      string(8) "20150201"
      [3]=>
      string(8) "20150201"
      [4]=>
      string(4) "0815"
      [5]=>
      string(4) "1524"
    }
  }
}

And i want change the indexed array to associative with foreach function. And here is my PHP code

foreach ($response->Schedule['DateMarket']['Journeys'] as $key=>$value) {
    $value->Name= $value[1];
}

But i got an error "Attempt to assign property of non-object on line xXx.. My Question is, how to insert a new associative array to indexed array like the example that i've provide.

UPDATE : I've tried this solution

foreach ($response->Schedule['DateMarket']['Journeys'] as $key=>$value) {
    $value['Name']=$value[1];
}

But my array format still the same, no error.

4
  • You should either put reference on $value or mutate the array e.g. $response->Schedule['DateMarket']['Journeys'][$key]['Name'] = $value[1] Commented Dec 8, 2014 at 9:15
  • put reference, how? sorry,im still trying to learn php Commented Dec 8, 2014 at 9:25
  • This is on the first several lines of the foreach documentation php.net/manual/en/control-structures.foreach.php Commented Dec 8, 2014 at 9:27
  • okay, thanks. i've tried with a reference, and its worked too. Commented Dec 8, 2014 at 9:40

3 Answers 3

2

In this line:

$value->Name= $value[1];

You expect $value to be both object ($value->Name) and array ($value[1]).

Change it to something like:

foreach ($response->Schedule['DateMarket']['Journeys'] as $key=>$value) {
    $response->Schedule['DateMarket']['Journeys'][$key]['Name'] = $value[1];
}

Or even better, without foreach:

$keys = array(
    0 => 'Id',
    1 => 'Name',
    2 => 'DateStart',
    3 => 'DateEnd',
    4 => 'HourStart',
    5 => 'HourEnd',
);
$values = $response->Schedule['DateMarket']['Journeys'];
$response->Schedule['DateMarket']['Journeys'] = array_combine( $keys , $values );

Array_combine makes an array using keys from one input and alues from the other. Docs: http://php.net/manual/en/function.array-combine.php

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

4 Comments

the first solution worked. but the second solution not working. Thanks. but is there any short syntax?
I edited my answer with even better solution instead of not-working one. Give it a try.
thanks for the answer. your first solution is working. but there is a new variable that have value from some old value. not only rename the indexes.
Try that one with array_combine. I'm pretty sure it will achieve what you wanted. It will convert your array to something like this: Array ( [Id] => 3210 [Name] => Plane Name [DateStart] => 20150201 [DateEnd] => 20150201 [HourStart] => 0815 [HourEnd] => 1524 )
2

Try this:

foreach ($response->Schedule['DateMarket']['Journeys'] as $key=>$value) {
    $value['Name'] = $value[1];
}

5 Comments

You mean that you're still getting the error Attempt to assign property of non-object on line xXx?
no, the error didnt show up, but the array still the same like the original array (indexed array). the $value['Name] = $value[1]; didnt show up on my array
The code just adds the associative key value pairs - it doesn't remove anything. You'll need a separate loop to remove the old indexes. See this question
i know, my first question is how to add new associative key. i cant remove the old indexes yet until i can create the new associative array with the old value from the old indexes.
It doesn't work because it only affects temporary variable $value. It makes no assignments to $response->Schedule so you won't see any change in it.
1

You want to create new array index, but try to create new object.

foreach ($response->Schedule['DateMarket']['Journeys'] as $key => $value) {
    $value['Name'] = $value[1];
}

1 Comment

Ofcourse, it will not show up, because you are not touching the reference

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.