I'm PHP beginner and have a question:
I have a multidimensional associative array:
array(
"X" => array( "x1" => "1", "x2" => "2", "x3" => "3" ),
"Y" => array( "y1" => "1", "y2" => "2", "y3" => "3" ),
"Z" => array( "z1" => "1", "z2" => "2", "z3" => "3" )
)
and need to prepend "" => "" to every element X, Y, Z, so then it will be:
array(
"X" => array( "" => "", "x1" => "1", "x2" => "2", "x3" => "3" ),
"Y" => array( "" => "", "y1" => "1", "y2" => "2", "y3" => "3" ),
"Z" => array( "" => "", "z1" => "1", "z2" => "2", "z3" => "3" )
)
X, Y, Z names are often changed, so I need to get a key name first and then add new value.
I think of using foreach somehow. I'm trying it, but can achieve it.
Thanks
<?php
$temp = array(
"X" => array( "x1" => "1", "x2" => "2", "x3" => "3" ),
"Y" => array( "y1" => "1", "y2" => "2", "y3" => "3" ),
"Z" => array( "z1" => "1", "z2" => "2", "z3" => "3" )
);
$you_array = array();
foreach($temp as $k=>$v){
$v = array_merge([""=>""],$v);
$you_array[$k] = $v;
}
print_r($you_array);
?>
check the following code:
<?php
$temp = array(
"X" => array( "x1" => "1", "x2" => "2", "x3" => "3" ),
"Y" => array( "y1" => "1", "y2" => "2", "y3" => "3" ),
"Z" => array( "z1" => "1", "z2" => "2", "z3" => "3" )
);
$you_array = array();
foreach($temp as $k=>$v){
array_unshift($v, " ");
$you_array[$k] = $v;
}
print_r($you_array);
?>
Out put :
Array (
[X] => Array ( [0] => [x1] => 1 [x2] => 2 [x3] => 3 )
[Y] => Array ( [0] => [y1] => 1 [y2] => 2 [y3] => 3 )
[Z] => Array ( [0] => [z1] => 1 [z2] => 2 [z3] => 3 )
)
You can use array_unshift to add new elements to an array.
As you already mentioned, create a foreach loop and add your new elements to each sub-array:
$new = array("new_key" => "");
foreach ($yourArray as $key => $value)
array_unshift($yourArray[$key], $new);
Be careful: Your new element needs a key value. An empty key (like you mentioned) is not possible. If you just add a new element without a key array("") it will be added with a numerical key (0 => ""). Existing numerical keys will be modified to start counting from zero.
I think this is what you are looking for
[akshay#localhost tmp]$ cat test.php
<?php
$array=array(
"X" => array( "x1" => "1", "x2" => "2", "x3" => "3" ),
"Y" => array( "y1" => "1", "y2" => "2", "y3" => "3" ),
"Z" => array( "z1" => "1", "z2" => "2", "z3" => "3" )
);
// Input
print_r($array);
// & reference
foreach($array as &$sub_array)
{
$sub_array = array(""=>"")+$sub_array;
}
// Output
print_r($array);
?>
Output
[akshay#localhost tmp]$ php test.php
Array
(
[X] => Array
(
[x1] => 1
[x2] => 2
[x3] => 3
)
[Y] => Array
(
[y1] => 1
[y2] => 2
[y3] => 3
)
[Z] => Array
(
[z1] => 1
[z2] => 2
[z3] => 3
)
)
Array
(
[X] => Array
(
[] =>
[x1] => 1
[x2] => 2
[x3] => 3
)
[Y] => Array
(
[] =>
[y1] => 1
[y2] => 2
[y3] => 3
)
[Z] => Array
(
[] =>
[z1] => 1
[z2] => 2
[z3] => 3
)
)
Related
I'm retrieving some JSON that I am converting to an associative array. The issue that I am having is I am trying to get the email value from the user's who id matches the value that I have already set as a variable.
Here is what the array looks like
Array
(
[object] => list
[data] => Array
(
[0] => Array
(
[object] => pro
[id] => pro_77c9c6a85d814e059a6a2690989bae29
[first_name] => Jane
[last_name] => Doe
[full_name] => Jane Doe
[initials] => JD
[email] => admin#testorg.com
[mobile_number] => 9998761234
[messaging_uuid] => 4547c231c3e7d0ff1796f47b88f166d5
[color_hex] => EF9159
[avatar_url] => /assets/add_image.png
[avatar_thumb_url] =>
[has_avatar] =>
[organization_name] => testorg
[is_admin] => 1
[permissions] => Array
(
[show_company_setup] => 1
[can_see_home_data] => 1
[show_reporting] => 1
)
[is_super_pro] =>
[is_archived] =>
[impersonated] =>
)
[1] => Array
(
[object] => pro
[id] => pro_0fcb8e8610e54c518078db77ced7530e
[first_name] => Robert
[last_name] => Jordan
[full_name] => Robert Jordan
[initials] => RJ
[email] => rj#testorg.com
[mobile_number] => 4547457742
[messaging_uuid] => 0fcb8e8610e54c518078db77ced7530e
[color_hex] => EF9159
[avatar_url] => /assets/add_image.png
[avatar_thumb_url] =>
[has_avatar] =>
[organization_name] => testorg
[is_admin] => 1
[permissions] => Array
(
[show_company_setup] => 1
[can_see_home_data] => 1
[show_reporting] => 1
)
[is_super_pro] =>
[is_archived] =>
[impersonated] =>
)
)
[url] => /pros
)
Im basically trying to match the value that I have set in my pro_id variable
pro_0fcb8e8610e54c518078db77ced7530e
To the 'ID' Key in the array above and get the 'email' value associated to that same array and store for use later in my code.
Here is what I have so far, but no joy
foreach ($proObject as $key => $value) {
if ($key['id'] == $pro_id)
$techmail = $key['email'];
}
From my understanding of your question, and if you want to get only one element, you should be able to use array_search with array_column to get the index of the searched element. Using that you can then access the element and its corresponding email value. If you might expect more than one element I would use array_filter.
One Element:
In short:
$i = array_search($pro_id, array_column($proObject, 'id'));
$element = ($i !== false ? $proObject[$i] : null);
print_r($element["email"]);
Full code:
<?php
$proObject = array(
array(
"object" => "pro",
"id" => "pro_77c9c6a85d814e059a6a2690989bae29",
"first_name" => "Jane",
"last_name" => "Doe",
"full_name" => "Jane Doe",
"initials" => "JD",
"email" => "admin#testorg.com",
"mobile_number" => "9998761234",
"messaging_uuid" => "4547c231c3e7d0ff1796f47b88f166d5",
"color_hex" => "EF9159",
"avatar_url" => "/assets/add_image.png",
"avatar_thumb_url" => "",
"has_avatar" => "",
"organization_name" => "testorg",
"is_admin" => "1",
"permissions" => array(
"show_company_setup" => "1",
"can_see_home_data" => "1",
"show_reporting" => "1",
) ,
"is_super_pro" => "",
"is_archived" => "",
"impersonated" => "",
) ,
array(
"object" => "pro",
"id" => "pro_0fcb8e8610e54c518078db77ced7530e",
"first_name" => "Robert",
"last_name" => "Jordan",
"full_name" => "Robert Jordan",
"initials" => "RJ",
"email" => "rj#testorg.com",
"mobile_number" => "4547457742",
"messaging_uuid" => "0fcb8e8610e54c518078db77ced7530e",
"color_hex" => "EF9159",
"avatar_url" => "/assets/add_image.png",
"avatar_thumb_url" => "",
"has_avatar" => "",
"organization_name" => "testorg",
"is_admin" => "1",
"permissions" => array(
"show_company_setup" => "1",
"can_see_home_data" => "1",
"show_reporting" => "1",
) ,
"is_super_pro" => "",
"is_archived" => "",
"impersonated" => "",
)
);
$pro_id = "pro_0fcb8e8610e54c518078db77ced7530e";
$i = array_search($pro_id, array_column($proObject, 'id'));
$element = ($i !== false ? $proObject[$i] : null);
print_r($element["email"]);
?>
Multiple Elements:
In short:
class idEqualsFilter {
private $id;
public function __construct($id) {
$this->id = $id;
}
function __invoke($i) {
return $this->id === $i["id"];
}
};
$elements = array_filter($proObject, new idEqualsFilter($pro_id));
print_r(array_column($elements, "email"));
Full code:
<?php
$proObject = array(
array(
"object" => "pro",
"id" => "pro_77c9c6a85d814e059a6a2690989bae29",
"first_name" => "Jane",
"last_name" => "Doe",
"full_name" => "Jane Doe",
"initials" => "JD",
"email" => "admin#testorg.com",
"mobile_number" => "9998761234",
"messaging_uuid" => "4547c231c3e7d0ff1796f47b88f166d5",
"color_hex" => "EF9159",
"avatar_url" => "/assets/add_image.png",
"avatar_thumb_url" => "",
"has_avatar" => "",
"organization_name" => "testorg",
"is_admin" => "1",
"permissions" => array
(
"show_company_setup" => "1",
"can_see_home_data" => "1",
"show_reporting" => "1",
),
"is_super_pro" => "",
"is_archived" => "",
"impersonated" => "",
),
array(
"object" => "pro",
"id" => "pro_0fcb8e8610e54c518078db77ced7530e",
"first_name" => "Robert",
"last_name" => "Jordan",
"full_name" => "Robert Jordan",
"initials" => "RJ",
"email" => "rj#testorg.com",
"mobile_number" => "4547457742",
"messaging_uuid" => "0fcb8e8610e54c518078db77ced7530e",
"color_hex" => "EF9159",
"avatar_url" => "/assets/add_image.png",
"avatar_thumb_url" => "",
"has_avatar" => "",
"organization_name" => "testorg",
"is_admin" => "1",
"permissions" => array
(
"show_company_setup" => "1",
"can_see_home_data" => "1",
"show_reporting" => "1",
),
"is_super_pro" => "",
"is_archived" => "",
"impersonated" => "",
)
);
$pro_id = "pro_0fcb8e8610e54c518078db77ced7530e";
class idEqualsFilter {
private $id;
public function __construct($id) {
$this->id = $id;
}
function __invoke($i) {
return $this->id === $i["id"];
}
};
$elements = array_filter($proObject, new idEqualsFilter($pro_id));
print_r(array_column($elements, "email"));
?>
This question already has answers here:
How to sum all column values in multi-dimensional array?
(20 answers)
Closed 9 months ago.
I have an array with duplicate key values. How can I sum all duplicate array key values in another new array?
$array = Array (
"0" => Array ( "2" => 123 ),
"1" => Array ( "4" => 45 ),
"2" => Array ( "3" => 12 ),
"3" => Array ( "5" => 2 ),
"4" => Array ( "2" => 12 ),
"5" => Array ( "4" => 21 ),
"6" => Array ( "2" => 12 ),
"7" => Array ( "3" => 21 ),
"8" => Array ( "2" => 12 ),
"9" => Array ( "3" => 21 ),
"10" => Array ( "2" => 2 ),
"11" => Array ( "4" => 2 ),
"12" => Array ( "2" => 2 ),
"13" => Array ( "4" => 2 ),
"14" => Array ( "3" => 12 ),
"15" => Array ( "4" => 12 ),
"16" => Array ( "2" => 12 ),
"17" => Array ( "2" => 12 ),
"18" => Array ( "4" => 12 ),
"19" => Array ( "3" => 12 ),
"20" => Array ( "2" => 15 ),
"21" => Array ( "4" => 21 ),
);
Output will looks like
$newArray = Array
(
[2] => 202
[3] => 78
[4] => 115
[5] => 2
)
You can use array_sum and array_column to get the sums of each.
First we have to get all the keys then sum them with array_sum and array_column.
$arr = Array (
"0" => Array ( "2" => 123 ),
"1" => Array ( "4" => 45 ),
"2" => Array ( "3" => 12 ),
"3" => Array ( "5" => 2 ),
"4" => Array ( "2" => 12 ),
"5" => Array ( "4" => 21 ),
"6" => Array ( "2" => 12 ),
"7" => Array ( "3" => 21 ),
"8" => Array ( "2" => 12 ),
"9" => Array ( "3" => 21 ),
"10" => Array ( "2" => 2 ),
"11" => Array ( "4" => 2 ),
"12" => Array ( "2" => 2 ),
"13" => Array ( "4" => 2 ),
"14" => Array ( "3" => 12 ),
"15" => Array ( "4" => 12 ),
"16" => Array ( "2" => 12 ),
"17" => Array ( "2" => 12 ),
"18" => Array ( "4" => 12 ),
"19" => Array ( "3" => 12 ),
"20" => Array ( "2" => 15 ),
"21" => Array ( "4" => 21 ),
);
// find all subarray keys (2,3,4,5)
foreach($arr as $subarr){
$keys[] = key($subarr);
}
// remove duplicate keys
$keys = array_unique($keys);
// sum values with same key from $arr and save to $sums
foreach($keys as $key){
$sums[$key] = array_sum(array_column($arr,$key));
}
var_dump($sums);
https://3v4l.org/F3RJr
The code can be made shorter like this:
foreach($arr as $subarr){
$key = key($subarr);
if(!isset($sums[$key])){
$sums[$key] = array_sum(array_column($arr,$key));
}
}
var_dump($sums);
but I'm not sure it's faster. Maybe...
You can use array_walk_recursive()
$result = [];
array_walk_recursive($array, function($v, $k) use (&$result) {
if (!isset($result[$k])) {
$result[$k] = $v;
} else {
$result[$k] += $v;
}
});
print_r($result);
Check the below code.
$output = array();
$keyarray = array();
foreach($arr as $key => $val){
if(is_array($val)){
$key = key($val);
if(in_array($key,$keyarray)) {
$output[$key] = $output[$key]+$val[$key];
} else {
$keyarray[] = $key;
$output[$key] = $val[$key];
}
}
}
print_r($output);
print_r($output); will give you the expected result.
Array 1 output
Array (
[0] => Array ( [ID] => 335 [userid] => 4 [username] => demo [media_id] => 17 )
[1] => Array ( [ID] => 436 [userid] => 4 [username] => demo [media_id] => 18 )
[2] => Array ( [ID] => 637 [userid] => 4 [username] => demo [media_id] => 19 )
[3] => Array ( [ID] => 838 [userid] => 4 [username] => demo [media_id] => 20 )
);
Array 2 output
Array (
[1] => Array ( [ID] => 35 [userid] => 4 [media_id] => 17 )
[2] => Array ( [ID] => 36 [userid] => 4 [media_id] => 18 )
);
How to get other array value if match? I need if media_id and userid of array 2 match in array 1 then how to get perticuler ID and username from array 1 in foreach loop of array 2 ?
Update
$array1 = array (
0 => array ( "ID" => "335", "userid" => "4", "username" => "demo", "media_id" => "17" ),
1 => array ( "ID" => "436", "userid" => "4", "username" => "demo", "media_id" => "18" ),
2 => array ( "ID" => "637", "userid" => "4", "username" => "demo", "media_id" => "19" ),
3 => array ( "ID" => "838", "userid" => "4", "username" => "demo", "media_id" => "20" )
);
$array2 = array (
1 => array ( "ID" => "35", "userid" => "4", "media_id" => "17" ),
2 => array ( "ID" => "36", "userid" => "4", "media_id" => "18" )
);
foreach($array2 as $array) {
foreach($array1 as $get_data) {
if($array1['media_id'] == $get_data['media_id'] && $array1['userid'] == $get_data['userid']){
$get_result[] = //get_data true;
} else {
$get_result[] = //get_data false;
}
}
//get ID and username or show false value
}
Final answer
foreach($array2 as $array) {
$return = "false";
foreach($array1 as $get_data) {
if($array1['media_id'] == $get_data['media_id'] && $array1['userid'] == $get_data['userid']) {
$get_result[] = ['ID' => $get_data['ID'], 'username' => $get_data['username']];
$return = "true";
}
}
if($return = "false"){
echo false;
}
}
If I understood well your question (I'm not sure), this should work:
foreach($array2 as $array) {
foreach($array1 as $get_data) {
if($array1['media_id'] == $get_data['media_id'] && $array1['userid'] == $get_data['userid']) {
$get_result[] = ['ID' => $get_data['ID'], 'username' => $get_data['username']];
}
}
}
if (count($get_result) == 0) {
$get_result[] = false;
}
Not sure about your real needs, but #gmc gave you (part of ? all ?) what you need IMHO
<?php
$array1 = array (
0 => array ( "ID" => "335", "userid" => "4", "username" => "demo", "media_id" => "117" ),
1 => array ( "ID" => "436", "userid" => "4", "username" => "demo", "media_id" => "118" ),
2 => array ( "ID" => "637", "userid" => "4", "username" => "demo", "media_id" => "19" ),
3 => array ( "ID" => "838", "userid" => "4", "username" => "demo", "media_id" => "20" )
);
$array2 = array (
1 => array ( "ID" => "35", "userid" => "4", "media_id" => "17" ),
2 => array ( "ID" => "36", "userid" => "4", "media_id" => "18" )
);
$get_result = array();
foreach($array2 as $array) {
foreach($array1 as $get_data) {
if( ($array['media_id'] == $get_data['media_id']) && ($array['userid'] == $get_data['userid'])) {
$get_result[] = ['ID' => $get_data['ID'], 'username' => $get_data['username']];
}
}
}
if (count($get_result) == 0) {
echo"false";
}
else {
echo"true"; print_r($get_result); }
?>
/* I modified values to 117 & 118 -> returns "false" */
/* If you set values to 17 & 18 -> returns "true" */
i am facing problem in storing work and education information of Facebook user.
this is how i am getting info of user:
$profile_request=$fb->get('/me?fields=name,first_name,last_name,email,work,education');
and then convert the response in to multidimensional array:
$profile = $profile_request->getGraphNode()->asArray();
this is how i get the response:
$profile=Array
(
"name" => "abc pqr",
"first_name" => "abc",
"last_name" => "pqr",
"email" => "abc#gmail.com",
"work" => Array
(
"0" => Array
(
"employer" => Array
(
"id" => "348468651959125",
"name" => "Karachi university"
),
"location" => Array
(
"id" => "110713778953693",
"name" => "Karachi, Pakistan"
),
"position" => Array
(
"id" => "1556082398000870",
"name" => "Lecturer"
),
"start_date" => "2008-04-25"
),
"1" => Array
(
"description" => "i am teaching......",
"end_date" => "2011-06-20",
"employer" => Array
(
"id" => "486743441453589",
"name" => "Happy Palace Grammar School NORTH 3 - J2"
),
"location" => Array
(
"id" => "110713778953693",
"name" => "Karachi, Pakistan"
),
"position" => Array
(
"id" => "1556082398000870",
"name" => "Lecturer"
),
"start_date" => "2005-06-17"
)
),
"education" => Array
(
"0" => Array
(
"school" => Array
(
"id" => "193563427344550",
"name" => "Mehran Model Sec School"
),
"type" => "High School"
),
"1" => Array
(
"concentration" => Array
(
"0" => Array
(
"id" => "104076956295773",
"name" => "Computer Science"
)
),
"school" => Array
(
"id" => "113340788773685",
"name" => "Sir Adamjee Institute"
),
"type" => "College",
"year" => Array
(
"id" => "118118634930920",
"name" => "2012"
)
),
"2" => Array
(
"concentration" => Array
(
"0" => Array
(
"id" => "104076956295773",
"name" => "Computer Science"
)
),
"degree" => Array
(
"id" => "519451718218292",
"name" => "Bachelor of science in Computer Science in Computer Science"
),
"school" => Array
(
"id" => "107345492681211",
"name" => "SZABIST"
),
"type" => "Graduate School",
"year" => Array
(
"id" => "127342053975510",
"name" => "2016"
)
)
),
"id" => "145864899647476"
);
and this is my code:
$keys = array_keys($profile);
print_r($keys);
echo count($profile);
echo "<br>";
for ($i=4; $i <count($profile)-1 ; $i++) {
# code...
/*[4] => work [5] => education*/
echo $keys[$i]."<br>";
foreach ($profile[$keys[$i]] as $k => $v) {
# code...
echo "<br>$k<br>";
$keeys=array_keys($v);
print_r($keeys);
}
}
I have the following:
$variants = [
0 => [
"variant_name" => "iPhone 5",
"sku_id" => "2",
"sku" => "GLC-IPH5REDXXXL",
"stock_total" => "10",
"stock_left" => "10",
"retail_price" => 1000,
"on_sale_price" => 0
],
1 => [
"variant_name" => "Red",
"sku_id" => "2",
"sku" => "GLC-IPH5REDXXXL",
"stock_total" => "10",
"stock_left" => "10",
"retail_price" => 1000,
"on_sale_price" => 0
],
2 => [
"variant_name" => "iPhone 6s Plus",
"sku_id" => "4",
"sku" => "GLC-IPH6SP",
"stock_total" => "5",
"stock_left" => "5",
"retail_price" => 1000,
"on_sale_price" => 0
],
3 => [
"variant_name" => "iPhone 6s",
"sku_id" => "13",
"sku" => "GLC-IPH6S",
"stock_total" => "5",
"stock_left" => "5",
"retail_price" => 1000,
"on_sale_price" => 0
]
]
I would like to put them in the following array
0 => [
"sku_id" => "2",
"sku" => "GLC-IPH5REDXXXL",
"stock_total" => "10",
"stock_left" => "10",
"retail_price" => 1000,
"on_sale_price" => 0,
"options" => ['iPhone 4', 'Red'],
"option1" => 'iPhone4',
"option2" => 'Red',
"option3" => null
],
1 => [
"sku_id" => "4",
"sku" => "GLC-IPH6SP",
"stock_total" => "5",
"stock_left" => "5",
"retail_price" => 1000,
"on_sale_price" => 0,
"options" => ['iPhone 6s Plus'],
"option1" => 'iPhone 6s Plus',
"option2" => null,
"option3" => null
],
2 => [
"sku_id" => "13",
"sku" => "GLC-IPH6S",
"stock_total" => "5",
"stock_left" => "5",
"retail_price" => 1000,
"on_sale_price" => 0,
"options" => ['iPhone 6s'],
"option1" => 'iPhone 6s',
"option2" => null,
"option3" => null
]
I can't fill the options with each variant_name
I can't set the option1, option2, option3 with each corresponding variant_name
I've tried a simple foreach($variants as $v) loop on the first array and I got it working until options, option1, option2, option3 from which I get the repeating values.
I just can't figure it out how to do it, any suggestions?
You could use a simple foreach() and perhaps a for(). The for() here is a little inefficient because it runs every time, so it's highly repetitive and continually overwrites...but it works:
foreach($variants as $rows) {
$sku[$rows['sku_id']]['sku_id'] = $rows['sku_id'];
$sku[$rows['sku_id']]['sku'] = $rows['sku'];
$sku[$rows['sku_id']]['stock_total'] = $rows['stock_total'];
$sku[$rows['sku_id']]['stock_left'] = $rows['stock_left'];
$sku[$rows['sku_id']]['retail_price'] = $rows['retail_price'];
$sku[$rows['sku_id']]['on_sale_price'] = $rows['on_sale_price'];
$sku[$rows['sku_id']]['options'][] = $rows['variant_name'];
for($i = 0; $i < 3; $i++)
$sku[$rows['sku_id']]['option'.($i+1)] = (isset($sku[$rows['sku_id']]['options'][$i]))? $sku[$rows['sku_id']]['options'][$i] : NULL;
}
echo print_r(array_values($sku));
Gives you:
Array
(
[0] => Array
(
[sku] => GLC-IPH5REDXXXL
[stock_total] => 10
[stock_left] => 10
[retail_price] => 1000
[on_sale_price] => 0
[options] => Array
(
[0] => iPhone 5
[1] => Red
)
[option1] => iPhone 5
[option2] => Red
[option3] =>
)
[1] => Array
(
[sku] => GLC-IPH6SP
[stock_total] => 5
[stock_left] => 5
[retail_price] => 1000
[on_sale_price] => 0
[options] => Array
(
[0] => iPhone 6s Plus
)
[option1] => iPhone 6s Plus
[option2] =>
[option3] =>
)
[2] => Array
(
[sku] => GLC-IPH6S
[stock_total] => 5
[stock_left] => 5
[retail_price] => 1000
[on_sale_price] => 0
[options] => Array
(
[0] => iPhone 6s
)
[option1] => iPhone 6s
[option2] =>
[option3] =>
)
)
Solution
$result = [];
foreach($variants as $v) {
$result[$v['sku_id']]['sku_id'] = $v['sku_id'];
$result[$v['sku_id']]['sku'] = $v['sku'];
$result[$v['sku_id']]['stock_left'] = $v['stock_left'];
$result[$v['sku_id']]['retail_price'] = price($v['retail_price']);
$result[$v['sku_id']]['options'][] = $v['variant_name'];
if(isset($result[$v['sku_id']]['options'][0])) {
$result[$v['sku_id']]['option1'] = $result[$v['sku_id']]['options'][0];
}
else {
$result[$v['sku_id']]['option1'] = null;
}
if(isset($result[$v['sku_id']]['options'][1])) {
$result[$v['sku_id']]['option2'] = $result[$v['sku_id']]['options'][1];
}
else {
$result[$v['sku_id']]['option2'] = null;
}
if(isset($result[$v['sku_id']]['options'][2])) {
$result[$v['sku_id']]['option3'] = $result[$v['sku_id']]['options'][2];
}
else {
$result[$v['sku_id']]['option3'] = null;
}
}
return $result;