0

I have an array of objects, each object contains a bunch of values, two of which are an int and a string. I need to loop over the objects and pull out the string and int and place them into an associative array, where each string associates with each int. How would I go about doing this?

This is what I have so far:

foreach( $fileobject as $p ) {
    $program_number = $p['number'];
    $filename = $p['InputFile']['filename'];
}

$fileobject is the array of objects. 'number' is the int, and 'filename' is the string. What is the syntax for putting the 'number' and 'filename' together into an associative array. There are undetermined amount of objects in the initial array.

0

1 Answer 1

1
$result = array();
foreach( $fileobject as $p ) {
    $program_number = $p['number'];
    $filename = $p['InputFile']['filename'];
    $result[] = array( 'number' => $program_number, 'filename' => $filename);
}

However you mentioned that you're using an array of objects, so this is likely the correct syntax:

$result = array();
foreach( $fileobject as $p ) {
    $program_number = $p->number;
    $filename = $p->InputFile->filename;
    $result[] = array( 'number' => $program_number, 'filename' => $filename);
}
Sign up to request clarification or add additional context in comments.

Comments

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.