How to fill an array with other if some value match - php

Hi I'm trying to fill this array
Array
(
[0] => Array
(
[id] => 107
[name] => Sara
[msgSinleer] => 2
[subject] => Array
(
)
)
[1] => Array
(
[id] => 110
[name] => Manuela
[msgSinleer] => 27
[subject] => Array
(
)
)
)
with the values of this array if one of the ID match
Array
(
[0] => Array
(
[id] => 110
[Cliente] => Alexis
[body] => Hola
[fecha] => 06/01/2021 11:35 AM
[status] => 1
[tiempoSinLeer] => 15 01:00
)
[1] => Array
(
[id] => 110
[Cliente] => Alexis
[body] => Hola asd asdasd
[fecha] => 05/26/2021 11:35 AM
[status] => 1
[tiempoSinLeer] => 21 01:00
)
[2] => Array
(
[id] => 107
[Cliente] => George
[body] => vhgfshsgf
[fecha] => 06/15/2021 03:48 PM
[status] => 1
[tiempoSinLeer] => 1 08:48
)
)
So I'm trying to show the result in this way but I don't know how to do it, I tried some others methods like array_search, array_in and a foreach inside another foreach trying to see if inside can do an if statement but it doesn't work.
Array
(
[0] => Array
(
[id] => 107
[name] => Sara
[msgSinleer] => 2
[subject] => Array
(
[Cliente] => George
[body] => vhgfshsgf
[fecha] => 06/15/2021 03:48 PM
[status] => 1
[tiempoSinLeer] => 1 08:48
)
)
[1] => Array
(
[id] => 110
[name] => Manuela
[msgSinleer] => 27
[subject] => Array
(
[Cliente] => Alexis
[body] => Hola
[fecha] => 06/01/2021 11:35 AM
[status] => 1
[tiempoSinLeer] => 15 01:00
),
(
[Cliente] => Alexis
[body] => Hola asd asdasd
[fecha] => 05/26/2021 11:35 AM
[status] => 1
[tiempoSinLeer] => 21 01:00
)
)
)
And this is the code that I have that bring me the arrays
foreach ($query2 as $key2) {
$result2[] = $key2;
}
foreach ($query as $value) {
array_push($result , array(
"id" => $value['id'],
"name" => $value['name'],
"msgSinLeer" => $value['msgSinleer'],
"subject" => array(),
));
}
print_r ($result);
print_r ($result2);
I'm new in php so if someone can help me I will appreciate

Following logic might help you on your way. Cycle through array $arr0 to find matching id's in $arr1. When a match is found, push to the 'subject' element in $arr0.
foreach($arr0 as $key => $record) {
foreach($arr1 as $subject) {
if($record['id'] === $subject['id']) $arr0[$key]['subject'][] = $subject;
}
}
demo

Related

How to Compare two multidimensional associative arrays with differen items count

I have two multidimensional associative arrays with differen items count.
Important is that I don't know which aray will have more elements (A or B)
First array (A):
[0] => Array
(
[catID] => 65
[discount] => 10
[productID] => Array
(
[0] => 10887
[1] => 8508
[2] => 8350
)
[startDate] => 05/12/2022 12:00 am
[endDate] => 10/12/2022 12:00 am
)
[1] => Array
(
[catID] => 66
[discount] => 10
[productID] => Array
(
[0] => 13184
[1] => 10707
[2] => 8350
)
[startDate] => 10/12/2022 12:00 am
[endDate] => 15/12/2022 12:00 am
)
Second array (B):
[0] => Array
(
[catID] => 72
[discount] => 15
[productID] => Array
(
[0] => 16239
[1] => 16236
[2] => 10887
[3] => 13184
[4] => 8524
[5] => 13314
)
[startDate] => 12/12/2022 12:00 am
[endDate] => 15/12/2022 12:00 am
)
After compare these arrays (A, B) I'd like to retrive something like that:
Array A(remove productID if exists in array B):
[0] => Array
(
[catID] => 65
[discount] => 10
[productID] => Array
(
[1] => 8508
[2] => 8350
)
[startDate] => 05/12/2022 12:00 am
[endDate] => 10/12/2022 12:00 am
)
[1] => Array
(
[catID] => 66
[discount] => 10
[productID] => Array
(
[0] => 10707
)
[startDate] => 10/12/2022 12:00 am
[endDate] => 15/12/2022 12:00 am
)
Array B(no changes):
[0] => Array
(
[catID] => 72
[discount] => 15
[productID] => Array
(
[0] => 16239
[1] => 16236
[2] => 10887
[3] => 13184
[4] => 8524
[5] => 13314
)
[startDate] => 12/12/2022 12:00 am
[endDate] => 15/12/2022 12:00 am
)
Populate a flat "blacklist" array from your second array.
Loop over the first array's rows and filter the productId values against the blacklist array.
Code: (Demo)
$blacklist = array_merge(...array_column($b, 'productID'));
var_export(
array_map(
fn($row) => array_replace($row, ['productID' => array_values(array_diff($row['productID'], $blacklist))]),
$a
)
);
Or if you don't mind a classic foreach(), then this may be easier to read: (Demo)
$blacklist = array_merge(...array_column($b, 'productID'));
foreach ($a as &$row) {
$row['productID'] = array_values(array_diff($row['productID'], $blacklist));
}
var_export($a);

