how to convert array in php [duplicate] - php

This question already has answers here:
Convert multidimensional array into single array [duplicate]
(24 answers)
Closed 5 years ago.
Hi i got this array from my database.
array (size=4)
0 =>
array (size=1)
0 =>
array (size=1)
'email_1' => string 'denise#aaa.com' (length=18)
1 =>
array (size=1)
0 =>
array (size=1)
'email_1' => string 'denise#aaa.com' (length=18)
And i need to do get like this
array (size=4)
0 =>
array (size=1)
email_1' => string 'denise#aaa.com' (length=18)
1 =>
array (size=1)
'email_1' => string 'denise#aaa.com' (length=18)
I tried with array_merge and all. But no idea how to archive this?

Do it like below:-
$final_array = array();
foreach($original_array as $key=>$val){
$final_array[$key][] = $val[0]['email_1'];
}
print_r($final_array);
Output:-https://eval.in/848213

In php it is possible to do like this
foreach ($yourArray as $arr){
$result[] = $arr[0];
}
You can get also your desired output like this:
$result = array_map('array_collapse',$yourArray);

For this just assign like this to its first element.
foreach($yourArray as $array){
$array = $array[0];
}

Related

How can i select array elements what name contains numbers? [duplicate]

This question already has answers here:
How to filter an array by a condition
(9 answers)
Closed 2 years ago.
At first, im beginner.
I want to push elements from array to another array what does not contain numbers
I have an array1:
0 => string '142221A' (length=7)
1 => string 'hOUSES' (length=6)
2 => string 'bOOKS' (length=5)
3 => string 'sHOES' (length=5)
4 => string '92921' (length=5)
5 => string '12231' (length=5)
6 => string 'cARS' (length=4)
7 => string 'tOYS' (length=4)
The output i want like this, array2:
0 => string 'hOUSES' (length=6)
1 => string 'bOOKS' (length=5)
2 => string 'sHOES' (length=5)
3 => string 'cARS' (length=4)
4 => string 'tOYS' (length=4)
I dont want a solution, i want the way for it.
in PHP you can use is_numeric() method to check the string is a just a numeric or not as the following way:
$elements = ['142221A','hOUSES','bOOKS','sHOES','92921','12231','cARS','tOYS'];
$string_array = [];
foreach ($elements as $element) {
if(!is_numeric($element)) {
array_push($string_array, $element);
}
}
print_r($string_array);
but if you want to filter elements of an array to just have the elements that don't have any numeric value inside of it use the following way:
$elements = ['142221A','hOUSES','bOOKS','sHOES','92921','12231','cARS','tOYS'];
$just_string = [];
foreach ($elements as $element) {
//it will check for the element which has a digit number inside of it or not
//if it doesn't contain any number then it will be added to new array
if(preg_match('~[0-9]~', $element) != 1){
array_push($just_string, $element);
}
}
print_r($just_string);

PHP array_merge result incorrectly on new line

I am trying to array_merge id’s from one array into another, it’s merging but putting the new values on a new array line:
IDs to be added: $attachment_ids
array (size=2)
0 => string '2620' (length=4)
1 => string '2621' (length=4)
IDs which already exist and will be added to: $existing_data
array (size=1)
0 => string '2589,2561,2432,2422' (length=19)
result of my array_merge:
$merged_data = array_merge($attachment_ids, $existing_data);
is
array (size=3)
0 => string '2620' (length=4)
1 => string '2621' (length=4)
2 => string '2589,2561,2432,2422' (length=19)
My expected result is:
array (size=1)
0 => string '2620,2621,2589,2561,2432,2422'
If you just need the 1 string with all of the values, you could use implode() with the result you have so far...
$merged_data = [ implode(",", $merged_data) ];
Shortly array merge with array_map
$result=array_map(null,$array1,$array2);
array_merge() requires 2 arrays. The existing data is all in a string. With explode, this data can be converted into an array. With implode() a list is created after array_merge().
$attachment_ids = ['2620','2621'];
$existing_data = ['2589,2561,2432,2422'];
$existing_data = explode(',',$existing_data[0]);
$result = [implode(',',array_merge($attachment_ids,$existing_data))];
//$result: array(1) { [0]=> string(29) "2620,2621,2589,2561,2432,2422" }
Update
Alternatively, strings can be chained manually.
$result = [implode(',',$attachment_ids).','.$existing_data[0]];

