PHP Array Search to get the second result - php

i got an Array like:
array(127) {
[0]=>
array(2) {
[0]=>
string(14) "Info"
[1]=>
int(9) "28491231"
}
[1]=>
array(2) {
[0]=>
string(16) "description"
[1]=>
string(9) "Webserver"
}
[2]=>
array(2) {
[0]=>
string(11) "server_type"
[1]=>
string(9) "HOST"
}
[3]=>
array(2) {
[0]=>
string(2) "os"
[1]=>
string(7) "Windows"
}
....
What would the fastest way to search for "Info" in this Array and get the Value "28491231" ?
Thanks

I assume you have an array like this:
$sourceArray = array(
array('Info', '28491231'),
array('description', 'webserver'),
array('server_type', 'HOST'),
array('os', 'Windows'),
);
i.e.
If you want to get something specific from it, you can create a helper function like this:
// Helper Function
function getData($targetKey, $sourceArray) {
foreach ($sourceArray as $arrayItem) {
list ($key, $val) = $arrayItem;
if ($key == $targetKey)
return $val;
}
return false;
}
// Usage
var_dump(getData('Info', $sourceArray));
Ouputs:

Related

Array with strings to integers

I have some trouble with an array (php, wordpress) like shown below:
array(2) {
[0] => array(1) { [0]=> string(3) "416" }
[1]=> array(1) { [0]=> string(4) "1591" }
}
How to convert it to an array with integers?
The problem is that values are also arrays and not values like this:
array(2) {
[0] => "416" ,
[1]=> "1591"
}
I'm trying to get id of some posts using get_post_meta().
It is only a piece of my code:
$course_product = array();
foreach ($comment_ids as $comment_id) {
$course_product[] = get_post_meta( intval($comment_id), '_llms_wc_product_id', true );
}
It is giving me this strange array:
array(2) {
[0]=>
array(1) {
[0]=>
string(3) "416"
}
[1]=>
array(1) {
[0]=>
string(4) "1591"
}
}
Apparently, your meta data _llms_wc_product_id is itself an array. So, get the first value from it by appending [0]:
get_post_meta( intval($comment_id), '_llms_wc_product_id', true )[0]
This array can looks like that:
array(2) {
[0]=>
array(1) {
[0]=>
string(3) "416"
}
[1]=>
array(2) {
[0]=>
string(4) "1591"
[1]=>
string(3) "416"
}
}
It can't be deeper and I only need values so I'm using:
call_user_func_array('array_merge', $course_products);
to flatten it and after that it looks like:
array(3) {
[0]=> int(416)
[1]=> int(1591)
[2]=> int(416)
}
Then I can do what I wanted.
BIG THX.

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.

Group array row data by two columns creating a new 3-level structure

I have 1 arrays:
array(5) { // This is the keys on the CSV.
[0]=>
array(4) {
[0]=>
string(4) "user"
[1]=>
string(4) "date"
[2]=>
string(3) "md5"
[3]=>
string(4) "sha1"
}
[1]=>
array(4) {
[0]=>
string(4) "user1"
[1]=>
string(8) "02/02/15"
[2]=>
string(6) "123456"
[3]=>
string(5) "54321"
}
[2]=>
array(4) {
[0]=>
string(4) "user1"
[1]=>
string(8) "02/03/15"
[2]=>
string(6) "123456"
[3]=>
string(5) "54321"
}
[3]=>
array(4) {
[0]=>
string(5) "user2"
[1]=>
string(8) "02/02/15"
[2]=>
string(6) "112233"
[3]=>
string(6) "332211"
}
[4]=>
array(4) {
[0]=>
string(5) "user2"
[1]=>
string(8) "02/03/15"
[2]=>
string(6) "112244"
[3]=>
string(6) "332244"
}
}
So User 1 and User 2 each have a file that is MD5/SHA1 sum'd and checked.
I would like the data to turn from the above, to this:
array(3) {
["user"]=>
array(1) {
["date"]=>
array(2) {
[0]=>
string(3) "md5"
[1]=>
string(4) "sha1"
}
}
["user1"]=>
array(1) {
["02/02/15"]=>
array(2) {
[0]=>
string(6) "123456"
[1]=>
string(5) "54321"
}
["02/03/15"]=>
array(2) {
[0]=>
string(6) "123456"
[1]=>
string(5) "54321"
}
}
["user2"]=>
array(1) {
["02/02/15"]=>
array(2) {
[0]=>
string(6) "112233"
[1]=>
string(6) "332211"
}
["02/03/15"]=>
array(2) {
[0]=>
string(6) "112244"
[1]=>
string(6) "332244"
}
}
}
So far, I'm close. Here is my code:
<?php
error_reporting(E_ALL & ~E_NOTICE);
//Reading csv
$csv = array_map('str_getcsv',file('/var/www/sums.csv'));
debug($csv);
//User list
$userData = array();
foreach ($csv as $user) {
$ts = $user[1];
$un = $user[0];
$sums = array($user[2],$user[3]);
$userPreProc[$un] = array($ts => $sums);
$userData = array_merge($userPreProc, $userData);
}
debug($userData);
function debug($db) {
echo "<pre>";
var_dump($db);
echo "</pre>";
}
?>
The issue here is that only the first 'user1' and 'user2' entries are merged to the new array, so it looks like this:
array(3) {
["user"]=>
array(1) {
["date"]=>
array(2) {
[0]=>
string(3) "md5"
[1]=>
string(4) "sha1"
}
}
["user1"]=>
array(1) {
["02/02/15"]=>
array(2) {
[0]=>
string(6) "123456"
[1]=>
string(5) "54321"
}
}
["user2"]=>
array(1) {
["02/02/15"]=>
array(2) {
[0]=>
string(6) "112233"
[1]=>
string(6) "332211"
}
}
}
Any suggestions how I can ensure that all the keys are merged as expected?
Just shift off the first two elements and use those respective values as the first and second level keys.
Code: (Demo)
$result = [];
foreach ($array as $row) {
$result[array_shift($row)][array_shift($row)] = $row;
}
var_export($result);
Or use array destructuring in the foreach declaration: (Demo)
$result = [];
foreach ($array as [$user, $date, $md5, $sha1]) {
$result[$user][$date] = [$md5, $sha1];
}
var_export($result);
Just change your foreach loop to be like this. You don't need to merge your array via function.
foreach ($csv as $user) {
$ts = $user[1];
$un = $user[0];
$sums = array($user[2],$user[3]);
$userData[$un][$ts] = $sums;
}

Array search on a multidimensional array?

Here is the start of my array:
array(19) {
[0]=> array(3) {
["id"]=> string(2) "46"
["title"]=> string(7) "A"
["thumb"]=> string(68) "013de1e6ab2bfb5bf9fa7de648028a4aefea0ade816b935dd423ed1ce15818ba.jpg"
}
[1]=> array(3) {
["id"]=> string(2) "47"
["title"]=> string(7) "B"
["thumb"]=> string(68) "9df2be62d615f8a6ae9b7de36671c9907e4dadd3d9c3c5db1e21ac815cf098e6.jpg"
}
[2]=> array(3) {
["id"]=> string(2) "49"
["title"]=> string(6) "Look 7"
["thumb"]=> string(68) "0bfb2a6dd1142699ac113e4184364bdf5229517d98d0a428b62f6a72c8288dac.jpg"
}
}
How can I use array_search on this? I need to get the id of an element.
with use of === operator compared types have to be exactly same, in this code you have to search string or just use == instead ===.
function searchId($id, $array) {
foreach ($array as $key => $val) {
if ($val['id'] === $id) {
return $key;
}
}
return null;
}
$id = searchId('46', $yourarray);

Group same array values inside object into same keys

I have the array below:
[1]=>
array(2) {
[0]=>
object(stdClass)#23 (7) {
["AddressesTableID"]=> string(1) "8"
["AccreditNo"]=> string(13) "5129876de28ff"
["Type"]=> string(4) "home"
["Street"]=> string(34) "Wallace, Village, Under the bridge"
["Municipality"]=> string(8) "Tortuous"
["Province"]=> string(8) "Sardonic"
["ContactNo"]=> string(8) "92012010"
}
[1]=>
object(stdClass)#24 (7) {
["AddressesTableID"]=> string(1) "9"
["AccreditNo"]=> string(13) "5129876de28ff"
["Type"]=> string(6) "office"
["Street"]=> string(25) "Rasputin Query, Palpitate"
["Municipality"]=> string(7) "Opulent"
["Province"]=> string(6) "Number"
["ContactNo"]=> string(8) "29101011"
}
}
Can you guys help me on how to transform this into:
Where all the values are grouped as an array into similar keys?
["AccreditNo"]=> array(2) { "5129876de28ff", "GKIJUGUIKGI" }
["Type"]=> array(2) { "home", "home" }
["Street"]=> ...
["Municipality"]=> ...
["Province"]=> ...
["ContactNo"]=> ...
Try with:
$input = array( /* your input data*/ );
$output = array();
foreach ( $input as $data ) {
foreach ( $data as $key => $value ) {
if ( !isset($output[$key]) ) {
$output[$key] = array();
}
$output[$key][] = $value;
}
}
You can use the array_merge_recursive() function, as follows:
$arr = array_merge_recursive($arr1, $arr2)
for more information, please see here:http://www.php.net/manual/en/function.array-merge-recursive.php

Categories