-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.

ramayeah
  • 39
  • 12

3 Answers3

2

Try this:

foreach ($response->Schedule['DateMarket']['Journeys'] as $key=>$value) {
    $value['Name'] = $value[1];
}
John Bupit
  • 10,406
  • 8
  • 39
  • 75
  • You mean that you're still getting the error `Attempt to assign property of non-object on line xXx`? – John Bupit Dec 08 '14 at 08:51
  • 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 – ramayeah Dec 08 '14 at 08:54
  • 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](http://stackoverflow.com/q/5450148/1492578) – John Bupit Dec 08 '14 at 08:58
  • 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. – ramayeah Dec 08 '14 at 09:01
  • 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. – Ecter Dec 08 '14 at 09:30
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

Ecter
  • 176
  • 2
  • 6
  • the first solution worked. but the second solution not working. Thanks. but is there any short syntax? – ramayeah Dec 08 '14 at 09:05
  • I edited my answer with even better solution instead of not-working one. Give it a try. – Ecter Dec 08 '14 at 09:28
  • 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. – ramayeah Dec 08 '14 at 09:42
  • 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 )` – Ecter Dec 08 '14 at 10:24
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];
}
pavel
  • 26,538
  • 10
  • 45
  • 61