Get & Show Values Of Multi Dimensional PHP Array

I have Some API that returning me Results in JSON Format, I successfully Converted it into Multi-Dimensional Array.
Array
(
[status] => success
[cdr] => Array
(
[0] => Array
(
[date] => 2017-04-01 05:14:00
[callerid] => "ABC" <61344341227>
[destination] => 1604535320207
[description] => ABC1
[account] => ABC1
[disposition] => ANSWERED
[duration] => 10:57:57
[seconds] => 437
[rate] => 0.00200000
[total] => 0.06480000
[uniqueid] => 105943343
)
[1] => Array
(
[date] => 2017-04-11 05:10:00
[callerid] => "XYZ" <61343241227>
[destination] => 16045353250207
[description] => XYZ1
[account] => XYZ1
[disposition] => ANSWERED
[duration] => 13:57:57
[seconds] => 447
[rate] => 0.01100000
[total] => 0.06411000
[uniqueid] => 105911143
)
)
)
Kindly Help me in Fetching Data in Rows/Columns.
I wrote Below Code But it's saying undefined offset ....
foreach( $curl_jason as $key => $value)
{
echo $key;
echo $value['callerid'];
}
Try this:
foreach( $curl_jason['cdr'] as $data)
{
echo $data['callerid']; // will print callerid value
}

PHP Array issue , mulitple rows generated

I have an array that is fetched from database using some bindModel in cakephp.
finally now I want to change its format according to my liking to own.
here is the array contains.
Array
(
[0] => Array
(
[DriverLocation] => Array
(
[id] => 1
[dispensary_id] => 1
[driver_id] => 85
[zip_code_id] => 42
[created] => 2015-05-25 12:01:14
)
[ZipCode] => Array
(
[id] => 42
[province_id] => 3846
[city] => Rohtak
[zip_code] => 30215
[status] => active
)
[UserProfile] => Array
(
[first_name] => Arman
[last_name] => Kumar
)
)
[1] => Array
(
[DriverLocation] => Array
(
[id] => 2
[dispensary_id] => 1
[driver_id] => 85
[zip_code_id] => 43
[created] => 2015-05-25 12:01:14
)
[ZipCode] => Array
(
[id] => 43
[province_id] => 3846
[city] => Rohtak
[zip_code] => 15478
[status] => active
)
[UserProfile] => Array
(
[first_name] => Arman
[last_name] => Kumar
)
)
[2] => Array
(
[DriverLocation] => Array
(
[id] => 3
[dispensary_id] => 1
[driver_id] => 77
[zip_code_id] => 41
[created] => 2015-05-25 12:45:47
)
[ZipCode] => Array
(
[id] => 41
[province_id] => 3846
[city] => Malviya Vihar
[zip_code] => 12558
[status] => active
)
[UserProfile] => Array
(
[first_name] => Manish
[last_name] => Kumar
)
)
)
I want to convert or change its format something like as follows.
This will altered on the behalf of DriverLocation=>driver_id
Array
(
[0] => Array
(
[DriverLocation] => Array
(
[id] => 1
[dispensary_id] => 1
[driver_id] => 85
[zip_code_id] => 42
[created] => 2015-05-25 12:01:14
)
[ZipCode] => Array
(
[0] => Array
(
[id] => 42
[province_id] => 3846
[city] => Rohtak
[zip_code] => 30215
[status] => active
)
[1] => Array
(
[id] => 43
[province_id] => 3846
[city] => Rohtak
[zip_code] => 15478
[status] => active
)
)
[UserProfile] => Array
(
[first_name] => Arman
[last_name] => Kumar
)
)
[1] => Array
(
[DriverLocation] => Array
(
[id] => 3
[dispensary_id] => 1
[driver_id] => 77
[zip_code_id] => 41
[created] => 2015-05-25 12:45:47
)
[ZipCode] => Array
(
[id] => 41
[province_id] => 3846
[city] => Malviya Vihar
[zip_code] => 12558
[status] => active
)
[UserProfile] => Array
(
[first_name] => Manish
[last_name] => Kumar
)
)
)
You can use this loop:
foreach($ar as $k1=>$a1){
foreach($ar as $k2=>$a2){
if($k1 < $k2 && $a1["DriverLocation"]["driver_id"] == $a2["DriverLocation"]["driver_id"]){
$ar[$k1]["ZipCode"][] = $a2["ZipCode"];
if(isset($ar[$k1]["ZipCode"]["id"])){
$ar[$k1]["ZipCode"][] = array("id"=>$ar[$k1]["ZipCode"]["id"]);
unset($ar[$k1]["ZipCode"]["id"]);
}
unset($ar[$k2]);
}
}
}
Use array_push() function to make it
for sample example
<?php
$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);
?>
output
Array ( [0] => red [1] => green [2] => blue [3] => yellow )

