0

I am getting confused as to how I could loop trough my multidimensional associative array and pull the values separately to insert into my database. I am wanting to pull each item from the old array and populate it into a column called old and pull each item from the new array and populate it to the new column.

    $array = array(

        'old' => array('item1', 'item2'),
        'new' => array('item3', 'item4')

    );


    foreach($array as $items){

        foreach($items as $value){

        $model = new \Namespace\Model;
        $model->old = $items['old'];
        $model->new = $items['new']
        $model->save();

        }

    }

2 Answers 2

2

Well, I think foreach is not going to help you here. Try this:

$array = array(

        'old' => array('item1', 'item2'),
        'new' => array('item3', 'item4')

    );

if(count($array['old'] == $array['new'])) {
    $model = new \Namespace\Model;
    for($i = 0; $i < count($array['old']); $i++){
        $model->old = $array['old'][$i];
        $model->new = $array['new'][$i];
        $model->save();
    }

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

1 Comment

If you see, he is saving it in the model before i rewrite it. My guess is save persists it in a database. If not, you would be right, but if I'm right I save memory by doing it like this.
1

try this:

$array = array( array("item1" => "1" , "item2" => "2") , array("item1" =>"1", "item2" =>"2");

You would have to parse each different array all the sub arrays would just run in a for loop like this:

for($i = 0, $i < $array->getLength;$i++){
$array["item1"][$i];
}

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.