0

I have an array from CSV import. The array look like this

Array
(
    [0] => Array
        (
            [DOCategory] => IM
            [ToCustomerCode] => M-CBC
            [ExternalDocNo] => DRDOC-DJ-GBAR10-25
            [Item] => S160373
            [Quantity] => 4
            [Remark2] => TEST
        )

    [1] => Array
        (
            [DOCategory] => IM
            [ToCustomerCode] => M-CBD
            [ExternalDocNo] => DRDOC-DJ-GBAR10-25
            [Item] => S160373
            [Quantity] => 5
            [Remark2] => TEST2
        )

    [2] => Array
        (
            [DOCategory] => IM
            [ToCustomerCode] => M-CJ
            [ExternalDocNo] => DRDOC-DJ-GBAR10-25
            [Item] => S160373
            [Quantity] => 5
            [Remark2] => TEST3
        )

    [3] => Array
        (
            [DOCategory] => IM
            [ToCustomerCode] => M-CL
            [ExternalDocNo] => DRDOC-DJ-GBAR10-25
            [Item] => S160373
            [Quantity] => 4
            [Remark2] => TEST4
        )

    [4] => Array
        (
            [DOCategory] => IM
            [ToCustomerCode] => M-CRG
            [ExternalDocNo] => DRDOC-DJ-GBAR10-25
            [Item] => S160373
            [Quantity] => 4
            [Remark2] => TEST5
        )

    [5] => Array
        (
            [DOCategory] => IM
            [ToCustomerCode] => M-CRGM
            [ExternalDocNo] => DRDOC-DJ-GBAR10-25
            [Item] => S160373
            [Quantity] => 4
            [Remark2] => TEST6
        )

    [6] => Array
        (
            [DOCategory] => IM
            [ToCustomerCode] => M-CRSB
            [ExternalDocNo] => DRDOC-DJ-GBAR10-25
            [Item] => S160373
            [Quantity] => 4
            [Remark2] => TEST7
        )

)

But now i want to add a session. So My Array will look loke this.

[0] => Array
        (
            [DOCategory] => IM
            [ToCustomerCode] => M-CBC
            [ExternalDocNo] => DRDOC-DJ-GBAR10-25
            [Item] => S160373
            [Quantity] => 4
            [Remark2] => TEST
            [session] => ....
        )

The [session] is from this

$random = md5(uniqid( $userlogin, true));
$new = array('session' => $random);

Then Here is what i try so far

 $x=0;
                    while( ($line = fgetcsv($f)) !== false) {
                        $data[] = $line;
                    }
                    for($x=0;$x<count($data);$x++){
                        if($x != 0) {
                            $newArray[] = array_combine($data[0], $data[$x]);
                            $newArrays[] = $new;
                        }
                    }
                $newArray = array_push($newArray,$newArrays);
                echo "<pre>";print_r($newArray);

and the result from my echo "<pre>";print_r($newArray); is 8. So how can i achieve that i want. Thanks in advance and sorry for my bad english.

1
  • 1
    Why not just use a for or foreach statement to iterate through the array and add the session key/value? Commented Mar 3, 2017 at 4:05

6 Answers 6

1

You use this function

   $arr1= array_merge($arr1, $arr2);

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

2 Comments

It gime like this [6] => Array ( [DOCategory] => IM [ToCustomerCode] => M-CRSB [ExternalDocNo] => DRDOC-DJ-GBAR10-25 [Item] => S160373 [Quantity] => 4 [Remark2] => TEST7 ) [session] => e3ba1d241080830f2638be5a91debd69
please say more clear about the trouble, I m using mobile web , so I cant see clearly
1

Your code is strange, but try this:

$x=0;
while( ($line = fgetcsv($f)) !== false) {
   $data[] = $line;
}
for($x=0;$x<count($data);$x++){
   if($x != 0) {
       $newArray[] = array_combine($data[0], $data[$x]);
       $newArray[$x]["session"] = $new;
   }
}
echo "<pre>";print_r($newArray);

you can also skip the temp array if you don't need it:

$x=0;
while( ($line = fgetcsv($f)) !== false) {
   $data[] = $line;
}
for($x=0;$x<count($data);$x++){
   if($x != 0) {
       $data[$x]["session"] = $new;
   }
}
echo "<pre>";print_r($data);

2 Comments

I still need this part ` $newArray[] = array_combine($data[0], $data[$x]);`
ok, so just add the array to it on the next line. $newArray[$x]["session"] = $new;
0

Loop over your array and add a session index

for($x=0;$x<count($data);$x++)
    {
     $newArrays[$x]['session'] = $new;
    }

1 Comment

I answered this first :(
0

You can use array_walk

Try this

array_walk($array, function(&$a) {
  $a['session'] = md5(uniqid( $userlogin, true));
});

Comments

0

You may use php array function array_merge() Here is link

Comments

0

use array_map() or foreach or array_walk to itereate and add session key.

array_map(function($v){
  $v['session'] = md5(uniqid( $userlogin, true));
  return $v;
});

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.