I have array ...
Array
(
[0] => Array
(
[id] => 1
[number1] => 0
[value] => 3
)
[1] => Array
(
[id] => 2
[number1] => 0
[value] => 3
)
[2] => Array
(
[id] => 3
[number1] => 0
[value] => 11
)
[3] => Array
(
[id] => 4
[number1] => 1
[value] => 10
)
[4] => Array
(
[id] => 5
[number1] => 1
[value] => 13
)
current my code ...
$namearray=array();
foreach($myArray as $key => $value){
if($value['number1']==0){
$namearray[0][]=$value['value'];
}
if($value['number1']==1){
$namearray[1][]=$value['value'];
}
}
print_r($namearray);
my code current output:
Array
(
[0] => Array
(
[0] => 3
[1] => 3
[2] => 11
)
[1] => Array
(
[0] => 10
[1] => 13
)
)
How to use loop to replace if $value['number1']==0 and $value['number1']==1 ?
I maybe have more number1 number!
I try to use for loop but not work ! It will not display anything.
You could use the number1 value to select the index.
$namearray = [];
foreach ($myArray as $key => $value) {
$index = $value['number1']; // get the index
$namearray[$index][] = $value['value']; // use it
}
print_r($namearray);
ex:
$myArray = [
['id' => 1, 'number1' => 0, 'value' => 3],
['id' => 2, 'number1' => 0, 'value' => 3],
['id' => 3, 'number1' => 0, 'value' => 11],
['id' => 4, 'number1' => 1, 'value' => 10],
['id' => 5, 'number1' => 1, 'value' => 13],
];
$namearray = [];
foreach ($myArray as $key => $value) {
$index = $value['number1'];
$namearray[$index][] = $value['value'];
}
print_r($namearray);
Output :
Array
(
[0] => Array
(
[0] => 3
[1] => 3
[2] => 11
)
[1] => Array
(
[0] => 10
[1] => 13
)
)
Related
I need help. I have an array of items like this one:
[7646] => Array
(
[0] => Array
(
[id] => 156153
[tmplvarid] => 5
[value] => 2
)
[1] => Array
(
[id] => 56795
[tmplvarid] => 7
[value] => 430
)
[2] => Array
(
[id] => 56798
[tmplvarid] => 19
[value] => rate_08
)
),
[7647] => Array ()
And I need to change array indexes to value of property tmplvarid in sub array to transform array like this:
`[7646] => Array
(
[5] => Array
(
[id] => 156153
[tmplvarid] => 5
[value] => 2
)
[7] => Array
(
[id] => 56795
[tmplvarid] => 7
[value] => 430
)
[19] => Array
(
[id] => 56798
[tmplvarid] => 19
[value] => rate_08
)
)
How can I transform it in assosiative array ?
set index value from array value using foreach loop
Code
<?PHP
$arr = [
"7646" => array
(
[
"id"=> 156153,
"tmplvarid" => 5,
"value" => 2
],
[
"id"=> 56795,
"tmplvarid" => 7,
"value" => 430
],
[
"id"=> 56798,
"tmplvarid" => 19,
"value" => "rate_08"
]
)
];
echo "<pre>";
print_r($arr);
$newarr= [];
foreach($arr as $key => $value)
{
foreach($value as $key1 => $value1)
{
$newarr[$key][$value1['tmplvarid']] = $value1;
}
}
print_r($newarr);
?>
Output
Array
(
[7646] => Array(
[5] => Array
(
[id] => 156153
[tmplvarid] => 5
[value] => 2
)
[7] => Array
(
[id] => 56795
[tmplvarid] => 7
[value] => 430
)
[19] => Array
(
[id] => 56798
[tmplvarid] => 19
[value] => rate_08
)
)
)
I have a PHP array that looks like this...
Array
(
[0] => Array
(
[id] => 1
[value] => 111
[date] => 'today'
)
[1] => Array
(
[id] => 2
[value] => 222
[date] => 'today'
)
[2] => Array
(
[id] => 3
[value] => 333
[date] => 'today'
)
[3] => Array
(
[id] => 1
[value] => 111
[date] => 'today'
)
[4] => Array
(
[id] => 5
[value] => 111
[date] => 'today'
)
)
If I use array_unique like this...
print_r(array_unique($array, SORT_REGULAR));
It removes the duplicate [3] which is correct, but I am looking for a way to ignore [id] and only match by [date] and [value] so that my output looks like this...
Array
(
[0] => Array
(
[id] => 1
[value] => 111
[date] => 'today'
)
[1] => Array
(
[id] => 2
[value] => 222
[date] => 'today'
)
[2] => Array
(
[id] => 3
[value] => 333
[date] => 'today'
)
)
array_reduce + array_values() solution:
$arr = [
['id' => 1, 'value' => 111, 'date'=> 'today'],
['id' => 2, 'value' => 222, 'date'=> 'today'],
['id' => 3, 'value' => 333, 'date'=> 'today'],
['id' => 1, 'value' => 111, 'date'=> 'today'],
['id' => 5, 'value' => 111, 'date'=> 'today']
];
$result = array_values(
array_reduce($arr, function($r, $a){
if (!isset($r[$a['value'] . $a['date']])) $r[$a['value'] . $a['date']] = $a;
return $r;
}, [])
);
print_r($result);
The output:
Array
(
[0] => Array
(
[id] => 1
[value] => 111
[date] => today
)
[1] => Array
(
[id] => 2
[value] => 222
[date] => today
)
[2] => Array
(
[id] => 3
[value] => 333
[date] => today
)
)
Iterate over your array and get a key as concatenation of 'date' and 'value' fields. If this key has already been found - skip array value:
$pairs = [];
$new_values = [];
foreach ($array as $item) {
$key = $item['date'] . $item['value'];
if (empty($pairs[$key])) {
$pairs[$key] = 1;
$new_values[] = $item;
}
}
I'am bulding an app with laravel, and I have a problem, I have two array in php :
Array1
(
[0] => 15
[1] => 15
[2] => 16
[3] => 16
[4] => 17
[5] => 17
[6] => 17
[7] => 17
)
Array2
(
[0] => 0
[1] => 1
[2] => 1
[3] => 2
[4] => 0
[5] => 1
[6] => 2
[7] => 3
)
in this case i can not to use array_chunk because the value of array2 is dinamic and key of array1 must not be same if i combine it, so, how i can combine it to be like this :
Array
(
[15] => Array
(
[0] => 0
[1] => 1
)
[16] => Array
(
[0] => 1
[1] => 2
)
[17] => Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
)
Simple foreach loop:
$arr1 = [15,15,16,16,17,17,17,17];
$arr2 = [0,1,1,2,0,1,2,3];
$result = [];
foreach($arr1 as $k => $v){
$result[$v][] = $arr2[$k];
}
print_r($result);
The output:
Array
(
[15] => Array
(
[0] => 0
[1] => 1
)
[16] => Array
(
[0] => 1
[1] => 2
)
[17] => Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
)
$array1 =
[
0 => 15,
1 => 15,
2 => 16,
3 => 16,
4 => 17,
5 => 17,
6 => 17,
7 => 17,
];
$array2 =
[
0 => 0,
1 => 1,
2 => 1,
3 => 2,
4 => 0,
5 => 1,
6 => 2,
7 => 3,
];
$arr = array_unique($array1);
print_r($arr);
$newarray = [];
foreach($arr as $ar){
foreach($array1 as $key => $value){
if($ar == $value){
$newarray[$value][] =$array2[$key];
}
}
}
print_r($newarray);
Example code array: https://eval.in/639002
A guest with (user_id = 1) bought multiple tickets (ticket_id = 1 & 2) On event (event_id = 11).
I am expecting the result format:
[ticket] => Array
(
[1] => Ticket Name, ***
[2] => Ticket Name
)
Code example below:
$data_db = array(
array(
'id' => 1,
'user_id' => 1,
'event_id' => 11,
'ticket_id' => 1,
'user_name' => 'guest 1'
),
array(
'id' => 2,
'user_id' => 2,
'event_id' => 11,
'ticket_id' => 1,
'user_name' => 'guest 2'
),
array(
'id' => 3,
'user_id' => 3,
'event_id' => 22,
'ticket_id' => 1,
'user_name' => 'guest 3'
),
array(
'id' => 4,
'user_id' => 1,
'event_id' => 11,
'ticket_id' => 2,
'user_name' => 'guest 1'
)
);
$output = [];
foreach ( $data_db as $key => $row ) {
$output[ $row['event_id'] ]['event'] = 'Event Name';
$output[ $row['event_id'] ]['attendee'][ $row['user_id'] ] = $row;
$output[ $row['event_id'] ]['attendee'][ $row['user_id'] ]['ticket'][ $row['ticket_id'] ] = 'Ticket Name';
}
print_r($output);
Current result
Array
(
[11] => Array
(
[event] => Event Name
[attendee] => Array
(
[1] => Array
(
[id] => 4
[user_id] => 1
[event_id] => 11
[ticket_id] => 2
[user_name] => guest 1
[ticket] => Array
(
[2] => Ticket Name
)
)
[2] => Array
(
[id] => 2
[user_id] => 2
[event_id] => 11
[ticket_id] => 1
[user_name] => guest 2
[ticket] => Array
(
[1] => Ticket Name ***
)
)
)
)
[22] => Array
(
[event] => Event Name
[attendee] => Array
(
[3] => Array
(
[id] => 3
[user_id] => 3
[event_id] => 22
[ticket_id] => 1
[user_name] => guest 3
[ticket] => Array
(
[1] => Ticket Name
)
)
)
)
)
First of you could start by sorting out an array of tickets of which ticket belongs to which user:
$output = [];
foreach($data_db as $key=>$row) {
// Define new $tickets Array E.g ['guest_name']=>['ticket_id']
$tickets[$row['user_name']][] = $row['ticket_id'];
$output [] = [
'id'=>$row['id'],
'user_id'=>$row['user_id'],
'event_id'=>$row['event_id'],
'user_name'=>$row['user_name'],
'tickets'=>[]
];
}
Your output of $tickets would look like:
Array
(
[guest 1] => Array
(
[0] => 1
[1] => 2
)
[guest 2] => Array
(
[0] => 1
)
[guest 3] => Array
(
[0] => 1
)
)
Then you can combine them, like this:
foreach($output as $k=>$v){
foreach($tickets as $guest=>$ticket){
if($v['user_name'] == $guest){
$output[$k]['tickets'] = $ticket;
}
}
}
Your output is:
Array
(
[0] => Array
(
[id] => 1
[user_id] => 1
[event_id] => 11
[user_name] => guest 1
[tickets] => Array
(
[0] => 1
[1] => 2
)
)
[1] => Array
(
[id] => 2
[user_id] => 2
[event_id] => 11
[user_name] => guest 2
[tickets] => Array
(
[0] => 1
)
........
I would like to construct sub n number of arrays from a multi dimensional array depends on the data. For example: I have a main array as
Array
(
[0] => Array
(
[id] => 1
[status] => -1
)
[1] => Array
(
[id] => 2
[status] => 1
)
[2] => Array
(
[id] => 3
[status] => 2
)
[3] => Array
(
[id] => 4
[status] => 2
)
[4] => Array
(
[id] => 5
[status] => 2
)
)
I would like to get 3 arrays from this array depends on the no and type of status value
like
array{
[0]=array(
[status]=-1
[count]=1
)
[1]=array(
[status]=1
[count]=1
)
[2]=array(
[status]=2
[count]=3
)
}
Thanks in advance,
Sunil Kumar P
You mean like this?:
$array = array(
array('id' => 1, 'status' => -1),
array('id' => 2, 'status' => 1),
array('id' => 3, 'status' => 2),
array('id' => 4, 'status' => 2),
array('id' => 5, 'status' => 2)
);
$statuses = array();
foreach($array as $one){
if(!isset($statuses[$one['status']])){ $statuses[$one['status']] = 0; }
$statuses[$one['status']]++;
}
$newArray = array();
foreach($statuses as $key => $val){
$newArray[] = array('status' => $key, 'count' => $val);
}
print_r($newArray);
/*Array
(
[0] => Array
(
[status] => -1
[count] => 1
)
[1] => Array
(
[status] => 1
[count] => 1
)
[2] => Array
(
[status] => 2
[count] => 3
)
)*/