array format issue PHP

I want to change an array format according to my needs. When I fetch data from a database using cakephp find('all') method, it returns something that is not in the format that I expected.
My resultant array is:
Array
(
[0] => Array
(
[DriverLocation] => Array
(
[id] => 15
[dispensary_id] => 1
[driver_id] => 85
[zip_code_id] => 43
[created] => 2015-05-20 12:25:34
)
[ZipCode] => Array
(
[id] => 43
[province_id] => 3846
[city] => Rohtak
[zip_code] => 15478
[status] => active
)
[UserProfile] => Array
(
[first_name] => Arman
[last_name] => Kumar
)
)
[1] => Array
(
[DriverLocation] => Array
(
[id] => 19
[dispensary_id] => 1
[driver_id] => 43
[zip_code_id] => 42
[created] => 2015-05-20 12:37:12
)
[ZipCode] => Array
(
[id] => 42
[province_id] => 3846
[city] => Rohtak
[zip_code] => 30215
[status] => active
)
[UserProfile] => Array
(
[first_name] => Pawan
[last_name] => Kumar
)
)
[2] => Array
(
[DriverLocation] => Array
(
[id] => 20
[dispensary_id] => 1
[driver_id] => 83
[zip_code_id] => 42
[created] => 2015-05-20 12:37:28
)
[ZipCode] => Array
(
[id] => 42
[province_id] => 3846
[city] => Rohtak
[zip_code] => 30215
[status] => active
)
[UserProfile] => Array
(
[first_name] => Ramesh
[last_name] => Saini
)
)
[3] => Array
(
[DriverLocation] => Array
(
[id] => 26
[dispensary_id] => 1
[driver_id] => 83
[zip_code_id] => 43
[created] => 2015-05-20 12:43:59
)
[ZipCode] => Array
(
[id] => 43
[province_id] => 3846
[city] => Rohtak
[zip_code] => 15478
[status] => active
)
[UserProfile] => Array
(
[first_name] => Ramesh
[last_name] => Saini
)
)
[4] => Array
(
[DriverLocation] => Array
(
[id] => 41
[dispensary_id] => 1
[driver_id] => 83
[zip_code_id] => 6
[created] => 2015-05-21 05:23:53
)
[ZipCode] => Array
(
[id] => 6
[province_id] => 3846
[city] => Whittier
[zip_code] => 90607
[status] => active
)
[UserProfile] => Array
(
[first_name] => Ramesh
[last_name] => Saini
)
)
)
And I want to convert it into something like this:
Array
(
[DriverLocation] => Array
(
[id] => 15
[dispensary_id] => 1
[driver_id] => 85
)
[ZipCode] => Array
(
[zip_code] => Array
(
[0] => 15478
)
[city] => Array
(
[0] => Rohtak
)
)
[UserProfile] => Array
(
[first_name] => Arman
[last_name] => Kumar
)
)
Array
(
[DriverLocation] => Array
(
[id] => 19
[dispensary_id] => 1
[driver_id] => 43
)
[ZipCode] => Array
(
[zip_code] => Array
(
[0] => 30215
)
[city] => Array
(
[0] => Rohtak
)
)
[UserProfile] => Array
(
[first_name] => Pawan
[last_name] => Kumar
)
)
Array
(
[DriverLocation] => Array
(
[id] => 20
[dispensary_id] => 1
[driver_id] => 83
)
[ZipCode] => Array
(
[zip_code] => Array
(
[0] => 30215
[1] => 15478
[2] => 90607
)
[city] => Array
(
[0] => Rohtak
[1] => Rohtak
[2] => Whittier
)
)
[UserProfile] => Array
(
[first_name] => Ramesh
[last_name] => Saini
)
)
Filter according to DriverLocation->driver_id
Seems that these arrays are exactly the same, except that you put every "record" of DriverLocation in its own variable. The idea here is that you receive the data from the DriverLocation Model in the fictional controller and put it in a variable; for example:
$allDriverLocations = $this->DriverLocation->find("all");
Now $allDriverLocations contains the first one of your mentioned arrays.
Next step is to pass it to the view:
$this->set(compact("allDriverLocations"));
And now to create a table in the associated view you should iterate through $allDriverLocations:
<table>
<tbody>
<?php
// Loop through the array with a foreach
foreach($allDriverLocations as $driverLocation){
// Create the table row here using the HtmlHelper.
// If you want to reach "Rohtak" for example, you use $driverLocation["Zipcode"]["city"] to print it.
}
?>
</tbody>
</table>
I wouldn't know why you should not follow Cake's easy conventions...

