How to add key to an existing array (PHP) - php

I have an existing array (as seen below)...
array(15) {
[0]=>
string(17) "orderid:100000154"
[1]=>
string(61) "shipping_method:channelunitycustomrate_channelunitycustomrate"
[2]=>
string(18) "qty_ordered:1.0000"
[3]=>
string(26) "shipping_firstname:John"
[4]=>
string(24) "shipping_lastname:Doe"
[5]=>
string(17) "shipping_company:"
[6]=>
string(36) "shipping_street1:123 Fake Street"
[7]=>
string(17) "shipping_street2:"
[8]=>
string(20) "shipping_city:LAUREL"
[9]=>
string(28) "shipping_postcode:20723-1042"
[10]=>
string(24) "shipping_region:Maryland"
[11]=>
string(19) "shipping_country:US"
[12]=>
string(21) "vendor_sku:3397001814"
[13]=>
string(16) "vendor_linecode:"
[14]=>
string(1) "
"
}
I have a desired key setup in this array -- the key for the first value would be orderid, so I'd like orderid => 1000000154
How would I go about this? I believe I have to explode the array again, but I'm not sure about the way to write it and none of my attempts have gotten me any closer.
Thanks!

Just loop through and set the keys and values using explode(). Use the first item in the exploded array as the key and the second as the value, then unset the existing item (the numeric-indexed array element) to clean up.
$input = array(
"orderid:100000154",
"shipping_method:channelunitycustomrate_channelunitycustomrate",
"qty_ordered:1.0000",
"shipping_firstname:John",
"shipping_lastname:Doe",
"shipping_company:",
"shipping_street1:123 Fake Street",
"shipping_street2:",
"shipping_city:LAUREL",
"shipping_postcode:20723-1042",
"shipping_region:Maryland",
"shipping_country:US",
"vendor_sku:3397001814",
"vendor_linecode:",
"
"
);
foreach($input as $key => $val) {
if(strstr($val, ":")) {
$exploded = explode(":", $val);
$input[$exploded[0]] = $exploded[1];
}
unset($input[$key]);
}
echo "<pre>";
var_dump($input);
echo "</pre>";
Outputs:
array(14) {
["orderid"]=>
string(9) "100000154"
["shipping_method"]=>
string(45) "channelunitycustomrate_channelunitycustomrate"
["qty_ordered"]=>
string(6) "1.0000"
["shipping_firstname"]=>
string(4) "John"
["shipping_lastname"]=>
string(3) "Doe"
["shipping_company"]=>
string(0) ""
["shipping_street1"]=>
string(15) "123 Fake Street"
["shipping_street2"]=>
string(0) ""
["shipping_city"]=>
string(6) "LAUREL"
["shipping_postcode"]=>
string(10) "20723-1042"
["shipping_region"]=>
string(8) "Maryland"
["shipping_country"]=>
string(2) "US"
["vendor_sku"]=>
string(10) "3397001814"
["vendor_linecode"]=>
string(0) ""
}

$result = array();
foreach($yourArray as $row) {
list($key, $value) = explode(":", $row);
$result[$key] = $value;
}

Related

how to split one array in to multiple arrays based on clientID in php

I am trying to split the array and create for each clienID one new array. I have not yet manage how to split the array to two new arrays (example has two clientIDs). All rows with the same clientID should be in one array:
We can see that I have two clientIDs '51df' and '51ff'
File with raw data:
clientID,Timestamp, Room temp., Pressure, Humidity, PM1.0, PM2.5
51df,05/04/2021 11:20:42,22.28,988.61,37.97,0,0
51ff,05/04/2021 11:20:53,22.29,988.57,37.95,0,0
51df,05/04/2021 11:21:05,22.29,988.55,37.94,0,0
51ff,05/04/2021 11:21:16,22.29,988.63,37.95,0,0
51df,05/04/2021 11:21:27,22.28,988.57,37.97,1,1
51ff,05/04/2021 11:21:38,22.30,988.61,37.95,1,1
51df,05/04/2021 11:21:50,22.30,988.74,37.94,0,0
...
array(24) { [0]=> array(7)
{ [0]=> string(2) "ID" [1]=> string(9) "Timestamp" [2]=> string(16) "Room temperature" [3]=> string(8) "Pressure" [4]=> string(8) "Humidity" [5]=> string(5) "PM2.5" [6]=> string(5) "PM1.0" } [1]=> array(7)
{ [0]=> string(22) "51df" [1]=> string(19) "05/04/2021 11:20:42" [2]=> string(5) "22.28" [3]=> string(6) "988.61" [4]=> string(5) "37.97" [5]=> string(1) "0" [6]=> string(1) "0" } [2]=> array(7)
{ [0]=> string(22) "51ff" [1]=> string(19) "05/04/2021 11:20:53" [2]=> string(5) "22.29" [3]=> string(6) "988.57" [4]=> string(5) "37.95" [5]=> string(1) "0" [6]=> string(1) "0" } [3]=> array(7)
{ [0]=> string(22) "51df" [1]=> string(19) "05/04/2021 11:21:05" [2]=> string(5) "22.29" [3]=> string(6) "988.55" [4]=> string(5) "37.94" [5]=> string(1) "0" [6]=> string(1) "0" } [4]=> array(7)
{ [0]=> string(22) "51ff" [1]=> string(19) "05/04/2021 11:21:16" [2]=> string(5) "22.29" [3]=> string(6) "988.63" [4]=> string(5) "37.95" [5]=> string(1) "0" [6]=> string(1) "0" } [5]=>
...
What I have until now. I am reading all the file into an array and do the transition to split each row at ',', which gives me the above printout:
<?php
$latestFile = '/client/logs/logging_multipleClients.out';
$tmp = tail($latestFile, $lines = 400, $buffer = 4096);
$tmp1 = preg_split( "/\n/", $tmp );
$data = array();
$table = 'ID,Timestamp,Room temperature,Pressure,Humidity,PM2.5,PM1.0';
$tmp2 = preg_split( "/\,/", $table );
array_push($data, $tmp2);
for ($j=1; $j < count($tmp1); $j++) {
$tmp2 = preg_split( "/\,/", $tmp1[$j] );
array_push($data, $tmp2);
}
var_dump($data);
?>
How do I now split this $data array into two arrays according to the clientIDs?
So, at the end I would like to have:
client['0'] with all the row with clientIDs '51df'
client['1'] with all the row with clientIDs '51ff'
After I manage that it should be rather easy to split the $data array into as many clientIDs the file could have.
Would it be easier if I would read the file line by line, something like this:
if (($handle = fopen($latestFile, "r")) !== FALSE) {
$data = array();
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
// do something here to push each line to the correspondent client array according to the clientID.
}
}
Thanks

