$email_users[] = $row;
print_r($email_users);
Result:
Array (
[0] => Array (
[user_id] => 87436
[username] => Admin
[user_email] => email#online.us
[user_lang] => de
[allowed] => 1 )
[1] => Array (
[user_id] => 68013
[username] => Testuser
[user_email] => email2#online.us
[user_lang] => de
[allowed] => 1 )
[2] => Array (
[user_id] => 68013
[username] => Testuser
[user_email] => email2#online.us
[user_lang] => de
[allowed] => 1 )
)
As you can see, the user_id 68013 is double. I have to remove the double Array. The Result should look like this:
Array (
[0] => Array (
[user_id] => 87436
[username] => Admin
[user_email] => email#online.us
[user_lang] => de
[allowed] => 1 )
[1] => Array (
[user_id] => 68013
[username] => Testuser
[user_email] => email2#online.us
[user_lang] => de
[allowed] => 1 )
)
I read and tried several solutions I found on stack. For example:
How to get unique value in multidimensional array
$email_users[] = $row;
$user_ids = array();
foreach ($email_users as $h) {
$user_ids[] = $h['user_id'];
}
$email_users = array_unique($user_ids);
print_r($email_users);
But the print_r is only:
Array ( [0] => 87436 [1] => 68013 )
Thanks for your time.
You should get distinct value from your query. Use SELECT DISTINCT * FROM tablename and you will get Distinct value.
Or use $email_users= array_unique($email_users, SORT_REGULAR);
Related
I've that xml structure retrieving from device
<packet>
<info action="fiscalmemory" fiscalmemorysize="1048576" recordsize="464" fiscal="1" uniqueno="ABC12345678" nip="123-456-78-90" maxrecordscount="2144" recordscount="7" maxreportscount="1830" reportscount="4" resetmaxcount="200" resetcount="0" taxratesprglimit="30" taxratesprg="1" currencychangeprglimit="4" currencychangeprg="0" fiscalstartdate="dd-mm-yyyy hh:dd:ss" fiscalstopdate="dd-mm-yyyy hh:dd:ss" currencyname="PLN" />
<ptu name="A" bres="Nobi">123.23</ptu>
<ptu name="B">123.23</ptu>
<ptu name="D">8</ptu>
<sale>999.23</sale>
</packet>
simpleXml does't see ptu's attributes
$array = simplexml_load_string($xml);
print_r($array);
It prints
SimpleXMLElement Object
(
[info] => SimpleXMLElement Object
(
[#attributes] => Array
(
[action] => fiscalmemory
[fiscalmemorysize] => 1048576
[recordsize] => 464
[fiscal] => 1
[uniqueno] => ABC12345678
[nip] => 123-456-78-90
[maxrecordscount] => 2144
[recordscount] => 7
[maxreportscount] => 1830
[reportscount] => 4
[resetmaxcount] => 200
[resetcount] => 0
[taxratesprglimit] => 30
[taxratesprg] => 1
[currencychangeprglimit] => 4
[currencychangeprg] => 0
[fiscalstartdate] => dd-mm-yyyy hh:dd:ss
[fiscalstopdate] => dd-mm-yyyy hh:dd:ss
[currencyname] => PLN
)
)
[ptu] => Array
(
[0] => 123.23
[1] => 123.23
[2] => 8
)
[sale] => 999.23
)
As we can see ptu doesn't contain attributes :/
I also tried parse it with recursive function because children also may contain chilren but without success :/
Could anybody point to me why SimpleXML doesn't take ptu's attributes and also share any parsing function?
Thanks in advance.
edited
As regards Nigel Ren I made that function
function parseXMLtoArray($xml){
$x = simplexml_load_string($xml);
$result = [];
function parse($xml, &$res){
$res['name'] = $xml->getName();
$res['value'] = $xml->__toString();
foreach ($xml->attributes() as $k => $v){
$res['attr'][$k] = $v->__toString();
}
foreach($xml->children() as $child){
parse($child, $res['children'][]);
}
}
parse($x, $result);
return $result;
}
$resArray = parseXMLtoArray($rawXml);
print_r($resArray);
this returns such array
Array
(
[name] => packet
[value] =>
[attr] => Array
(
[crc] => BKJFKHKD54
)
[children] => Array
(
[0] => Array
(
[name] => info
[value] =>
[attr] => Array
(
[action] => fiscalmemory
[fiscalmemorysize] => 1048576
[recordsize] => 464
[fiscal] => 1
[uniqueno] => ABC12345678
[nip] => 123-456-78-90
[maxrecordscount] => 2144
[recordscount] => 7
[maxreportscount] => 1830
[reportscount] => 4
[resetmaxcount] => 200
[resetcount] => 0
[taxratesprglimit] => 30
[taxratesprg] => 1
[currencychangeprglimit] => 4
[currencychangeprg] => 0
[fiscalstartdate] => dd-mm-yyyy hh:dd:ss
[fiscalstopdate] => dd-mm-yyyy hh:dd:ss
[currencyname] => PLN
)
)
[1] => Array
(
[name] => ptu
[value] => 123.23
[attr] => Array
(
[name] => A
)
)
[2] => Array
(
[name] => ptu
[value] => 123.23
[attr] => Array
(
[name] => B
)
)
[3] => Array
(
[name] => ptu
[value] => 8
[attr] => Array
(
[name] => D
)
)
[4] => Array
(
[name] => sale
[value] => 999.23
)
)
)
Is this correct?
Thanks again Nigel
When loading with SimpleXML, using print_r() gives only an idea of the content and doesn't have the full content. If you want to see the full content then use ->asXML()...
$array = simplexml_load_string($xml);
echo $array->asXML();
To check the attributes of <ptu> element, (this just gives the first one)...
echo $array->ptu[0]['name'];
This uses ->ptu to get the <ptu> element and takes the first one [0] and then takes the name attribute using ['name'].
How i can transform this first array [ONE}: Into second array [TWO} where i can call meta_key and than i get var: i get this array by calling specified sql rows and i am using this query: $user_data = $wpdb->get_results("SELECT * FROM $wpdb->usermeta WHERE user_id = $fivesdraft->user_id "); maybe its another better way to connect it in query? not in PHP
Array ONE
(
[0] => stdClass Object
(
[umeta_id] => 16
[user_id] => 2
[meta_key] => nickname
[meta_value] => user1
)
[1] => stdClass Object
(
[umeta_id] => 17
[user_id] => 2
[meta_key] => first_name
[meta_value] => testname
)
[2] => stdClass Object
(
[umeta_id] => 18
[user_id] => 2
[meta_key] => last_name
[meta_value] => testlastname
)
[3] => stdClass Object
(
[umeta_id] => 19
[user_id] => 2
[meta_key] => description
[meta_value] => user desc
)
)
Array TWO
(
[nickname] => 'user1'
[first_name] => 'user desc'
[last_name] => 'user desc'
[description] => 'user desc'
...
)
You can use array_reduce:
$myArray = array_reduce($arrayOne, function ($result, $item) {
$result[$item->meta_key] = $item->meta_value;
return $result;
}, array());
Just loop over the array and write the values in a new array
$two = array();
foreach ($one as $item) {
$two[$item->meta_key] = $item->meta_value;
}
How do I select 'peerIP' and 'client_ip' as IP address. (DATA given below)
I have gone this far.
$cursor = $collection->find();
$cursor->fields(array("payload.peerIP" => true, "payload.client_ip" => true, "payload.remote_host" => true, "payload.source" => true));
foreach ($cursor as $document) {
print_r($document['payload']);
}
which gives me:
Array ( [remote_host] => 00.00.00.000 )
Array ( [peerIP] => 000.00.000.00 )
.......
Array ( [client_ip] => 00.000.00.00 )
I am trying to achieve something like this:
Array ( [peerIP] => 00.00.00.000 )
Array ( [peerIP] => 000.00.000.00 )
.......
Array ( [peerIP] => 00.000.00.00 )
Any help? Is this even possible ?
DATA:
Array (
[session] => e283f2defa254228bb20527aeb334bf7
[peerIP] => 000.000.00.00
[commands] => Array ( )
[loggedin] => [startTime] => 2015-07-29T11:45:46.685927
[ttylog] => [hostPort] => 22 [peerPort] => 52963
[version] => SSH-2.0-libssh2_1.6.0
[urls] => Array ( )
[hostIP] => 127.0.0.1
[credentials] => Array (
[0] => Array (
[0] => root [1] => root ) )
[endTime] => 2015-07-29T11:45:49.976508
[unknownCommands] => Array ( ) )
Array ( [client_ip] => 000.00.000.00
[dist] => 23 [server_port] => 21
[timestamp] => 2015/07/31 02:41:04
[client_port] => 61447
[raw_sig] => 4:41+23:0:1400:63,0:mss,ws,sok,ts,eol+0::0
[params] => none
[server_ip] => 00.00.00.00
[subject] => cli [os] => ??? [mod] => syn )
PS: The values posted above are sys logs from different nodes, the outputs have different field names..
I have a problem. I am trying to count the array keys in my array using an specific element inside my array in the main array
public function index()
{
$user=User::all();
$lead=Lead::all();
$role=Role::all();
$lead=DB::table('lead')
->select(DB::raw('lead.id','users.number','users.username', 'lead.leadid','lead.first_name', 'lead.last_name','lead.email' ,count('lead.email') ))
->where ('lead.email', '>', 1)
->groupBy('lead.email')
->leftJoin('users', 'users.number', '=', 'lead.assign')
->get();
$user=DB::table('users')->select('users.number','users.username')->get();
echo "<pre>";
print_r($lead);
die();
}
Here is the output when I print it out
Array
(
[0] => Array
(
[id] => 63
[number] => 3
[username] => shankar
[leadid] => zcrm_125720000016007771
[first_name] =>
[last_name] => Amoah Owusu Richmond
[email] => ramoahhowusu50#gmail.com
)
[1] => Array
(
[id] => 64
[number] => 3
[username] => shankar
[leadid] => zcrm_125720000016007733
[first_name] => Deus
[last_name] => mathew
[email] => mathewdeus#gmail.com
)
[2] => Array
(
[id] => 65
[number] => 2
[username] => james
[leadid] => zcrm_125720000016007737
[first_name] => bari
[last_name] => safi
[email] => barisafi57#gmail.com
)
[3] => Array
(
[id] => 66
[number] => 11
[username] => nishupandey
[leadid] => zcrm_125720000016007741
[first_name] => Noorahmad
[last_name] => Noor
[email] => noorahmad.noor81#gmail.com
)
[4] => Array
(
[id] => 67
[number] => 12
[username] => ravi123
[leadid] => zcrm_125720000016007747
[first_name] => munsanje
[last_name] => nakeempa
[email] => mnakeempa#yahoo.com
)
[5] => Array
(
[id] => 68
[number] => 8
[username] => veerkishor
[leadid] => zcrm_125720000016007751
[first_name] => Noorahmad
[last_name] => Noor
[email] => noorahmad.noor71#gmail.com
)
[6] => Array
(
[id] => 69
[number] => 13
[username] => rahul
[leadid] => zcrm_125720000016007755
[first_name] => painad
[last_name] => sherzad
[email] => painda12sherzad#gmail.com
)
)
I want to count the elements using the email and check how many times the element is appearing in the array. Am trying to use the array_count_values but am not quite sure how to use it. Thanks in advance
Try this:
In your DB::raw, you need to put "count" inside quotes. Otherwise, you will just run php count (like sizeof).
<?php
$lead=DB::table('lead')
->select(['lead.id','users.number','users.username', 'lead.leadid','lead.first_name', 'lead.last_name','lead.email'])
->select( DB::raw( "count('lead.email') as total") )
->where ('lead.email', '>', 1)
->groupBy('lead.email')
->leftJoin('users', 'users.number', '=', 'lead.assign')
->get();
If you want to count the number of arrays with specific property inside on big array then you need to loop over all the outer array elements and check the inner array elements.
To count how many elements have the same e-mail, I guess you have to do it manually...
$emails = array();
foreach ($lead as $lineNumber => $line) {
$email = $line['email'];
if(array_key_exists("$email",$emails) {
$emails["$email"]++;
} else {
$emails["$email"] = 1;
}
}
echo "<pre>";
print_r($emails);
Is that what you want ?
Simply Use Count ($array) which u want to count
hope it will work
<input type="text" id="reference_code" name="reference_code[]" required class="form-control" value="<?php echo set_value("reference_code"); ?>"/>
How to use multidimentional to insert into MySQL using PHP?
Please check with my below code
Array
(
[reference] => asdf
[company_id] => 25
[contact] => Sam Deva
[sales_manager_demand] => 1
[team_leader_demand] => 11
[demand_date] => 11-05-2015
[target_date] => 27-05-2015
[state] => 11
[city] => 328
[description] => asdfasdfsadfdsf
[countdown1] => 485
[reference_code] => Array
(
[0] => 200
[1] => 300
)
[position] => Array
(
[0] => sales manager
[1] => sales manager
)
[pri_skill_id] => Array
(
[0] => 2
[1] => 7
)
[other_primary_tech] => Array
(
[0] =>
[1] =>
)
[sec_skill_id] => Array
(
[0] => 2
[1] => 5
)
[other_secondary_tech] => Array
(
[0] =>
[1] =>
)
[minimum_experience] => Array
(
[0] => 23
[1] => 12
)
How to insert these values into MySQL using a single foreach
$reference_code=$_POST['reference_code'];
$query="insert into tablename (reference,company_id,......,other_primary_tech) values "; //write insert with all the fields
$val="";
foreach($reference_code as $key=>$val){
$ref_code=$reference_code[$key];
$position=$_POST[position][$key];
$pri_skill_id=$_POST[pri_skill_id][$key];
$other_secondary_tech=$_POST[other_secondary_tech][$key];
$minimum_experience=$_POST[minimum_experience][$key];
$val.=" (".$_POST['reference'].",........,".$minimum_experience.")"; // values to insert
}
$query.=$val; //final query
You can try like this.....