Need help on PHP json_decode() [duplicate]

This question already has answers here:
Access array returned by a function in php
(5 answers)
How to extract and access data from JSON with PHP?
(1 answer)
How can I access an array/object?
(6 answers)
Closed 5 years ago.
Need help and advice on json_decode() in PHP. I am working on a CURL operation for an API call and the return result is a JSON. What is the syntax that I need to access the field pgid?
CURL Operation
$output = curl_exec($ch);
$rslt = json_decode($output, true);
var_dump($rslt);
The var_dump() output
array (size=1)
'protection-groups' => array (size=3)
0 => array (size=2)
'ppsDropped' => float 0
'pgid' => int 13
1 => array (size=2)
'ppsDropped' => float 7.9957930115316
'pgid' => int 18
2 => array (size=2)
'ppsDropped' => float 5302.7606884163
'pgid' => int 19
Try like this :
$output = curl_exec($ch);
$rslt = json_decode($output, true);
$idsArray = array();
foreach($rslt['protection-groups'] as $key => $value){
$pgId = $value['pgid'];
echo $pgId;
//Or in case you need all the PGID's in array then :
array_push($idsArray,$value['pgid']);
}
print_r($idsArray);
exit();
try below code that will loop through and get all pgid
foreach($rslt['protection-groups'] as $group)
{
echo $group['pgid'];
}

Converting multidimentional array into single array and removing the keys [duplicate]

This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 5 months ago.
array (size=1551884)
0 =>
array (size=1)
'entity_id' => string '131813' (length=6)
1 =>
array (size=1)
'entity_id' => string '213808' (length=6)
2 =>
array (size=1)
'entity_id' => string '712885' (length=6)
is it possible to convert it to single array without the key 'entity_id' without a loop?
array
0 =>
131813
1 =>
213808
2 =>
712885
I have tried this one :
call_user_func_array('array_merge', $array)
but somehow is only returning 1 element
UPDATE:
here are the benchmark results from the given answers to this question:
php version > 5.6
array_column: 0.20802903175354
foreach: 0.46231913566589
array_map: 1.021989107132
php version > 7
array_column: 0.079965829849243
foreach: 0.15323305130005
array_map: 0.28970503807068
This is also possible with array_column.
$result = array_column($your_array, 'entity_id');
You can do this very easily with array_map like this:
$result = array_map(function($value) {
return $value['entity_id'];
}, $originalArray);
Working example: https://3v4l.org/JOEMI
Of course you could also do it with a foreach loop:
$result = [];
foreach($originalArray AS $entity) {
$result[] = $entity['entity_id'];
}
Working example: https://3v4l.org/9J5XH
I prefer the first option personally.
Update: the accepted answer is clearly the best way. Do that! Leaving this here for comparison.

Filter associative array with keys array [duplicate]

This question already has answers here:
How to filter an associative array comparing keys with values in an indexed array?
(12 answers)
Closed 4 months ago.
I have following arrays:
$keys
array (size=2)
0 => string 'foo' (length=3)
1 => string 'buz' (length=3)
$data
array (size=3)
'foo' => int 1
'bar' => int 2
'buz' => int 3
How to get $data array filtered by $keys values ? Desired output:
array (size=3)
'foo' => int 1
'buz' => int 3
array_intersect_key should be able to help you out here
array_intersect_key($data, array_flip($keys));
The array_flip is needed because array_intersect_key operates on keys, so this makes sure both arrays are in the right format.
DEMO: http://codepad.org/AGpDAZtE

Categories