Extract value from an array of array in PHP

I have a form and I serialised it to send it to PHP (AJAX) :
var dataString = $('#form_filtre').serializeArray();
I would like to extract in PHP value where names is "ou" :
array(1) {
["form_serialize"]=>
array(6) {
[0]=>
array(2) {
["name"]=>
string(3) "ctr"
["value"]=>
string(6) "maison"
}
[1]=>
array(2) {
["name"]=>
string(6) "action"
["value"]=>
string(17) "readHomesLocation"
}
[2]=>
array(2) {
["name"]=>
string(2) "ou"
["value"]=>
string(1) "8"
}
[3]=>
array(2) {
["name"]=>
string(2) "ou"
["value"]=>
string(1) "6"
}
[4]=>
array(2) {
["name"]=>
string(5) "quand"
["value"]=>
string(0) ""
}
[5]=>
array(2) {
["name"]=>
string(3) "max"
["value"]=>
string(3) "500"
}
}
}
I would like to extrat 6 and 8.
The problem, is that I don't know in advance how many "ou" I will have.
It can be from 0 to n
Seems like an easy approach would be to have PHP unserialize the array, then iterate over it with a foreach loop like this:
foreach($array as $index => $subArray) {
foreach($subArray as $key => $val) {
if ($key == "ou") {
$ouArray[$index] = $val;
}
}
}
(where $array, obviously, is your unserialized array)
You could also just use $ouArray[] = $val, if you don't care which element the ou belonged to.

I am unable to read the only emails in the form of array