PHP Accessing an item in an array

I have the following array:
Cart Object
(
[event_list] => Array
(
[15] => stdClass Object
(
[event_id] => 15
[event_name] => North Pole Express 2014
[event_date] => 2014-12-06
[event_time] => 10:40:00
[event_ort_id] => 1
[ort_name] => Tanfield Railway
[ort_city] => Newcastle upon Tyne
[event_order_limit] => 0
[event_use_alt] =>
)
[14] => stdClass Object
(
[event_id] => 14
[event_name] => North Pole Express 2014
[event_date] => 2014-11-30
[event_time] => 10:40:00
[event_ort_id] => 1
[ort_name] => Tanfield Railway
[ort_city] => Newcastle upon Tyne
[event_order_limit] => 0
[event_use_alt] =>
)
[13] => stdClass Object
(
[event_id] => 13
[event_name] => North Pole Express 2014
[event_date] => 2014-11-29
[event_time] => 10:40:00
[event_ort_id] => 1
[ort_name] => Tanfield Railway
[ort_city] => Newcastle upon Tyne
[event_order_limit] => 0
[event_use_alt] =>
)
)
[cat_list] => Array
(
[138] => stdClass Object
(
[cat_id] => 138
[category_event_id] => 15
[cat_name] => Child - 4:00 pm
[cat_price] => 12.00
[cat_numbering] => none
)
[120] => stdClass Object
(
[cat_id] => 120
[category_event_id] => 14
[cat_name] => Child - 4:00 pm
[cat_price] => 12.00
[cat_numbering] => none
)
[102] => stdClass Object
(
[cat_id] => 102
[category_event_id] => 13
[cat_name] => Child - 4:00 pm
[cat_price] => 12.00
[cat_numbering] => none
)
)
[disc_list] => Array
(
)
[items] => Array
(
[4] => PlaceItem Object
(
[id] => 4
[cart] => Cart Object
*RECURSION*
[event_id] => 14
[category_id] => 120
[seats] => Array
(
[26151] => stdClass Object
(
[seat_id] => 26151
[seat_row_nr] => 0
[seat_nr] => 0
[seat_ts] => 1388769219
[discount_id] => 0
)
)
[ts] =>
[created] => 2014-01-03T16:43:39+00:00
[expired] =>
)
)
[ts] => 1388769219
)
I need to be able to access the 'Items' Key.
However everything I have tried so far has failed. I suspect I am missing something really obvious.
What would be the best way to access these items?
Cart is not an array, it's an Object.
$cart = new Cart();
$cart->items; // <-- this is an array (of PlaceItems)
Since $cart is an object you need to use object notation to access it, the arrays are underneath that:
forach ($cart->items as $key=>$value) {
//...
}
Update from your comment:
$items=$_SESSION['_SMART_cart']->items;
foreach ($items as $key=>$value) {
//...
}

Categories