-1

I am getting this array of std class object how can i call it in single array as you can see only some values are changed.

Array
(
    [0] => stdClass Object
        (
            [movie_id] => 4
            [movie_name] => Harvey
            [genre_id] => 4
            [genre_title] => Action/Adventure
            [rating_id] => 2
            [rating_title] => Pg-16 (USA)
            [person_id] => 14
            [person_name] => John Beck
            [role_title] => producer
        )

    [1] => stdClass Object
        (
            [movie_id] => 4
            [movie_name] => Harvey
            [genre_id] => 4
            [genre_title] => Action/Adventure
            [rating_id] => 2
            [rating_title] => Pg-16 (USA)
            [person_id] => 15
            [person_name] => Henry Koster
            [role_title] => directer
        )

    [2] => stdClass Object
        (
            [movie_id] => 4
            [movie_name] => Harvey
            [genre_id] => 4
            [genre_title] => Action/Adventure
            [rating_id] => 2
            [rating_title] => Pg-16 (USA)
            [person_id] => 16
            [person_name] => Mary Coyle Chase
            [role_title] => writer
        )

    [3] => stdClass Object
        (
            [movie_id] => 4
            [movie_name] => Harvey
            [genre_id] => 4
            [genre_title] => Action/Adventure
            [rating_id] => 2
            [rating_title] => Pg-16 (USA)
            [person_id] => 17
            [person_name] => Oscar Brodney
            [role_title] => writer
        )

    [4] => stdClass Object
        (
            [movie_id] => 4
            [movie_name] => Harvey
            [genre_id] => 4
            [genre_title] => Action/Adventure
            [rating_id] => 2
            [rating_title] => Pg-16 (USA)
            [person_id] => 18
            [person_name] => Myles Connolly
            [role_title] => writer
        )

)
kelly
  • 415
  • 2
  • 9
  • 24
Fawaz
  • 3
  • 2

2 Answers2

1

Not quite sure what are you trying to achieve. To print only some members try doing this:

foreach ($your_array as $key => $value) {

    echo '<pre>';
    echo $value->person_id . " ";
    echo $value->person_name . " ";
    echo $value->role_title . " ";
    echo "\n";
    echo '</pre>';
}

And if you can print it, you can save it to new array however you desire.

Qwerty
  • 29,062
  • 22
  • 108
  • 136
  • I think there is problem in my array – Fawaz Mar 01 '13 at 09:33
  • You have array of objects. You can acces each object by `array[index]`. You can acces object's members by `object->member`. Altogether it is something like this: `array[0]->member`. That's what the foreach do. You can then save those values into new array and convert that array. – Qwerty Mar 01 '13 at 09:39
  • How to save this in array i am new to php – Fawaz Mar 01 '13 at 09:45
  • Saving old values into new array `$newarray["key"] = $oldarray[object_index]->member;` But you probably want this: make a new Array of Arrays `$newarray["person1"]["person_id"] = $oldarray[0]->person_id;` etc. Try googling a bit, use php.net. – Qwerty Mar 01 '13 at 09:52
  • is it possible to combine these array – Fawaz Mar 01 '13 at 10:10
  • Array ( [0] => 1 [1] => Samuel Goldwyn [2] => producer ) Array ( [0] => 2 [1] => George Fitzmaurice [2] => directer ) Array ( [0] => 3 [1] => Dorothy Arzner [2] => directer ) Array ( [0] => 4 [1] => Alex Merces [2] => writer ) – Fawaz Mar 01 '13 at 10:11
  • Sure. Try googling "php join arrays" and it will find `array_merge` http://php.net/manual/en/function.array-merge.php But you have same keys, which is a problem. Save it into array of arrays. – Qwerty Mar 01 '13 at 10:17
0

This is what you want:

echo '<pre>';

foreach ($arr as $key => $value) {
    $newarray[$key]["person_id"]   = $value->person_id;
    $newarray[$key]["person_name"] = $value->person_name;
    $newarray[$key]["role_title"]  = $value->role_title;
}

// var_dump($newarray);
$json = json_encode($newarray);
echo $json;

echo "</pre>";

You should read some tutorials. These are real basics.

Qwerty
  • 29,062
  • 22
  • 108
  • 136
  • it result in this [{"person_id":"1","person_name":"Samuel Goldwyn","role_title":"producer"},{"person_id":"2","person_name":"George Fitzmaurice","role_title":"directer"},{"person_id":"3","person_name":"Dorothy Arzner","role_title":"directer"},{"person_id":"4","person_name":"Alex Merces","role_title":"writer"}] – Fawaz Mar 01 '13 at 11:01