I have one array. That array assign to one variable but i am unable to read only emails in that array.That array in given below.
$users = array(3) { [0]=> object(stdClass)#404 (4) { ["guid"]=> string(1) "2" ["name"]=> string(13) "xyz" ["username"]=> string(13) "xyz" ["email"]=> string(23) "xyz#gmail.com" } [1]=> object(stdClass)#405 (4) { ["guid"]=> string(3) "138" ["name"]=> string(12) "wxyz" ["username"]=> string(5) "wxyz" ["email"]=> string(21) "wxyz#gmail.com" } [2]=> object(stdClass)#406 (4) { ["guid"]=> string(3) "126" ["name"]=> string(13) "xxxx" ["username"]=> string(7) "xxxx" ["email"]=> string(17) "xxxx#gmail.com" } }
I need ouput like that
array(3) { [0]=> string(22) "xyz#gmail.com" [1]=> string(19) "wxyz#gmail.com" [3]=> string(19) "xxxx#gmail.com"}
Please give me reply.Advance thank you very much.
You can simply use an array_walk function over here like as
$result = [];
array_walk($users,function($v,$k)use(&$result){
$result[$k] = $v->email;
});
Using array_map
$result = array_map(function($v){return $v->email;},$arr);
You can try preg_grep() to return particular pattern only from an array.
preg_grep( "your Pattern", $array);
Check sample below
$email_only_array = preg_grep("/^[A-z0-9_\-]+[#][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z.]{2,4}$/", $your_array);
Example:
// Your array
$array = array("john","wxyz","aaa#mail.com", "wxyz", "bbb#mail.com", "ccc#mail.com");
// validating the value as array
$email_only_array = preg_grep("/^[A-z0-9_\-]+[#][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z.]{2,4}$/", $array);
// Output
print_r($email_only_array);
Output
Array ( [2] => aaa#mail.com [4] => bbb#mail.com [5] => ccc#mail.com )
Refer this also, http://php.net/manual/en/function.preg-grep.php

Merge array where duplicate in multidimensional array

I've been trying to merge this array ONLY WHERE $array[4] exists more than one time, example: $array[4] == 'red' exactly twice. How can I merge only those arrays while keeping the others? I have made several attempts at this and I am willing to include my efforts if asked.
Consider this array:
array(3) {
[0]=>
array(5) {
[0]=>
string(3) "UID"
[1]=>
string(3) "532"
[2]=>
string(1) "2"
[3]=>
string(9) "Domain(s)"
[4]=>
string(20) "red"
}
[1]=>
array(5) {
[0]=>
string(3) "UID"
[1]=>
string(3) "532"
[2]=>
string(7) "License"
[3]=>
string(3) "Fee"
[4]=>
string(20) "red"
}
[2]=>
array(5) {
[0]=>
string(3) "UID"
[1]=>
string(3) "536"
[2]=>
string(7) "License"
[3]=>
string(3) "Fee"
[4]=>
string(16) " University Test"
}
}
TRYING TO ACHIEVE:
array(3) {
[0]=>
array(5) {
[0]=>
string(3) "UID"
[1]=>
string(3) "532"
[2]=>
string(1) "2"
[3]=>
string(9) "Domain(s)"
[4]=>
string(20) " red"
[5]=>
string(3) "Fee"
[6]=>
string(7) "License"
}
[1]=>
array(5) {
[0]=>
string(3) "UID"
[1]=>
string(3) "536"
[2]=>
string(7) "License"
[3]=>
string(3) "Fee"
[4]=>
string(16) " University Test"
}
}
foreach ($test as $item) {
if (!isset($merged[$item[4]])) {
// add the item to the merged array using key=$item[4]
$merged[$item[4]] = $item;
} else {
// merge the item with the item that is already in the array at key=$item[4]
$merged[$item[4]] = array_unique(array_merge($merged[$item[4]], $item));
// array_unique is necessary because array_merge will not overwrite numeric keys
}
}
// convert the keys back to numeric (if you care to)
$merged = array_values($merged);

Fixing multidimensional array

I'm trying to make a multidimensional array that should print out like this (without formatting):
array(3) {
[0]=> array(5) {
[0]=> int(0)
[1]=> string(5) "Arena"
[2]=> string(18) "2012-05-3017:00:00"
[3]=> string(18) "2012-05-3000:00:00"
[4]=> string(33) "Masquerade Checkin (Participants)"
},
[1]=> array(5) {
[0]=> int(0)
[1]=> string(10) "Workshop 1"
[2]=> string(18) "2012-05-3017:00:00"
[3]=> string(18) "2012-05-3000:00:00"
[4]=> string(15) "Death Note (Live)"
},
[2]=> array(5) {
[0]=> int(0)
[1]=> string(7) "Video 6"
[2]=> string(18) "2012-05-3017:00:00"
[3]=> string(18) "2012-05-3000:00:00"
[4]=> string(26) "Takeuchi Fan Panel"
}
}
Notice from the above code that the inner array() length is always 5.
Here is my code below:
$loopsArray = array();
$data=array();
// graphing info come in here.
foreach ($events as $key => $event) {
$el=$event['event_location'] ;
$eln=$event['event_locationName'];
$ed=$event['event_date'];
$es=$event['event_start'];
$ee=$event['event_end'];
$en=$event['event_name'];
array_push($loopsArray,$el,$eln, $ed.$es,$ed.$ee,$en);
array_push($data,$loopsArray);
}
var_dump($data);
Here the print out
array(27) {
[0]=> array(5) {
[0]=> int(0)
[1]=> string(5) "Arena"
[2]=> string(18) "2012-05-3017:00:00"
[3]=> string(18) "2012-05-3000:00:00"
[4]=> string(33) "Masquerade Checkin (Participants)"
}
[1]=> array(10) {
[0]=> int(0)
[1]=> string(5) "Arena"
[2]=> string(18) "2012-05-3017:00:00"
[3]=> string(18) "2012-05-3000:00:00"
[4]=> string(33) "Masquerade Checkin (Participants)"
[5]=> int(13)
[6]=> string(11) "Autograph 1"
[7]=> string(18) "2012-06-2419:00:00"
[8]=> string(18) "2012-06-2422:00:00"
[9]=> string(17) "Parents and Anime"
}
//... continues
}
Notice that the inner arrays length double each iteration. array(5) array(10) array(15)array(20).
It doubles up to 60 elements in the last inner array. Each inner array should only have 5 elements in them. I don't understand why it is doubling or how to fix it.
Can you look over my loop and let me know how to fix it?
I have to use this multidimensional array for this code to work in JpGraph.
TIP : write $loopsArray = array(); inside foreach
better approach
instead of
array_push($loopsArray,$el,$eln, $ed.$es,$ed.$ee,$en);
array_push($data,$loopsArray);
try this
$temp = array ($el,$eln, $ed.$es,$ed.$ee,$en);
$data[] = $temp;

Categories