I would like to switch the main keys(0,1,2) of an array with a subelement key(user_id).
For example, from this array:
array(3) {
[0]=>
array(3) {
["num_products_user_by_ref"]=>
string(1) "1"
["user_id"]=>
string(2) "77"
["reference"]=>
string(3) "E49"
}
[1]=>
array(3) {
["num_products_user_by_ref"]=>
string(1) "9"
["user_id"]=>
string(3) "526"
["reference"]=>
string(3) "E49"
}
[2]=>
array(3) {
["num_products_user_by_ref"]=>
string(2) "38"
["user_id"]=>
string(3) "346"
["reference"]=>
string(3) "E49"
}
}
I need :
array(952) {
[77]=>
array(2) {
["num_products_user_by_ref"]=>
string(1) "1"
["reference"]=>
string(3) "E49"
}
[526]=>
array(3) {
["num_products_user_by_ref"]=>
string(1) "9"
["reference"]=>
string(3) "E49"
}
[346]=>
array(3) {
["num_products_user_by_ref"]=>
string(2) "38"
["reference"]=>
string(3) "E49"
}
Every user_id could contains more than 1 pair num_products_user_by_ref/reference.
I remember that there is a function to achieve this (ksort?) associated to a custom function to implement.
$out = array();
foreach ($arr as $key => $value){
$out[$value['user_id']]["num_products_user_by_ref"] = $value["num_products_user_by_ref"];
$out[$value['user_id']]["reference"] = $value["reference"];
}
print_r($out);
Your question showed a structure that doesn't seem to fit with your comment "Every user_id could contains more than 1 pair num_products_user_by_ref/reference." So, here's another version that allows for that possibility:
$out = array();
foreach ($arr as $key => $value){
$entry = array("num_products_user_by_ref" => $value["num_products_user_by_ref"],
"reference" => $value["reference"]);
$out[$value['user_id']][] = $entry;
}
Output:
Array
(
[77] => Array
(
[0] => Array
(
[num_products_user_by_ref] => 1
[reference] => E49
)
[1] => Array
(
[num_products_user_by_ref] => 5
[reference] => E49
)
)
[526] => Array
(
[0] => Array
(
[num_products_user_by_ref] => 9
[reference] => E49
)
)
[346] => Array
(
[0] => Array
(
[num_products_user_by_ref] => 38
[reference] => E49
)
)
)
Here's another version for those who don't like traditional loops:
$out = array();
array_walk($arr, function($e, $k) use(&$out){
$entry = array("num_products_user_by_ref" => $e["num_products_user_by_ref"],
"reference" => $e["reference"]);
$out[$e['user_id']][] = $entry;
});
Related
I have an array
Array ( [0] => 4-8-2019 [1] => 5-8-2019 [2] => 5 [3] => 6 ,[4]=>1,[5]=>2 )
How I can make this.Pleasse help me I have tried array mege but not working
Array ( [0] =>
[0]=> 4-8-2019
[1] => 5
[2] => 1
[1] =>[0]=> 5-8-2019
[1]=>6
[2]=>2 )
You want to seperate you array into two by every other item.
foreach($array as $key => $value){
$result[$key & 1][] = $value;
}
Only logic I found in your example is about even and odd indexes. If that's what you want to do, try this :
<?php
$array = [
'4-8-2019',
'5-8-2019',
'5',
'6',
'1',
'2',
];
$odds = [];
$evens = [];
foreach ($array as $index => $value) {
if ($index % 2 === 0) {
$evens[] = $value;
} else {
$odds[] = $value;
}
}
var_dump([$evens, $odds]);
Which outputs :
array(2) {
[0]=>
array(3) {
[0]=>
string(8) "4-8-2019"
[1]=>
string(1) "5"
[2]=>
string(1) "1"
}
[1]=>
array(3) {
[0]=>
string(8) "5-8-2019"
[1]=>
string(1) "6"
[2]=>
string(1) "2"
}
}
I have an array like this :
array(3) {
["data"]=> array(1) {
["mesq_G3SC"]=> array(1) {
["data"]=> object(stdClass)#30 (19) {
["cart_id"]=> string(13) "mesq_G3SC"
["qty"]=> string(4) "2.00"
["price"]=> string(5) "11400"
["product_id"]=> string(1) "6"
["member_id"]=> string(1) "5"
["session_id"]=> string(32) "4dedde2a2eb12b25e940b7051a5f65a1"
["date_added"]=> string(19) "2016-09-28 10:39:06"
["date_modified"]=> string(19) "2016-09-28 10:39:06"
["status"]=> string(7) "pending"
["category_id"]=> string(1) "2"
["location"]=> string(9) "England"
["square"]=> string(3) "300"
["stock"]=> string(3) "260"
["folder_name"]=> string(23) "assets/uploads/products"
["photo1"]=> string(11) "photo1.jpg"
["photo2"]=> string(11) "pc.jpg"
["category_name"]=> string(6) "PC"
["is_published"]=> string(1) "1"
["modified_by"]=> NULL
}
}
}
["total_item"]=> int(1)
["total_price"]=> string(8) "11400.00"
}
I would to get each value of that array with foreach but I got error
Trying to get property of non-object...
I developed this with Codeigniter and this is my foreach:
foreach ($data as $row) {
echo $row->product_id;
}
This is what I get if I do print_r($row) :
Array ( [mesq_G3SC] => Array ( [data] => stdClass Object ( [cart_id] => mesq_G3SC [qty] => 2.00 [price] => 11400 [product_id] => 6 [member_id] => 5 [session_id] => 4dedde2a2eb12b25e940b7051a5f65a1 [date_added] => 2016-09-28 10:39:06 [date_modified] => 2016-09-28 10:39:06 [status] => pending [category_id] => 2 [lokasi_lahan] => England [square] => 300 [stock] => 260 [folder_name] => assets/uploads/products [photo1] => photo1.jpg [photo2] => photo2.jpg [category_name] => PC [is_published] => 1 [modified_by] => ) ) ) 1 11400.00
Anyone knows how to get each value of that array?
I re-created your multidimensional array, if you want to get the deepest values, here it is:
$deepest_values = array();
foreach($arr_ as $first_level_key => $first_level_value) {
if(is_array($first_level_value)) {
foreach($first_level_value as $second_level_key => $second_level_value) {
if(is_array($second_level_value)) {
foreach($second_level_value as $third_level_key => $third_level_value) {
if(is_array($third_level_value)) {
foreach($third_level_value as $fourth_level_key => $fourth_level_value) {
$deepest_values[$fourth_level_key] = $fourth_level_value;
}
}
}
}
}
}
}
echo '<pre>';
print_r($deepest_values);
echo '</pre>';
if mesq_G3SC is the key you want to iterate over - i suggest you do the following
$obj = new stdClass();
$obj->cart_id = "mesq_G3SC";
$obj->qty = "2.00";
$obj->price = "11400";
$obj->product_id = "6";
$obj->member_id = "5";
$obj->session_id = "4dedde2a2eb12b25e940b7051a5f65a1";
$obj->date_added = "2016-09-28 10:39:06";
$obj->date_modified = "2016-09-28 10:39:06";
$obj->status = "pending";
$obj->category_id = "2";
$obj->location = "England";
$obj->square = "300";
$obj->stock = "260";
$obj->folder_name = "assets/uploads/products";
$obj->photo1 = "photo1.jpg";
$obj->photo2 = "pc.jpg";
$obj->category_name = "PC";
$obj->is_published = "1";
$obj->modified_by = NULL;
$data = array(
"mesq_G3SC"=> array(
"data"=> $obj
),
"total_item" => 1,
"total_price" => "11400.00"
);
foreach($data['mesq_G3SC'] AS $key => $obj)
{
echo $key;
print_r($obj);
}
Update: i simulated your array - and it works like a charm - there is something wrong with the keys - instead of var_dump try to print_r your data array and show us the output pls
Having the following array:
array(4) {
[0]=>
array(2) {
[0]=>
string(3) "233"
[1]=>
string(37) "some data"
}
[1]=>
array(2) {
[0]=>
string(3) "233"
[1]=>
string(68) "some other data"
}
[2]=>
array(2) {
[0]=>
string(3) "144"
[1]=>
string(38) "some other data"
}
[3]=>
array(2) {
[0]=>
string(3) "233"
[1]=>
string(42) "some other data"
}
}
I want to replace the values 233 and 144 (the key 0 from the inner array) by some random HEX color. The ones with the same keys (233) for example, has to have the same HEX color (FFF000 for example in the desired solution above).
This is the function I use to generate random HEX colors:
function randHEXcolor() {
return sprintf('%06X', mt_rand(0, 0xFFFFFF));
}
My desired output should be:
array(4) {
[0]=>
array(2) {
[0]=>
string(6) "FFF000"
[1]=>
string(37) "some data"
}
[1]=>
array(2) {
[0]=>
string(6) "FFF000"
[1]=>
string(68) "some other data"
}
[2]=>
array(2) {
[0]=>
string(6) "111333"
[1]=>
string(38) "some other data"
}
[3]=>
array(2) {
[0]=>
string(6) "FFF000"
[1]=>
string(42) "some other data"
}
}
How can I archieve this?
Thanks in advance.
foreach ($array as &$item) {
if (!isset($temp[$item[0]]) {
$temp[$item[0]] = randHEXcolor();
}
$item[0] = $temp[$item[0]];
}
If you want all values to be translated to the same random color, you'll have to save those colors:
$colors_translation = array();
foreach ($array as &$item) {
$color = $item[ 0 ];
$translate = $colors_translation[ $color ];
if (empty($translate)) {
$colors_translations[ $color ] = $translate = randHEXcolor();
}
$item[ 0 ] = $translate;
}
The solution using in_array and isset functions:
$keys = [];
foreach ($arr as &$v) { // $arr is your initial array
if (in_array($v[0], ['233', '144'])) {
if (!isset($keys[$v[0]])) $keys[$v[0]] = sprintf('%06X', mt_rand(0, 0xFFFFFF));
$v[0] = $keys[$v[0]];
}
}
print_r($arr);
The output:
Array
(
[0] => Array
(
[0] => 65A4BB
[1] => some data
)
[1] => Array
(
[0] => 65A4BB
[1] => some data
)
[2] => Array
(
[0] => DDB588
[1] => some data
)
[3] => Array
(
[0] => 65A4BB
[1] => some data
)
)
This code will create a color map as the array is traversed. Pre-populate $colorMap if you want pre-defined color translations.
<?php
$array = array(
0 => array(
0 => "233",
1 => "some data"
),
1 => array(
0 => "233",
1 => "some data"
),
2 => array(
0 => "144",
1 => "some data"
),
3 => array(
0 => "233",
1 => "some data"
),
);
$colorMap = array();
foreach ($array as &$inner) {
if (!array_key_exists($inner[0],$colorMap)) {
$newColor = randHEXcolor();
$colorMap[$inner[0]] = $newColor;
$inner[0] = $newColor;
} else {
$inner[0] = $colorMap[$inner[0]];
}
}
function randHEXcolor() {
return sprintf('%06X', mt_rand(0, 0xFFFFFF));
}
print_r($array);
print_r($colorMap);
Array
(
[0] => Array
(
[0] => F1519A
[1] => some data
)
[1] => Array
(
[0] => F1519A
[1] => some data
)
[2] => Array
(
[0] => 2F7D00
[1] => some data
)
[3] => Array
(
[0] => F1519A
[1] => some data
)
)
Array
(
[233] => F1519A
[144] => 2F7D00
)
Try:
<?php
$array = array(
0 => array(
0 => "233",
1 => "some data"
),
1 => array(
0 => "233",
1 => "some data"
),
2 => array(
0 => "144",
1 => "some data"
),
3 => array(
0 => "233",
1 => "some data"
),
);
function randHEXcolor() {
return sprintf('%06X', mt_rand(0, 0xFFFFFF));
}
$firstHex = randHEXcolor();
$secondHex = randHEXcolor();
foreach($array as $arrayIndex => &$arrayValue){
if($arrayValue[0] == "144"){
$arrayValue[0] = $firstHex;
}
if($arrayValue[0] == "233"){
$arrayValue[0] = $secondHex;
}
}
output:
array(4) {
[0]=>
array(2) {
[0]=>
string(6) "AB8248"
[1]=>
string(9) "some data"
}
[1]=>
array(2) {
[0]=>
string(6) "AB8248"
[1]=>
string(9) "some data"
}
[2]=>
array(2) {
[0]=>
string(6) "22AF8B"
[1]=>
string(9) "some data"
}
[3]=>
&array(2) {
[0]=>
string(6) "AB8248"
[1]=>
string(9) "some data"
}
}
I have this array here that contains a group of duplicate ids each id is set as as array key and the sub array contains a list of ids that also has another array within the same array?
DUPLICATES 16
array(16) {
[19503804]=>
array(3) {
[0]=>
string(8) "19501594"
[1]=>
string(8) "15539642"
[2]=>
string(8) "19498944"
}
[19501594]=>
array(3) {
[0]=>
string(8) "19503804"
[1]=>
string(8) "15539642"
[2]=>
string(8) "19498944"
}
[19837033]=>
array(6) {
[0]=>
string(8) "19854557"
[1]=>
string(8) "19854558"
[2]=>
string(8) "19854553"
[3]=>
string(8) "19854565"
[4]=>
string(8) "19854554"
[5]=>
string(8) "19854683"
}
[19854553]=>
array(6) {
[0]=>
string(8) "19854557"
[1]=>
string(8) "19854558"
[2]=>
string(8) "19837033"
[3]=>
string(8) "19854565"
[4]=>
string(8) "19854554"
[5]=>
string(8) "19854683"
}
[19544216]=>
array(3) {
[0]=>
string(8) "19524884"
[1]=>
string(8) "19560234"
[2]=>
string(8) "19540264"
}
[19854565]=>
array(6) {
[0]=>
string(8) "19854557"
[1]=>
string(8) "19854558"
[2]=>
string(8) "19837033"
[3]=>
string(8) "19854553"
[4]=>
string(8) "19854554"
[5]=>
string(8) "19854683"
}
[19854554]=>
array(6) {
[0]=>
string(8) "19854557"
[1]=>
string(8) "19854558"
[2]=>
string(8) "19837033"
[3]=>
string(8) "19854553"
[4]=>
string(8) "19854565"
[5]=>
string(8) "19854683"
}
[15539642]=>
array(3) {
[0]=>
string(8) "19503804"
[1]=>
string(8) "19501594"
[2]=>
string(8) "19498944"
}
[19844271]=>
array(1) {
[0]=>
string(8) "19341140"
}
[19498944]=>
array(3) {
[0]=>
string(8) "19503804"
[1]=>
string(8) "19501594"
[2]=>
string(8) "15539642"
}
[16399898]=>
array(1) {
[0]=>
string(8) "15436391"
}
[15436391]=>
array(1) {
[0]=>
string(8) "16399898"
}
[19341140]=>
array(1) {
[0]=>
string(8) "19844271"
}
[19560234]=>
array(3) {
[0]=>
string(8) "19544216"
[1]=>
string(8) "19524884"
[2]=>
string(8) "19540264"
}
[19854683]=>
array(6) {
[0]=>
string(8) "19854557"
[1]=>
string(8) "19854558"
[2]=>
string(8) "19837033"
[3]=>
string(8) "19854553"
[4]=>
string(8) "19854565"
[5]=>
string(8) "19854554"
}
[19540264]=>
array(3) {
[0]=>
string(8) "19544216"
[1]=>
string(8) "19524884"
[2]=>
string(8) "19560234"
}
}
notice first array?
[19503804]=>
array(3) {
[0]=>
string(8) "19501594"
[1]=>
string(8) "15539642"
[2]=>
string(8) "19498944"
}
the "19501594" "15539642" "19498944" also has another array which need to be unset.so at the end in this array only the unique arrays with unique values are given.
and this is the expected output required.
array(5) {
[19503804]=>
array(3) {
[0]=>
string(8) "19501594"
[1]=>
string(8) "15539642"
[2]=>
string(8) "19498944"
}
[19837033]=>
array(6) {
[0]=>
string(8) "19854557"
[1]=>
string(8) "19854558"
[2]=>
string(8) "19854553"
[3]=>
string(8) "19854565"
[4]=>
string(8) "19854554"
[5]=>
string(8) "19854683"
}
[19544216]=>
array(3) {
[0]=>
string(8) "19524884"
[1]=>
string(8) "19560234"
[2]=>
string(8) "19540264"
}
[19844271]=>
array(1) {
[0]=>
string(8) "19341140"
}
[16399898]=>
array(1) {
[0]=>
string(8) "15436391"
}
}
This is what i had initially coded as proposed by #Dale in the answer below.
$theduplicates = array (
19503804 =>
array (
0 => '19501594',
1 => '15539642',
2 => '19498944',
),
19501594 =>
array (
0 => '19503804',
1 => '15539642',
2 => '19498944',
),
19837033 =>
array (
0 => '19854557',
1 => '19854558',
2 => '19854553',
3 => '19854565',
4 => '19854554',
5 => '19854683',
),
19854553 =>
array (
0 => '19854557',
1 => '19854558',
2 => '19837033',
3 => '19854565',
4 => '19854554',
5 => '19854683',
),
19544216 =>
array (
0 => '19524884',
1 => '19560234',
2 => '19540264',
),
19854565 =>
array (
0 => '19854557',
1 => '19854558',
2 => '19837033',
3 => '19854553',
4 => '19854554',
5 => '19854683',
),
19854554 =>
array (
0 => '19854557',
1 => '19854558',
2 => '19837033',
3 => '19854553',
4 => '19854565',
5 => '19854683',
),
15539642 =>
array (
0 => '19503804',
1 => '19501594',
2 => '19498944',
),
19844271 =>
array (
0 => '19341140',
),
19498944 =>
array (
0 => '19503804',
1 => '19501594',
2 => '15539642',
),
16399898 =>
array (
0 => '15436391',
),
15436391 =>
array (
0 => '16399898',
),
19341140 =>
array (
0 => '19844271',
),
19560234 =>
array (
0 => '19544216',
1 => '19524884',
2 => '19540264',
),
19854683 =>
array (
0 => '19854557',
1 => '19854558',
2 => '19837033',
3 => '19854553',
4 => '19854565',
5 => '19854554',
),
19540264 =>
array (
0 => '19544216',
1 => '19524884',
2 => '19560234',
),
)
foreach($theduplicates as $k => $v){
foreach($v as $d){
if(isset($theduplicates[$d])){
unset($theduplicates[$d]);
}
}
// but it totally cleans theduplicates array
array(0) {
}
For every iteration you want to check they keys that have already been iterated. Therefore you want to keep track of these keys. I would do something like this:
$keys = [];
foreach ($main_array as $k => $set) {
if (in_array($k, $keys)) {
unset($main_array[$k]);
continue;
}
foreach ($set as $val) {
$keys[] = $val;
}
}
var_dump($main_array);
This code will also work, but you have to pass by reference:
foreach ($main_array as &$values) {
foreach ($values as $value) {
if(isset($main_array[$value])) {
unset($main_array[$value]);
}
}
}
If I understand your question (of which I am doubtful) this code should get you on your way
$array = [
0 => [5, 6, 7],
1 => [10],
5 => [10], // to remove
6 => [10], // to remove
7 => [10] // to remove
];
print_r($array);
foreach ($array as $key => $values) {
foreach ($values as $value) {
if(isset($array[$value])) {
unset($array[$value]);
}
}
}
print_r($array);
output..
Array
(
[0] => Array
(
[0] => 5
[1] => 6
[2] => 7
)
[1] => Array
(
[0] => 10
)
[5] => Array
(
[0] => 10
)
[6] => Array
(
[0] => 10
)
[7] => Array
(
[0] => 10
)
)
Array
(
[0] => Array
(
[0] => 5
[1] => 6
[2] => 7
)
[1] => Array
(
[0] => 10
)
)
I have had success merging two arrays by difference using the following code:
$a=array("2013-08-22"=>"12","2013-08-25"=>"5","2013-08-27"=>"10");
$b=array("2013-08-22"=>"1","2013-08-23"=>"3","2013-08-25"=>"5","2013-08-27"=>"10","2013-08-29"=>"5");
foreach ($b as $key => $value){
if(!array_key_exists($key, $a)){
$a[$key]=0;
}
}
This will return:
Array
(
[2013-08-22] => 0
[2013-08-23] => 0
[2013-08-25] => 5
[2013-08-27] => 10
[2013-08-29] => 0
[2013-12-22] => 12
)
The idea is for a to additionally hold the elements from b that are not present in a.
I am having issues now doing the same thing for the following array format:
$a=array(array("2013-12-22","12"),array("2013-08-25","5"),array("2013-08-27","10"));
$b=array(array("2013-08-22","1"),array("2013-08-23","3"),array("2013-08-25","5"),array("2013-08-27","10"),array("2013-08-29","5"));
I went to try this:
foreach ($b as $key => $value){
if(!array_key_exists($key, $a)){
$a[$key]=array($value[0], 0);
}
}
But the returned result is far from what I need:
Array
(
[0] => Array
(
[0] => 2013-12-22
[1] => 12
)
[1] => Array
(
[0] => 2013-08-25
[1] => 5
)
[2] => Array
(
[0] => 2013-08-27
[1] => 10
)
[3] => Array
(
[0] => 2013-08-27
[1] => 0
)
[4] => Array
(
[0] => 2013-08-29
[1] => 0
)
)
I understand they keys are no longer the dates, but how should I go about checking each array and making sure I don't get double entries?
$a = array(
array("2013-12-22","12"),
array("2013-08-25","5"),
array("2013-08-27","10"));
$b = array(
array("2013-08-22","1"),
array("2013-08-23","3"),
array("2013-08-25","5"),
array("2013-08-27","10"),
array("2013-08-29","5"));
$exists = array();
foreach ($a as $data) {
$exists[$data[0]] = 1;
}
foreach ($b as $data) {
if (array_key_exists($data[0], $exists)) {
continue;
}
$a[] = array($data[0], $data[1]);
}
$a now contains:
array(6) {
[0]=>
array(2) {
[0]=>
string(10) "2013-12-22"
[1]=>
string(2) "12"
}
[1]=>
array(2) {
[0]=>
string(10) "2013-08-25"
[1]=>
string(1) "5"
}
[2]=>
array(2) {
[0]=>
string(10) "2013-08-27"
[1]=>
string(2) "10"
}
[3]=>
array(2) {
[0]=>
string(10) "2013-08-22"
[1]=>
string(1) "1"
}
[4]=>
array(2) {
[0]=>
string(10) "2013-08-23"
[1]=>
string(1) "3"
}
[5]=>
array(2) {
[0]=>
string(10) "2013-08-29"
[1]=>
string(1) "5"
}
}